Basic Component Based Entity

Started by
67 comments, last by crancran 12 years, 4 months ago
On 11/3/2011 at 5:02 PM, Madhed said:

Does any one know why the inheritance obsession became so widespread that the new term "component based entity" had to be invented?

It comes from object oriented design, under which you focus on defining classes to represent your different types of application-domain objects. When thinking in terms of object oriented design, you typically think about how different types of objects relate to each other - what attributes they share, and how they differ. And the basic strategy for sharing features is to define object types in terms of other (more basic/general) object types.

This approach worked very well to promote encapsulation and separation of concerns, when the objects, and the relationships between them, were relatively simple. For example, suppose you made a banking system and defined class hierarchies for accounts, and transactions. Those are relatively simple, conceptual objects, with only a handful of possible operations each, and not much variation in the different types of each.

Once you get into our type of real-time simulation world, with a wide variety of objects having arbitrary collections of a lot of very different types of features, it breaks down pretty quickly.

But let's not forget that once you use a component system to build your wide variety of entity types, there's still room to then create small hierarchies of closely related entity types. For instance, you could define a class to represent the skeleton MOB, pulling together all its requisite components. And you could define a handful of different skeleton variations which represent minor alterations like differing initialization values. True, this might not make it into the run-time, and might be happening in a tool, but the point is, even when using component based entities, inheritance isn't obsolete. We just have to use it where it's the best tool.

On 11/3/2011 at 5:02 PM, Madhed said:

I'm just wondering why there seem to be so many paradigm shifts in game development.

Because we've been making games a long time, and their complexity has been continuously changing. The problems with object oriented design are something we had to learn the hard way.

Advertisement
So my Transformation component (aka Spatial) right now has 4 properties. Naturally this component has two Vector3 coordinates to represent the position and scale. It also contains a quaternion representing the location's rotation in 3D space. The fourth property is my scene graph's node reference. First, is it typical that this type of component would hold a reference to a scene node? I can't rely on my or mesh render components if the entity is a sound and isn't visible, so this makes logical sense to me. Does it to others?

If that is the right approach, then I would expect my mesh component would simply create a child scene node off the node of the transformation and place my entity's mesh attached to that node. Similarly, if I attach a 3rd-person orbiting camera to my player because I he is what I what I want to focus on, then the camera component would chain child nodes off the transformation's scene node too.

