Demise of the "game entity".

Started by
18 comments, last by ddn3 12 years, 9 months ago
It will be interesting to see where he takes his articles.


The concept of a game entity is such a vague and nebulous term it could mean many different things. It would be foolish in the extreme to have many hundred million pieces of data placed into a common pool that is constantly polled for no reason. Nobody does that in the real world, nor would they.

Many systems do have "entities" which are composed of various parts. Unity3D does this very nicely, and it is similar to examples I have seen in the regular work environment. Again, they aren't blindly running all processing work on every piece of data, but intelligently selecting what to run and then those items in turn pruning down to only processing the data that actually needs it.



It is common to have a set of management classes which are always updated, and then they coordinate the work within that subsystem.

For example, you have a particle system, and it determines what work needs to take place within that system. You don't have every particle mixed in with AI processing and with network updates, but a single system that runs it's portion of work only as needed. Similarly you have a networking system which is always updated, and determines what work needs to take place, if any. Same for the AI system, perhaps updating only a few of its AI actors as needed; the system is always updated, but the individual AI actors do not necessarily need to be polled every update.

Even with a single large tree, that is still something that can be made trivially parallel by maintaining an 'old' and 'new' value (often with three rather than two), which large systems will likely do anyway since since rendering and updating are typically decoupled. Rendering is an interpolation between two states. With subsystems instead of a giant tree the parallelization concerns are also unfounded, since each of those subsystems can be updated in parallel, and often the individual parts within them can be updated in parallel.



Hopefully the rest of his articles are carefully considered.
Advertisement
Thank you very much for your replies.
Perhaps I need to make clear that scripts are involved in some parts of the discussion. That is, I am not, in general, talking about code that gets executed natively that the application knows about.

To be completely honest, I don't think I am going to drop my generic [font="Courier New"]Object[/font] class. While I understand the point in not allowing a particle system to stay together with AI controllers, it seems to deliver some added value to me. For example, I like very much Object::finalize and Object::hashCode. The 'game entity' would have been something definitely higher-level. I think having a common base class is not really bad by itself. Sure abusing it is another business.

What I can see from the examples is that there's a recurrent theme about behavior you know about classes you know. Let's call those "core" objects/classes/interfaces. Pooling in the right place is relatively easy for those objects, sure we can update it with no trouble.
The problem arises from the others, let's call them "user entities". They are, W.R.T. the core class definition, different/unknown in behavior and need to execute code which is likely to be different, the component model cleans up a lot here but in general to make the system flexible, I'll need to have some kind of listeners to events.
For example, when CD code determines two thingies collided, it will issue a [font="Courier New"]Bump[/font] call, from now on, I have no guarantee what's going on. Sure, the call is now [font="Courier New"]Bump[/font], and not [font="Courier New"]Update[/font], and called from a more specific context, and thus hopefully not abused. Yet it likely involves updating something and this may have the same issues associated with [font="Courier New"]Update[/font], albeit on a smaller scale, or perhaps not? Am I missing something here? Perhaps I am thinking too generic?

When I wrote about constraints, I meant inter-object constraints. To satisfy those, I was planning to let the script provide the required "sync" code, or perhaps I should allow them to register "update groups"? I wonder if I need to keep track of update status on a per-object, per-tick basis.
After all, if the script can define a function, it can probably call it as well.

Inter-task constraints don't worry me much. Ok, they do but I'm positive I'll figure them somehow.

