# Gameplay Game Engine Logic ECS but not really?

## Recommended Posts

Posted (edited)

Hello,
I have built games in the past in unity and XNA, android SDK etc. but it’s always been quite specific, when building a small Game Engine or more accurately a sub-set of code that is reusable across projects the tendency to structure the project falls towards composition of objects that are more "generic" than toward inheritance-based models.

For the Engine specific sub-systems, we have components such as a MeshInstance/Renderer that contains a Handle to a Mesh in a MeshLibary and a handle to a Material from the material library which in turn hold PSO data etc. We can also have Components such as SoundSorce, which are managed by an AudioSystem and contains the data to specify what sound, at what volume etc.

These systems can be Set up and shut down from within the Engine/Game class such as:

void Engine::Run()
{
if (Initialize())
{
//Main loop call the systems
//Dispatch Events
}

ShutDown();
}

void Engine::Initialize()
{
//Init all sub-systems
FileSystem.Init();
RenderSystem.Init();
PhysicsSystem.Init();
AudioSystem.Init();
WorldManager.Init(); //Creats Entities
}

void Engine::ShutDown()
{
//shutdown all sub-systems
WorldManager.ShutDown();
AudioSystem.ShutDown();
PhysicsSystem.ShutDown();
RenderSystem.ShutDown();
FileSystem.ShutDown();
}

But where does logic go? in smaller games like XNA or SFML you generally create some managers, let’s take the example of darkest dungeon.

They have various managers like:

• Darkest Dungeon Manager
• Campaign Selection Manager
• Estate manager
• Party formation manager
• Town manager
• etc..

All the logic and systems to run the game are based within the managers, and objects contain scripts of data that are fed thorough the systems.

In a small reusable code base how would you separate the logic from the engine, should like the smaller games all the managers just be shoved into the Engine Class even though that goes against a reusable data-driven framework? Should they just be stuck in a GameLogic system that's is initialized in the Engine Initialism function. And how do people tend to connect data scripts to the other various engine systems without causing too much game specific coupling. For example, you can use an Event System, but firing and event such as DAMAGED_BY_ZOMBIE and having the internal engine respond to that seems to break the separation from the low-high level of the engine system.

Would be great to here some opinions from the community on this subject as it is quite a vital and potentially problem prone aspect of engine/game development.

Thanks.

Edited by Jemme
Title Spelling Mistake

##### Share on other sites

Everyone has a different way of approaching structuring a game, so there are no right or wrong answers here - just options, and different justifications for choosing those options. Personally I think 'manager' classes are often a bad idea - these tend to mean there is global state, which makes things harder to test and debug. But if you're careful with them, they are a reasonable approach to dividing your game up along broad lines that correspond to features.

Separating the logic from the engine is often just a case of thinking through every code decision as you make it. When you write a function, consider whether it could be more generic. When you create a class, think about which part of it is specific to this game and which part is more general, and whether they could be separated. When one class references another class, consider whether an engine class is referencing a game-specific class, and if so, try refactoring it to decouple them (e.g. via events, or a generic interface, etc).

I don't think any of this advice really cares whether you have a traditional inheritance model for GameObjects, or entities with components, or a fully-fledged entity/component/system division. The problems are broadly the same and the aim is not to know up-front exactly how everything should be structured, but to spot when things need changing and make the changes as you go.

(Specifically regarding your event problem - it's hard to see why engine code would want to react to a DAMAGED_BY_ZOMBIE event because it's meaningless to the engine, which knows nothing about zombies, only to the game, which might. You might change it to a more generic CHARACTER_DAMAGED event, which could actually be handled meaningfully by an engine, with some data value inside the event designating the source of the damage, if it's necessary.)

##### Share on other sites

I guess its more about where game logic should live, in the XNA SFML style games, the main Game class becomes bloated with a bunch of systems for logic in most examples. Because the framework is custom though your initializing all the engine systems then the logic systems in the same place. It feels like the engine should be a self contained system, but both the game logic and the engine need to be hooked into a Main loop. the logic needs to know about the entities and so does the engine, it needs to sync up the engine specific components like MeshInstance. The World or Scene stores lists of Entities which have ID's and handles to the Engine specific components, but the world/scene shouldn't really have the logic? because technically the Scene/world is part of the engine in the scenes a small engine should provide the ability to create its entities and sub-system compoentns independent of what ever game it is.

For examples, a lot of games will Use Mesh's so the Scene should let you make an Entity with a transform and a mesh to render for you, what ever logic you want to apply shouldn't factor into the World/scene at all.

So this leads down to the option of having a separate GameLogic system that stores all the game specific systems within it, like menu system, enemy spawner etc. that all hook back into the world manager using the entity id's.

Other systems have a BehaviorComponent that they attach and update on each entity like unities mono behavior but logic in the component can sometimes be seen negatively.

3 hours ago, Kylotan said:

no right or wrong answers here - just options

There is industry wisdom though, that could pro/con the placement of logic.

Im going to try the GameLogic method and see how that goes.

Thanks

##### Share on other sites

You mention 'industry wisdom', and my 10 years of industry wisdom told me that everyone does this differently.

16 hours ago, Jemme said:

in the XNA SFML style games, the main Game class becomes bloated with a bunch of systems for logic in most examples﻿

If you make a single class and then choose to bloat it, that's your decision. There is always going to be some top-level object and you always have the option of subdividing it along the lines that make sense to you. Start simple and refactor as you go.

16 hours ago, Jemme said:

It feels like the engine should be a self contained system﻿, but both the game logic and the engine need to be hooked into a Main loop

Nothing can be fully self-contained. The idea is that you have layers. The low level libraries like SFML wrap around the operating system, your own 'engine' code might wrap around SFML, and your game code will mostly wrap around whatever engine code you have. It's totally optional to have 'engine' code - you can just have your game reference SFML directly if you like. (In fact, I'd probably recommend that.)