The next step would be to consider input. I have a player input component that my input system works with. If I hit the "W" key, this gets mapped to a move forward situation by my player input component. I would think it would then make sense that the player input component would look up the transformation component and call a void setMoveForward(true) method. When the transformation subsystem iterates it's components, it detects the move forward flag, sets the appropriate speed calculation and adjusted the node's position accordingly. Is this a decent approach to this?
Great discussion you guys have got going here. I recently started looking into component based design as well, and the original variant I've seen doesn't really have subsystems; therefore, the logic seems to be in the components (more akin to the OP's first post). Is that still true of the subsystem variant? Or do the components purely contain data.

For instance, if I wanted to model chess, should the movement logic be in the movement subsystem, or in the components? I see pros and cons for both. If it's all in the subsystem, I imagine the subsystem could conceivably become large and unwieldy, perhaps with giant if then/switch ladders. But on the flip side it's all contained in one place. If the logic is all in the components, then each component is responsible for it's own movement type, but then the code is spread out....
On 11/3/2011 at 8:01 PM, crancran said:

...is it typical that this type of component would hold a reference to a scene node?

...[snip]...

I would think it would then make sense that the player input component would look up the transformation component and call a void setMoveForward(true) method.

These are tough to answer because there is no right approach. Personally, for instance, although I know that scene graphs can be used for other things, I prefer to keep it within the rendering engine, and not let most other systems/components know anything about it. Similarly, I wouldn't want a basic position tracking component/system to deal with locomotion or physics.

But that's just me - based on the degree of specialization and encapsulation I tend to think in terms of. You'd probably get a better answer from someone whose approach is more similar to your own.

On 11/6/2011 at 1:00 AM, moogleii said:

if I wanted to model chess, should the movement logic be in the movement subsystem, or in the components? I see pros and cons for both. If it's all in the subsystem, I imagine the subsystem could conceivably become large and unwieldy, perhaps with giant if then/switch ladders. But on the flip side it's all contained in one place. If the logic is all in the components, then each component is responsible for it's own movement type, but then the code is spread out....

We could be talking about the logic for determining legal moves for a given piece, or the code to actually move a piece to a target square. Either way, there's so little data involved in making decisions about how a piece moves that there's hardly any need for a component at all. The only data a component would hold is the type of piece, which would undoubtedly be redundant with information held elsewhere.

If the code were in the component, it might use a virtual function, or some other stored object/function pointer, to select the move code appropriate to the piece. If instead, the decision were handled by logic in a sub-system, it would likely be a single switch statement, dispatching to that same move code. Probably the most complex part of the whole movement system would be the way in which different types of movement might try to share functionality between them.

It seems no more complex to handle this in a sub-system than in a collection of movement component sub-types.

Hypotheticals aside...

On 11/6/2011 at 1:00 AM, moogleii said:

the logic seems to be in the components ... Is that still true of the subsystem variant? Or do the components purely contain data.

The idea is that each subsystem provides it's own functionality to entities, and the components are the way in which entities can elect to participate in that functionality. Code may still belong to components, and when it does, the idea is that the sub-system, rather than the entity, should run that code.

But that's just a general preference which has potential to lead to more efficient processing, and simpler deterministic processing order, which is not really the point of using components. The main point of using them is to facilitate the definition of a wide variety of entity types which can each utilize an arbitrary selection from a rich set of features.


[quote name='crancran' timestamp='1320375666' post='4880356']is it typical that this type of component would hold a reference to a scene node?
...[snip]...
I would think it would then make sense that the player input component would look up the transformation component and call a void setMoveForward(true) method.

These are tough to answer because there is no right approach. Personally, for instance, although I know that scene graphs can be used for other things, I prefer to keep it within the rendering engine, and not let most other systems/components know anything about it. Similarly, I wouldn't want a basic position tracking component/system to deal with locomotion or physics.

But that's just me - based on the degree of specialization and encapsulation I tend to think in terms of. You'd probably get a better answer from someone whose approach is more similar to your own.
[/quote]


Would you care to share how you would approach locomotion and physics? The entire input/movement/physics debate has been a focus for me these last few days as I have been trying to integrate Bullet. I have come to the conclusion that I my design for these aspects are not well thought out as I would have liked for them to work well together.

As for the scene graph, I would like to keep it self contained, but I honestly struggle with how I can do just that. A lot of it could just be that I have a very flawed way of approaching the design my components and how they integrate with the rendering engine itself. Lets take a 3rd person camera for example, one approach I read was to setup the scene graph so that both the camera and the avatar were both children of the same scene node (position). This way as you moved the avatar (position node), the camera would remain relative. This involved the camera component having 3 scene nodes chained together to deal with the camera's pitch, yaw, and roll separately. I suspect this is wrong and why I am finding a scene graph node pointer in several components.

Lets take a few non-visible elements for a moment. We have a 3d sound that is playing. Wouldn't there be a need to represent this with some form in the scene graph? What about triggers that upon being interacted with fire some logic? Maybe I have this notion of how I have seen other game editors layout all the entities and components in a view of a game map and I'm trying to tie the two together in my mind in a bad way.
I have been looking over my component structure the past few days and I have began to question my approach. In a number of places, I have always seen it iterated that there is this 1:1 relationship between a component and it's subsystem, but why so?

I have two components, a light source and a mesh. Both of these components make sense to be derived from a render component so they get child scene graph properties that all render components should have; however, the process to go about creating a light versus drawing a mesh are slightly different but the end result is the same and at this point I see nothing beyond the few lines of code of creating the render engine objects to be different between these two.

It would seem in this scenario I could benefit from placing logic inside the component itself either by passing the subsystem as a pointer to the component's update method or during the creation of the component so that it can either 1. perform the creation/update process internally based on it's state or 2. make a function call to the system where the create/update methods exist for lights and meshes.

I understand the benefit of having the subsystem as it relates to the main game loop itself; however I can see why having logic for creating/updating a component's state reside in the component itself, particularly where you may have many derived classes of a particular component type and rather than having separate systems, the same system could manage those grand children components so long as they adhere to a common interface but require either system method calls or to place the logic in the grand children components for state management.

How have others accomplished this?

EDIT:
To add to this, I am also working on the concept of input. I am using OIS (for those who may be familiar) and so my input system class has the necessary callback methods for receiving the buffered keyboard and mouse actions.

The goal is that these events need to be eventually routed to either the PlayerControllerComponent which allows me to control a player by certain actions, the CameraControllerComponent that allows me to control the camera by mouse rotation and button combinations, or to be routed to the GUI system.

This seems to be somewhat game specific in a way rather than generalized correct? What I mean is that the input system is responsible for collecting an input and dispatching it. At that point how it reaches the player controller or the camera controller components are dictated by game logic right, not at the game engine level? By that statement, a simple Game Controller class could listen to the event bus for these events and then drive dispatching the input to the GUI first, waiting for a response. If the GUI doesn't process it, then translate the event into a game key binding action and then dispatch the action to the event bus. This action is what the controllers would respond to. Is that how it typically is handled or a good way?

cc,

When I said,

On 11/3/2011 at 11:27 AM, vreality said:

If it doesn't make sense to create two subsystems, it probably doesn't make sense to create two component types.

I was only talking about figuring out how to split a single entity's data down into components. I agree with your analysis completely. I see no reason that a single sub-system can't handle multiple component types. A physics system might handle AABB components, and spherical components, and rag-doll skeletal components, etc. And I see no reason that code specific to those component types can't reside in the component objects.

As for input, you'll generally want an abstraction to map input control states and events to game specific input "signals" (e.g., Move_Forward = W is_down or UP_Arrow is_down, Fire1 = Right_Trigger on_press, etc.). On top of that you'll generally need some sort of scheme to group bunches of signals together so you can activate/deactivate them, prioritize them, override them, etc.

I like a system in which entities/components which are interested in input can poll for a given signal, so code that cares about the input gets it when and where it needs it.

So I've been working on a simple component based entity system so I can create entities on the fly without have to have extreme levels of inheritance.[/quote]

I'm not sure anyone can write a simple Component-based Entity System. Firstly, there's no standard definition for a component or entity. Secondly, there's a massive amount of grey area between Component communication which also appears to be the most complex aspect behind the approach. Thirdly, its not a programming paradigm in itself and trying to treat it as such complicates matters. Perhaps a Component-Oriented Programming Language would be cool, however, as it stands its just a architectural pattern applicable to high-level modular game entities - imo.

Components are cool for Game Entities. I found myself evolving naturally towards Component-based Entities in writing my umpteenth iteration of a 3D GUI which was the highlight for my Game Engine. In fact, I thought I came up with some revolutionary approach to designing GUIs at the time, only to discover after reading ~20 Threads on GameDev.net and some popular Articles on the net the approach has been adopted by Pros for years :angry:.

With my new found knowledge, I ditched my previous Game Engine design and started building the all new Entity Component-Objects design that adheres to two concepts:

  1. An Entity is a container defined by its Components!
  2. Components are Special Purpose Objects that belong to the Game Engine's Subsystems: Rendering, Physics, Audio, Scripting, Input, Networking, Solvers, etc.

Of course, I have my own definition of components/entities and my own communications system implemented with techniques derived from my previous GUI inter-communication and data modeling. But, I think if you hit the two concepts you're doing good for a basic component system and I'm no expert on the subject as I find myself constantly tweaking the system.

@crancran, I noticed that you're using Ogre and many of the addons that I'm using for my game engine. Would you be interested in comparing notes?

I'm not sure anyone can write a simple Component-based Entity System. Firstly, there's no standard definition for a component or entity. Secondly, there's a massive amount of grey area between Component communication which also appears to be the most complex aspect behind the approach. Thirdly, its not a programming paradigm in itself and trying to treat it as such complicates matters. Perhaps a Component-Oriented Programming Language would be cool, however, as it stands its just a architectural pattern applicable to high-level modular game entities - imo.


I believe CBE systems are at the core simple, but you have to simply wrap your head around how to approach the project with this design model in mind. A lot of times, some of the biggest hurdles developers face is to what degree do we split functionality into components and how do the various parts of this approach communicate, as you indicated.

I came to the conclusion after reading so many articles, forum threads, and asking others about how they approached a problem that there is absolutely no RIGHT way to use this pattern. I've seen effective uses of this pattern but trying to compare the approaches were likely comparing apples to oranges. The neat thing about so many different approaches is that it gives a fresh new outlook on the pattern every time I find a new way someone approached something, which often takes sheds light on my own hurdles.

Communication for me has probably been one of the hardest aspects of using this pattern. I am still by no means comfortable with my current approach but its a crude, elementary, but working solution that I look at being something I can tune and refine at a later point.



@crancran, I noticed that you're using Ogre and many of the addons that I'm using for my game engine. Would you be interested in comparing notes?


Sure thing, I'll send ya a note with contact details.
CC

This topic is closed to new replies.

Advertisement