I understand the point about composition and I'm glad to read that my approach about a family of well-known sub-entities instead of a single generic entity was correct. I personally don't think that involves a big departure from OO (we're basically taking advantage from the implementation provided by a specific derived class exposing a certain interface in a way or the other, some kind of manual devirtualization).
Still an update call, perhaps event-based, needs to be here. Similarly, some kind of propagation must go on albeit hopefully for a reason. Ok, I'll smartly update the classes I know about but I had the impression the original concept involved sub-types in the same pool... perhaps I am taking the linked post too seriously, I'll surely want to still think at this for a while.

Previously "Krohm"


What I can see from the examples is that there's a recurrent theme about behavior you know about classes you know. Let's call those "core" objects/classes/interfaces. Pooling in the right place is relatively easy for those objects, sure we can update it with no trouble.
The problem arises from the others, let's call them "user entities". They are, W.R.T. the core class definition, different/unknown in behavior and need to execute code which is likely to be different, the component model cleans up a lot here but in general to make the system flexible, I'll need to have some kind of listeners to events.
For example, when CD code determines two thingies collided, it will issue a [font="Courier New"]Bump[/font] call, from now on, I have no guarantee what's going on. Sure, the call is now [font="Courier New"]Bump[/font], and not [font="Courier New"]Update[/font], and called from a more specific context, and thus hopefully not abused. Yet it likely involves updating something and this may have the same issues associated with [font="Courier New"]Update[/font], albeit on a smaller scale, or perhaps not? Am I missing something here? Perhaps I am thinking too generic?
When I wrote about constraints, I meant inter-object constraints. To satisfy those, I was planning to let the script provide the required "sync" code, or perhaps I should allow them to register "update groups"? I wonder if I need to keep track of update status on a per-object, per-tick basis.
After all, if the script can define a function, it can probably call it as well.


Go look at Unity3D. It does a very good job of this, and mirrors the same style I've seen in several professional game engines I've worked with.

You should never ever be in a position where "I have no guarantee what's going on". The person who wrote that system should absolutely know what's going on. Even on a huge project with hundreds of developers, you personally may not know what every system does, but when you get the right people from the right teams together you can know exactly what everything is doing.

Yes, it looks like you are being too generic. Game objects are put together through composition (even when they come through scripts) and properly designed composition can intelligently process events and prune them down. Each layer can select its subordinates that need the message or prune it out, relatively few items actually get processed during an update. If a function needs to be called it can register to be called, but that is something to avoid for the reasons mentioned above: Don't run code if you don't have to.

The constraints you mentioned are generally non-issues with that sort of system when it is designed well. Most notably they have decoupled rendering and processing: they don't update the 'live' variables, they are given a copy of the previous frame's values and the end result becomes the next frame's values. This can be made trivially parallel through proper design and composition, with each layer generating new tasks that are independent of the others. Tasks can be run in parallel or serial order with a proper design. Ordering of items such that object A must be updated before object B is an obvious design flaw; if you require additional ordering something is wrong. (Often you have a pre-update, update, and post-update cycle; similarly there is a pre-render, render, and post-render cycle. More than that indicates too much interdependence.)
To be completely honest, I was thinking about going for direct variable access for the time being as I do not plan to have any support for parallel processing mid-term.
I'm downloading Unity right now.

Previously "Krohm"

I've fiddled a bit with Unity today and while I still don't understand quite well how the system works in general, I think I am having a sufficient understanding of how the scripting is supposed to work. This is my biggest concern as I really don't want to have anything system-specific in source.

Everything in Unity is an [font="Courier New"]Object[/font]. It appears that object creation is tracked and drives appropriate behavior, very much like I'm doing for my "core classes". Their [font="Courier New"]Object [/font]is therefore much higher level than mine to start with. Interestingly, each [font="Courier New"]Object [/font]has a name, some editor-oriented data and the ability to be kept on level load. This is something that has kept me thinking for a while right now and I'll have to consider this simple solution as it really makes sense.

From there, the next step is the [font="Courier New"]GameObject[/font]. This is where things start to be really different from the approach I was thinking about: the [font="Courier New"]GameObject [/font]appears to contain an handle (possibly [font="Courier New"]null[/font]) for everything I might want to put into it. This in my opinion somewhat clashes with what we have discussed so far, to use a family of basic components instead of a "game entity", by defining a [font="Courier New"]GameObject [/font]that way, I cannot reasonably tell how this will be used. Nonetheless, routines in [font="Courier New"]GameObject [/font]might act differently according to what references are valid or not. [font="Courier New"]Component [/font]is a "Base class for everything attached to GameObjects".
I wonder why they did it that way.
By sure, I don't want to have a thing like that, with different management depending on the inner fields.
I was thinking about working more with composition, independently resolving events for each sub-object, then dispatching callbacks to those base objects, which would then forward them to the containing objects by using Java-like listeners.
I can see some advantages by considering the aggregated information emerging from component interactions, but I cannot tell if this translates in real world benefit without more experimentation.

[font="Courier New"]Behaviour[/font] is the same as component but can be disable ("Enabled Behaviours are Updated, disabled Behaviours are not." What about events?). From there, they derive [font="Courier New"]MonoBehaviour[/font], which defines a boatload of event listener calls which I'll have to consider. But, we also get [font="Courier New"]Update[/font], [font="Courier New"]FixedUpdate[/font] and [font="Courier New"]LateUpdate[/font]. Whoops. I was completely sold on that "no generic update/tick call" right now.

Summing up, I would say I don't see a "silver bullet" solution. My plan is therefore to just try an iteration and see what happens, hopefully just getting the rid of interface-inheritance should save me some problems although listeners would still be needed to connect the well-known component events to the object managing it. I'll need to consider the details.

Previously "Krohm"



Summing up, I would say I don't see a "silver bullet" solution. My plan is therefore to just try an iteration and see what happens, hopefully just getting the rid of interface-inheritance should save me some problems although listeners would still be needed to connect the well-known component events to the object managing it. I'll need to consider the details.


The missing piece is that you're still unable to differentiate between language-specific parts and the design.

C# simply requires certain constructs, such as inheritance from common class. The "many callbacks in one class" is also just because of language, it could be done differently.

Behaviour is the same as component but can be disable ("Enabled Behaviours are Updated, disabled Behaviours are not." What about events?). From there, they derive MonoBehaviour, which defines a boatload of event listener calls which I'll have to consider. But, we also get Update, FixedUpdate and LateUpdate. Whoops. I was completely sold on that "no generic update/tick call" right now.[/quote]
They are stages or phases. They work something like this:- ...
- update AI (for anything with AI component)
- update rigid body physics (for anything with rigid body)
- fixup (sometimes a constraint needs to be set after previous two phases)
- ...
- ...
The fact they derive from MonoBehavior or anything similar is, again, because of language.

Design benefit of the above is, if you have a Pot of Petunias that was intended as decoration, you can, at any point, just add AI component and it will have a deep conversation with a whale. With inheritance, you'd be stuck with StaticObject<-PotOfPetunias. To make it AI, you'd suddenly have to change it to inherit from Object<-Actor<-NPC<-PotOfPetunias. But oops, now it would automatically become RigidBody/QuestGiver/InventoryHolder/CombatTarget, because that is what an NPC is as well.

Component is a "Base class for everything attached to GameObjects".[/quote]
It's irrelevant tag required by language. Everything is a component, so it doesn't even need to be mentioned.

Type identity of a "Car" is just a bag of components, it is not "Car derives from Component derives from GameObject derives from Object". Car is a car with several properties. The properties define what "type" it is. If it has rigid body component, then it's part of physical world and can collide, crash, jump. If not, then it it's just decoration.
Something you may wish to look into is Component based entities/systems. Looking at my post history (I've not checked it in a year so I may be wrong) you can find several threads on this site that are great discussions on the subject.

Myself, I use entities as a compilation of components. Each entity is basically an array of components with functions to add/remove them easily. But it could be just as easily a struct (C) and an array, or a std::map<std::string, std::vector<ComponentBase*>> (Note: I use smart pointers but for ease of reading I will just use raw pointers here, and in this case its a weak pointer).

Then each Manager class (using manager here since its a common term, I am personally not a fan of it and in my code use the term System, but it's really just semantics) has a smart pointer array of all its appropriate components. Then each manager makes intelligent decisions on which component to update, etc.

We've been using this technique for a few years now and it allows us to rapidly prototype new projects in just hours verses days/weeks it used to take.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

The missing piece is that you're still unable to differentiate between language-specific parts and the design.
Admittedly, I am not. What I don't get is the following: couldn't they just do this?

// particularly lazy on public/private/protected, not being formal here
class Object { // Everything inherits from Object. Like in Java. Not really defined in code, provided by library.
String name;
// note this is potentially empty
};


// Base class for anything going through the physics system. Does nothing by itself.
// Again, this is provided by the library. Automatic "extends Object"
class Collidable {
class CollisionListener {
virtual void OnTouch(const Collidable &other) = 0;
};
CollisionListener *notify;
/* etc */
};


/* "Core class" with special management. Everything inheriting from there will be pooled by the physics management system
and updated accordingly. Inheriting from this class is highly discouraged but possible, as I don't have any way to prevent this.
This inherits from Collidable mostly because of syntactic sugar than anything else. */
class RigidBody extends Collidable { /* center of mass, transform... */ };


class SomeGameObject implements Collidable::CollisionListener { // auto inheritance from Object, multiple interface-only inheritance
RigidBody inner;
SomeGameObject() { inner.notify = this; }
void OnTouch(const Collidable &other) { /* not really relevant */ }
};


This way, I don't need to say much on inheritance (used for listeners, but not for object composition). Aggregating the behaviors happens by overriding and pooling the appropriate event calls. Each class has the chance to aggregate the objects as they want and I don't need any container struct. This way I never do this:

void MangleGameObject(Object &object) {
if(object.inventory) MangleInventory(object.inventory);
if(object.collidable) MangleCollidable(object.collidable);
if(object.visualization) MangleVis(object.visualization);
}

In general I can see this is probably going to be more manageable due to its centralized approach and allow higher tuning. I still cannot quite get in the line of thinking.
For example, why couldn't them just pull out the component they need to work on using for example [font="Courier New"]GetComponent [/font](which seems to do some reflective magic)?
Because that's what we are saying: a [font="Courier New"]GameObject [/font]might have a transform... or perhaps not. So why to put a transform variable there explicitly in the first place?
I suppose I am not well aware of the language limitations you're referring to. Why not to just let derived objects do whatever they want, declare whatever they want and then using listeners? Or inspeciton/reflection? You wrote exactly this: it could have been done differently. So, is there a reason to do it this way?

I also require inheritance for a common class because this is how the system pools the "core classes" up to now. If I instance a [font="Courier New"]DigitalButtonBinding("w")[/font], the system will silently create the special behavior associated to keyboard mangling. If some object wants to mangle keyboard it just has to [font="Courier New"]new [/font]it and listen/check its status. I don't need to put an handle in some base class to let this happen.

Thank you very much for the petunias example. It's good to have a "real" example to think at. I think there would be no problem using the above method, it seems to retain the benefits of component model. Sure it would be slightly more verbose because of the need to define ad-hoc listeners. At this point I cannot estimate if this is a problem for me.

They are stages or phases....The fact they derive from MonoBehavior or anything similar is, again, because of language.[/quote]Ok, so that would be the "Update by type" approach referred above. So what happens is that the objects will "evolve" naturally. Ok, but those [font="Courier New"]Update [/font]calls are here for something, to provide the logic that is non-core.
The point I was trying to make here is that: [font="Courier New"]Update [/font]exists. Why? Because events are dispatched in nontrivial order. Neither those calls. But they hold an interesting property: they are called before or after a certain phase. And they do what? They "propagate" the update somehow sure it's not called [font="Courier New"]Update [/font]but this takes me to the starting line (a) I don't see any way to avoid using Update, nor propagation.

Myself, I use entities as a compilation of components. Each entity is basically an array of components with functions to add/remove them easily.
...
Then each Manager class (using manager here since its a common term, I am personally not a fan of it and in my code use the term System, but it's really just semantics) has a smart pointer array of all its appropriate components. Then each manager makes intelligent decisions on which component to update, etc.
Thank you, this is exactly the point. If we put every component in an array of [font="'Courier New"]ComponentBase[/font]* nobody has knowledge of who will contain what until runtime. Which means I don't need to say "those objects might hold a camera... or perhaps not". I mean, it's a question of style here. If we can retain all the benefits of component models by ... not explicitly naming components at all, why to worry about each of them explicitly in the first place? Just let the user to declare the classes he wants, slapping in the components he wants and we are done.

But unity is good stuff and if they didn't do that way, I'd say there's a reason to. Which is the language-specific issue we are talking about? I don't know.

Is this correct?

Previously "Krohm"

I have taken a bit similar approach as Mike. My entity class is lightweight and has 5 pointers to different types of components: graphics, audio, physics, animation & controller (ai/script), i.e. one component for each type of sub-engine. I didn't want to generalize these components though to keep the design simple & efficient and to have custom class interface for each type of component. It's up to each sub-engine how they deal with these components as well, e.g. physics engine can also contain an internal list of simulated entities and efficiently update only physics components or entity properties as required by the simulation. The entity class is needed for composing different entities in a level from the components, so for example when an entity is deleted, it deletes its components, which in turn may inform the associated sub-engines about the deletion. Or in editor you can tweak properties of a component that's assigned to the entity.


Cheers, Jarkko
Logical game entities don't live in low level game code anymore, usually they've been moved over into some scripting framework. The entities inside the engine themselves are not much more than collections of interfaces into owned subsystems objects, centralized to simplify dispatching of events or methods. The more advance systems I've seen implement something like the COM protocol where they bind to objects interfaces, usually to get around the slow event dispatch problem. As for the update function, in modern game architectures the subsystems are responsible for updating their respective owned objects not the entry itself. This allows for better cache coherence and parallazation.

The game entity itself is still very pertinent but lives mostly inside its own special built framework, managed by a scripting core. All this of course is just generalization, plenty of exceptions and there is nothing stopping you from putting your game entity inside straight C++..


-ddn

This topic is closed to new replies.

Advertisement