15 hours ago, Jemme said:

So this leads down to the option of having a separate GameLogic system that stores all the game specific systems within it, like menu system, enemy spawner etc. that all hook back into the world manager using the entity id's. ﻿

Yes, you probably want some sort of class that represents your game-specific behaviour. It might store game specific systems within it. They may well use entity IDs. This isn't particularly controversial so it's unclear why you seem worried about it.

16 hours ago, Jemme said:

Other systems have a BehaviorComponent that they attach and update on each entity like unities mono behavior but logic in the component can sometimes be seen negatively. ﻿

Most games work this way. The Unity and Unreal engines are both designed to facilitate this approach as the main way of implementing game logic. Don't let some opinionated blog posts deter you from this if you think it would work for you.

##### Share on other sites

There isn't really a good way to implement the game logic / components that interact with the underlying engine, these are the common approaches which i have used in different projects with the pro/cons.

• OOP Inheritance:
• Pro:
• Makes sense to normal programmers, easy to explain.
• Works for simple systems
• Con
• Monolithic class hierarchies
• Multiple inheritance
• Deep wide hierarchies
• bubble up effect
• Entity Component
• pro
• Easy to implement / explain
• Works for most systems
• Allows many Entities of different types
• Con
• Not friendly to cache
• pointer chasing
• virtrual function, vtable cache miss
• spaghetti pointer chasing in the Update loop
• Entity Component System
• pro
• Easy to implement / explain
• Works for most systems
• Allows entities of different types
• Data Orientated Design (contiguous)
• No vtables
• ID's allow reallocation + avoid dangling pointer
• con
• Claimed cache benefits fall away when you have to chase a pointer to a GameObject to get a Transform for a meshrender or physics system to act upon it.
• Using ID's causes dependency on systems. e.g (RenderSystem.GetComponent(Entity.ID)) so now the script / other system has to suddenly know about every other system it needs data from as opposed to just having a pointer to the data.
• Still spaghetti pointer chasing in the update loop.

There the common way's you see being talked about, another method is to use a message system which makes sense. However, should you really have a message for every possible thing that can happen + every system having to know about every possible thing that can happen and just ignoring it. You could store a position with your MeshRenderer if a message is received to update position you can just update it that way it is cached with the MeshRender, the physics can cache its version, yes its duplication of data but ti stops pointer chasing.

So basically every system/method i have encountered improves something at the cost of making something else less efficient.

##### Share on other sites

You don't need to pick one of these three routes - you can have all of the above, in the areas where it makes sense. It's not ideal to obsess over low level things like vtables or cache misses unless you know you have a system where that is going to matter.

It's also not right to consider a message system to be some sort of 4th approach - that is just a way of handling communication between things. I don't think anybody serious is trying to create an entire system where absolutely everything is handled by messaging. It is however useful for communication between completely unrelated systems - but so is the observer pattern, or callbacks, or events, or lambdas, or whatever your language provides to keep coupling low.

All programming is about trade-offs. Dwelling on it too much at the start is a good recipe for never getting anywhere. Start coding, and improve your code as you go along.

##### Share on other sites

There's a significant difference between

On 6/11/2018 at 4:29 PM, Jemme said:

I have built games in the past in unity and XNA, android SDK etc.

On 6/11/2018 at 4:29 PM, Jemme said:

For the Engine specific sub-systems, ﻿we have components such as ...

On 6/11/2018 at 4:29 PM, Jemme said:

But where does logic go? in smaller games like XNA or SFML you generally create some managers

On 6/11/2018 at 4:29 PM, Jemme said:

In a small reusable code base how would you separate the logic from the engine, should like the smaller games all the managers just be shoved into the Engine Class even though that goes against a reusable data-driven framework?

On 6/11/2018 at 8:42 PM, Jemme said:

It feels like the engine should be a self contained system, but both the game logic and the engine need to be hooked into a Main loop.

Are you trying to extract an engine from your old games, to develop a new or improved engine for future use, or simply to find a good architecture for a new game? Sometimes you appear to have an established engine you want to improve, sometimes not.

The indecision between a main loop that belongs in the engine or in the game logic is a particularly troubling instance of a problem you shouldn't have: either you use adopt an existing game engine that "owns" the main loop and lets you write callbacks etc.:to customize it, or you adopt some less opinionated framework (like the mentioned XNA or SFML) and you just write (or adapt from examples) a suitable main loop for your game that will be eventually refactored and decomposed into different modules and layers as its complexity increases.

##### Share on other sites

I'm building everything from scratch, all the low-level, dx11, gl etc.  In the past many years ago I used xna and sfml for specific projects like build an RPG etc. But now I'm doing everything  and I want it to be reuseable, I'm going the route of having a game logic system which initialises all the logic systems and using lua for behaviour scripts and JSON for components initialisation on entities.

##### Share on other sites
On 6/11/2018 at 6:17 PM, Kylotan said:

- it's hard to see why engine code would want to react to a DAMAGED_BY_ZOMBIE event because it's meaningless to the engine,

Each bullet fired can be interpritated as damane message with dalayed delivery and unknown at send time recepient. And bullet manager/subsystem can be interpritated  as special case of messaging system,

##### Share on other sites
2 hours ago, Jemme said:

But now I'm doing everything  and I want it to be reuseable, I'm going the route of having a game logic system which initialises all the logic systems and using lua for behaviour scripts and JSON for components initialisation on entities.

If you want really reusable code, decleaning of inheritance-based model is wery wery bad idea. inheritance-based model is key feature to creation of self-managed objects that can be just added to world simulation and than do anything else himself, including desigin about deletion from model.

First that you need is a persistent object model. And use json or other human readable format is wery bad idea for storing and editinf object properties becouse it give no waranty of data correctness. It can be given only by especially made property editor. So human-readability not make a human writeability, and only makes a parsing expenses and difficulties in comparsion with binary data formats, expecially when used with static typed languages.

## Create an account

Register a new account

• ### Similar Content

• So, as the title says i am trying to figure out a good way sync all that information with other players in Unity. My problem is that i can't come up with a good solution since i am used to creating classes for everything e.g. attachments are its own class and then the weapon would save a reference to that attachment. But since you can't send custom classes over [Command] & [ClientRPC] i am a little stuck. A solution for this would be giving each attachment for a slot a unique ID and then passing the ID to other player but i feel like that is very error prone if other ppl add a new attachment or the IDs get mixed up.
Is there a "standard" way that this is usually done that i am missing?
I am fairly new to programming so any help is appreciated!

• Hello!
I am seeking a few team members to help me run some survival sandbox game servers through a 3rd party hosting site. My goal is to replace "official" servers in the sense that our servers offer the best experience for players of all types (not easy to do!). Why do I need a team for this? Because the game we are working with has far more potential than most people realize - it is my hope that I can bring a better ruleset to the table that focuses more on fun-factor than a genre label.
My end-game goal is to run quite a few long-term small MRPG servers across multiple games/platforms (platforms of course based on availability of supported server hosting). Personally, I see multiplayer and co-op gamers gravitating towards a more personal experience with small, MRPG servers, as opposed to something like an MMORPG.
Current Progress:
Design Document
Server Patch Notes
I took 6 years of high school and have zero college education Zero game/software development experience Hate math, can't code, also can't create any type of art assets Musician of almost 30 years Work on songwriting and music production in a metal band based in my region Only have hobbies for things I'm passionate about, have had some success professionally, with more to come Can I build a toaster, no, but I can tell you exactly how it should work without confusing you That being said, I have made a lot of progress this year on my own - and currently have one server up and running. I am at a point now where I need to construct a small team to help me get my other servers up and running, as well as adding/creating custom content.
Currently looking for:
1-2 x Mod Developers 1 x Server Administrator You must have access to the game we are running our server on, as well as any other tools that are needed to get the job done (like Unreal 4 engine, software development tools, etc.)
I am more than willing to provide information as requested for anyone interested!
Helgraves

• Hopefully this is a simple question for some of you. I've managed to create a nice fly cam:

...but now I'd like to make a standard fps control. If I keep the y axis at 0, I don't fly up or down and I obviously move around at y0, but if I look down, my player slows down. Keep in mind, I'm using my own little engine btw.
I'm guessing this has to do to the fact that my w,a,s,d forward vectors are calculated using the model matrix which also calculated XYZ rotation. Making a google search, I found this link, but it's an Unreal related question, and on top of that using their graphical blueprint whatever you call it. But it's pretty much the same question and (I'm guessing) possible answer.
What I think I should do
Should I make a completely different model matrix that ignores the X and Z axis which I can use for the player's movement? I'm a little confused in what I can do, and I can't find anything on Google that isn't THREE.js or Unity related.
I'm pretty sure I have to disconnect the look controls from the movement controls. I'd like to be able to move towards where I'm looking without flying up or down, but at the same time be able to apply gravity at some point. But for now, I'd really like to solve that "look down move slow" issue.
Thanks all for your support. Below I'll share some of the code I wrote for the movement.

// Controls flyCam(camSpeed) { if (Game.Input.Key.rightArrow.down) { Game.camera.translateX(camSpeed); } if (Game.Input.Key.leftArrow.down) { Game.camera.translateX(-camSpeed); } if (Game.Input.Key.upArrow.down) { Game.camera.translateY(-camSpeed); } if (Game.Input.Key.downArrow.down) { Game.camera.translateY(camSpeed); } Game.camera.transform.rotation.y += Game.Input.Mouse.delta.x * (200.0 / Game.Renderer.width/2) * Game.Time.step; Game.camera.transform.rotation.x += Game.Input.Mouse.delta.y * (200.0 / Game.Renderer.height/2) * Game.Time.step; } // TranslateXYZ translateX (speed) { this.transform.position.x += this.transform.right.x * speed * Game.Time.step; this.transform.position.y += this.transform.right.y * speed * Game.Time.step; this.transform.position.z += this.transform.right.z * speed * Game.Time.step; } translateY (speed) { this.transform.position.x += this.transform.up.x * speed * Game.Time.step; this.transform.position.y += this.transform.up.y * speed * Game.Time.step; this.transform.position.z += this.transform.up.z * speed * Game.Time.step; } translateZ (speed) { this.transform.position.x += this.transform.forward.x * speed * Game.Time.step; this.transform.position.y += this.transform.forward.y * speed * Game.Time.step; this.transform.position.z += this.transform.forward.z * speed * Game.Time.step; } // Model Matrix Mat4.translate(translation, this.position.x, this.position.y, this.position.z); Mat4.scale(scale, this.scale.x, this.scale.y, this.scale.z); Mat4.rotateX(rotateX, this.rotation.x); Mat4.rotateY(rotateY, this.rotation.y); Mat4.rotateZ(rotateZ, this.rotation.z); Mat4.mul(rotateXY, rotateY, rotateX); Mat4.mul(rotation, rotateXY, rotateZ); Mat4.mul(rotScale, rotation, scale); Mat4.mul(model, translation, rotScale); // Update Direction Mat4.transformVec4(this.forward, FWD, model); Mat4.transformVec4(this.up, UP, model); Mat4.transformVec4(this.right, RIGHT, model);
• By drcrack
It is a combination of fundamental RPG elements and challenging, session-based MOBA elements. Having features such as creating your unique build, customizing your outfit and preparing synergic team compositions with friends, players can brave dangerous adventures or merciless arena fights against deadly creatures and skilled players alike.

• I wanted to get some thoughts on player character collisions in MMOs for my project.   As I recall the original Everquest had them, however WoW didn’t. I also remember Age of Conan had them but I’m guessing they were done on the server which was highly annoying because you could collide with things that were unseen. I’m not sure how EQ did them but I don’t remember having this problem in EQ, but it was a long time ago.

My general idea as to do player collisions on the client side. A collision would only affect a given clients player character as seem from that client. On the server, characters would pass right through each other.  This means that because of lag, two players might see different things during a collision or one may think there was a collision while the other doesn’t, but I figure that’s less annoying than colliding with something you can’t see and everything should resolve at some point.

There is one more case which I can see being a bit problematic but I think there might be a solution (actually my friend suggested it).  Suppose two characters run to the same spot at the same time.  At the time they reached the spot it was unoccupied but once the server updates each other’s position, they both occupy the same space. In this case after the update, a force vector is applied to each character that tries to push them away from each other. The vector is applied by each client to its own player. So basically player to player collisions aren’t necessarily absolute.

I was also thinking you could generalize this and allow players to push each other.  When two players collide their bounding capsule would be slightly smaller than the radius where the force vector would come into play. So if you stood next to another player and pushed he would move.  By the vector rules he is pushing back on you (or rather your own client is pushing) but since your movement vector could overcome the collision force vector, only he moves unless he decides to push back. You could add mass calculation to this, so larger characters could push around smaller characters more easily.

Of course there are griefing aspects to this, but I was thinking I would handle that with a reputation/faction system. Any thoughts?

• 11
• 19
• 12
• 9
• 34
• ### Forum Statistics

• Total Topics
631398
• Total Posts
2999840
×