Jump to content

  • Log In with Google      Sign In   
  • Create Account

Basic Component Based Entity


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
68 replies to this topic

#61 crancran   Members   -  Reputation: 416

Like
0Likes
Like

Posted 17 November 2011 - 09:48 AM

So you seem to have kind of a game view system going on then don't you?
So if I'm understanding right your animation system isn't in a component then? For my purposes I'm thinking of making it a component that just listens to events from it's ID.


I can't say whether my design is solid or not yet, but I like the approach at least. I began under the premise that my components are nothing more than data bags, glorified structures called classes that hold state information for a particular component. They hold pointers to allocated resources, etc but there isn't any logic within them at all. All the logic and behavior has been offloaded into a system class that oversees all components of a particular class. In the component's world, all it has is an entity id (a number) but it has no idea what it means, just it needs to hold onto it. The component system doesn't know anything about the outside world except for the event dispatcher. It works under the premise that I send events and either I get an answer or I don't (in the case of callbacks) or I send events and forget it and move to the next task. Beyond that, a component system and its components work in their own little area of space without a care of what else is happening in the system.

In my system, when events are fired, they have an event id and most events typically have an entity id property or a target/source entity id when we talk about cross-entity interaction. The component systems register with the dispatcher upon start-up and specify what event types they are interested in as well as registering their respective callback handler methods for each event type. When an event is dispatched by the dispatcher, that particular function pointer is invoked in the subsystem. That subsystem statically casts the event object to the appropriate type and then performs the necessary logic that method was designed to do; namely look-up an entity by id and change properties, set dirty flag for state changes so it gets picked up on next game loop, then ends. In short, the components are never accessed externally, only via proxy through events and the component system that knows how to handle those events.

Also what is a good way to manage the event objects produced? They aren't pointers I make them like this:

 	Event event;
        event.type=E_CREDITSSTATE;
        event.category=E_GAME;
        CEventDispatcher::inst()->sendEvent(event);

That sends to the stateManager (who is registered to listen to game events) an event from the menu state to switch to the credit state. Do I need to delete the event after it gets used or will it go out of scope and disappear itself?

The CEventDispatcher calls all of the IEventHandlers->handleEvent(e) functions in the registration list for that particular category. It's up to the handlers to decide if they ignore the event or not.


Before I get to the event deletion question, the common event dispatch handler method approach above, I personally dislike. It is an easy design pattern none the less and will work, but it typically leads to gigantic switch statements based on some data element that determines how to cast the common event object to a particular type. As I said, it works but not an approach I prefer to support because those methods can get lengthy when you have classes that listen to many events. I tend to use function pointer callbacks instead giving me the flexibility to to know when the function pointer is called that I need to take that base class pointer that I got and statically cast it to event type ABC. It avoids the switch statement concept and (although i haven't ran benchmarks personally), I think the virtual callback is a little less performing than a direct function pointer call. I would certainly consider looking into function pointers for a future pass, but YMMV.

Now for the deleting of events, it depends upon your design; whether your event is immediately dispatched or whether it is queued for dispatch at the next game loop or a future game loop. I use two different methods in my engine to distinguish between events that are to be QUEUED vs SENT/NOTIFY.

The notify() method in my case simply takes the event reference, looks up a function pointer and passes the reference to the function. The function can modify the event parameters as it sees fit and return. The next line in the code that called notify() can inspect the event and maybe get some information that was added by the prior callback; think of it as a glorified callback with return data :P.

When I invoke the queue() method, I pass the event information the same way as I do to the notify() method; however, the dispatcher makes a copy of the event and stores the copy. The dispatcher does not care whether what I pass is a heap allocated object or a stack allocated object because internally the queue() method always makes a copy and places it's own allocated object into its internal list for dispatch on the next game loop. This way if I passed a pointer into the dispatcher, that pointer is still my responsibility to delete and manage, not the dispatcher. When the dispatcher finally sends that queued event in the future, that allocated object is what the dispatcher is responsible for deleting when it is no longer needed. There are probably other ways to do this that are better, maybe using some form of smart pointers where once the reference reaches 0; it gets deleted automatically, but I'm not at that optimization stage. The point though is who ever creates a pointer should be the one who deletes the pointer. One of the hardest things to manage is who OWNS a pointer :P.

Sponsor:

#62 jaeg   Members   -  Reputation: 165

Like
0Likes
Like

Posted 17 November 2011 - 10:07 AM

So if I'm reading right your actual system is updating the information based on the component data rather than having your component have an update function and it does it's task?

So for example a collision system just pulls the data from the current component it's looking at and updates the data based on that itself? I can see that working for some of the components but what about a more complex component like AI? In my system I have two AI like components. AI and Behavior. Behvavior runs a simple script while AI deals with more complicated inputs and outputs.



EDIT:

I found an article that I think is explaining what you are talking about correct?
Entity Systems

The point your making is about halfway down the page under "Almost"

#63 crancran   Members   -  Reputation: 416

Like
0Likes
Like

Posted 17 November 2011 - 10:56 AM

So if I'm reading right your actual system is updating the information based on the component data rather than having your component have an update function and it does it's task?

So for example a collision system just pulls the data from the current component it's looking at and updates the data based on that itself? I can see that working for some of the components but what about a more complex component like AI? In my system I have two AI like components. AI and Behavior. Behvavior runs a simple script while AI deals with more complicated inputs and outputs.


That is correct, the update is at the system level, not at the component level. If you think about it at a higher level, when you iterate over your entities and call the component's respective update() method, you're invoking behavior based on that component's existing state. Inevitability, it is no different except we've factored the logic out of the component itself ad into a subsystem that is responsible not only for the logic behind the component it manages, but also the life cycle of that component by creating and destroying it as requested and communicating with the outside world or in my case the event dispatcher.

For things like AI and behavioral things like scripts, there is nothing that says that the component system's update loop can not iterate over the components and call a method such as update on each component too :P. The prime reason for these component systems and isolating logic in the system was to provide uniformity among components, keeping core code in single places and treating the components themselves as data. There will always be exceptions as you point out with behavioral scripts for sure.

When it comes to AI however, I tend to think that AI can be broken out into various components and systems such as:

- Randomly greet a player that walks by
- Pick a random phrase and yell on a specified interval or based upon an event trigger (similar to above)
- Path navigation (pick the next point on a path and walk to that point)
- Threat / Aggressive (if an entity that is an enemy is within X distance of the current entity, latch on to first one and be aggressive)

These are very common AI features that most AI entities will inherit. They may not inherit all of them, but there will be a core set of functionality that will be shared by a vast many and by separating it out like that, I can easily add it or take it away at will and the subsystem that runs an update() simply takes the current component, checks it's state and then applies whatever algorithm the system is designed to execute (navigate to next path after X seconds - pathing). The more complex things like handling what happens in an encounter with attacks, etc could all be driven by a script component that has an internal state machine or behavior tree attached that dictate fight mechanics.

This is my 100000 ft view on where I plan to head, but not sure whether that is right :P. I'm still working on basics :).

#64 jaeg   Members   -  Reputation: 165

Like
0Likes
Like

Posted 17 November 2011 - 11:28 AM

This is my 100000 ft view on where I plan to head, but not sure whether that is right :P

. I'm still working on basics

:)

.


Same here. As I work on it I find that certain things don't work (problem I'm having right now) and that certain things do. Also that certain things work better with others. Coding isn't hard it's designing it first. The nice thing though is that I have it set up in a way that I could complete remove the entity system without having to rewrite other parts (stateManager, game) since they are independent of each other. I know that isn't impressive but it's a first for me to design things modularly like this. lol

I need to sit down list what I need in the game how things need to work based on the players view and from there figure out how it needs to work behind the scenes. I also think I'm going to drop CopperCube/irrEdit and make my own editor so that I can decide what components are applied to what and have it generate the .init files for me. But first things first I need to figure out what components I need.

Like for example:
A playable entity needs a component for: Spatial, Render, Animation, Collision, Controllable, Equiping.
A weapon would need: Spatial, Render, Animation, Behavior, and Equipable.
AI NPC (Zombie): Spatial, Render, Animation, Collision, Sight, Hearing, AI Logic.

And if I'm thinking right it should be created and updated in the order I listed the components.

Now for the components Equiping and Equipable:
My thinking is that since every weapon has it's own behavior and other attributes making it able to attach and receive input via a entity with an Equiping component which receives controlling events will let me be able to use it easier.

#65 crancran   Members   -  Reputation: 416

Like
0Likes
Like

Posted 17 November 2011 - 12:41 PM

As I work on it I find that certain things don't work (problem I'm having right now) and that certain things do. Also that certain things work better with others. Coding isn't hard it's designing it first. The nice thing though is that I have it set up in a way that I could complete remove the entity system without having to rewrite other parts (stateManager, game) since they are independent of each other. I know that isn't impressive but it's a first for me to design things modularly like this. lol


That's the beauty of using the dispatcher concept right now. If I find I want to try something new, I can easily plug in a new component & system to replace the old one so long as it registers and listens/sends the same events as the other one did then everyone stays in harmony :P.

Like for example:
A playable entity needs a component for: Spatial, Render, Animation, Collision, Controllable, Equiping.
A weapon would need: Spatial, Render, Animation, Behavior, and Equipable.
AI NPC (Zombie): Spatial, Render, Animation, Collision, Sight, Hearing, AI Logic.

And if I'm thinking right it should be created and updated in the order I listed the components.


This is one area that I still continue to struggle with because for one I don't want to limit the editor or the designer for that matter to make sure that component A is added before component B. If I need to impose those rules, somehow I have to build a rules system into the plan to control making sure that when components C, A, and B are added to an entity, that before the editor window is updated, the components are re-arranged to A, B, and C in the editor tree so that they are then instantiated in the proper order for rendering. Unfortunately, I don't know if this is how other games are designed so that the game itself does not need to care about the load order as the tools handle that offline. And while a rules system can be imposed, then what happens when your game dynamically adds/removes components from an entity. If the code adds them out of order, then I may get weird results. I'd rather (personally) not have order requirements, but I can see why it helps to have it because now I don't have to necessarily test for that case in the game loop itself. How have others tackled this?

Now for the components Equiping and Equipable:
My thinking is that since every weapon has it's own behavior and other attributes making it able to attach and receive input via a entity with an Equiping component which receives controlling events will let me be able to use it easier.


That sounds about right - at least in my mind too. The way I would see that playing out is that the player selects an ability on their bar. The event is dispatched and the attack subsystem receives the event, determines that the spell used is fire the gun. The attack subsystem dispatches a fire ranged weapon which the weapon subsystem receives. The weapon subsystem inspects what ranged weapon is associated to the entity at that point in time, checks the tag associated with the component as to whether it is a wand, gun, etc. In our case it is a gun, so an event dispatches to play a gun sound and a particle emitter event is dispatched to show bullets leaving the end of the gun.

Thinking about this; it sounds overly complex, so I'm open to input :P.

#66 jaeg   Members   -  Reputation: 165

Like
0Likes
Like

Posted 17 November 2011 - 02:38 PM

I'll probably make two tools. An entity generator and the level editor.In my system the blueprints for entities are stored in .init files that are parsed by the entityFactory creating the components in the correct order. So before actually constructing a level in an editor I would go through and create the entities I.E. enemy types, pick-ups, weapons, and other dynamic stuff. After I have designed them for their purpose the generator goes through and using it's set of rules makes sure the components are listed in the correct order. Note that the generator does not need to use the components so it doesn't have to have them added to it in the correct order either. (I hope that made sense)

The level editor on startup will look through the "scripts/Init" folder find the entities available to it and allow you to place them in the level.

As of right now I don't need to come up with new entity types on the fly I just plan to use the ones I have planned out ahead of time. It keeps things simple for now.


I've been going through and writing notes and drawing diagrams using the systems as the updater and the components as data-bags and I'm seeing why you do it. It's easier to communicate data between components via the systems. Same with system to system.


To me it seems like multiple systems with dependencies on each other should be considered as units especially ones that function together to form AI. So those components need to be made together anyway so the dependency shouldn't really be too worried about. Now using AI as an example again since not all entities need to hear or see rules can be made to exclude those from the system that stores the action queue or makes decisions based on those two's input.

#67 crancran   Members   -  Reputation: 416

Like
0Likes
Like

Posted 17 November 2011 - 03:13 PM

I'll probably make two tools. An entity generator and the level editor.In my system the blueprints for entities are stored in .init files that are parsed by the entityFactory creating the components in the correct order. So before actually constructing a level in an editor I would go through and create the entities I.E. enemy types, pick-ups, weapons, and other dynamic stuff. After I have designed them for their purpose the generator goes through and using it's set of rules makes sure the components are listed in the correct order. Note that the generator does not need to use the components so it doesn't have to have them added to it in the correct order either. (I hope that made sense)

The level editor on startup will look through the "scripts/Init" folder find the entities available to it and allow you to place them in the level.
As of right now I don't need to come up with new entity types on the fly I just plan to use the ones I have planned out ahead of time. It keeps things simple for now.


It does make sense from the perspective of keeping things simple, but I decided to take a different stance when I started. With the editor in mind, I will be able to construct my terrain, shape it as I wish. Additionally, the editor will allow me to place an entity (broad-term) anywhere on the scene. The entity being placed can be selected in two ways 1) pre-fabricated or 2) dynamically. The user will be able to create a monster by simply selecting and dragging components to the entity container, setting values and moving the entity in the scene where they want it to be placed. They'll be able to script in behavior or add AI elements by specifying path points, etc; all because of the components that a selected entity has. The users can create a silly entity and if they like what they have, they can easily with a simple click copy an entity to a pre-fabricated template. Later when they move on to another area, they can grab that entity from the pre-frabricated list instead and simply tweak parameters as they need. It's a big project no doubt, but that's the goal and with that in mind, I am of the opinion that entity components are somewhat dynamic. It certainly adds a bigger element of complexity :(.

I've been going through and writing notes and drawing diagrams using the systems as the updater and the components as data-bags and I'm seeing why you do it. It's easier to communicate data between components via the systems. Same with system to system.


One current debate I have with all this is where to draw the line, particularly where it surrounds the render engine's components, such as lights, meshes, planes, and movable text, etc. Most of these objects require access to the scene manager for their respective render engine equivalent to be created. One way I considered is that all these "specialized subsystems" are child component systems of the render system. So when the render engine starts, there are about 10 different subsystems that start up with it. These subsystems handle various render-component types. Another alternative is that I have 1 system that manages all these components in various lists. The downside here though is I end up with a laundry list of logic methods in this one system for various component types; albeit they all depend heavily on the OGRE render engine to work. I just can't seem to put my finger on a good design for these types of components.

To me it seems like multiple systems with dependencies on each other should be considered as units especially ones that function together to form AI. So those components need to be made together anyway so the dependency shouldn't really be too worried about. Now using AI as an example again since not all entities need to hear or see rules can be made to exclude those from the system that stores the action queue or makes decisions based on those two's input.



When you say units, are you implying that you have like a master AI system and then child AI subsystems? Then those child AI systems can request from the parent AI subsystem pointers to the various AI child systems to communicate back and forth and work with other various AI components without using the event dispatcher? As for your comment about see/hear rules, I don't follow.

#68 jaeg   Members   -  Reputation: 165

Like
0Likes
Like

Posted 17 November 2011 - 03:33 PM

When you say units, are you implying that you have like a master AI system and then child AI subsystems? Then those child AI systems can request from the parent AI subsystem pointers to the various AI child systems to communicate back and forth and work with other various AI components without using the event dispatcher? As for your comment about see/hear rules, I don't follow.

I'm thinking my AI system is more complicated than yours. Rather than just one part running scripts there are other systems for things like sensory input. These systems watch for things like sounds to play or check via rays if the entity can see another entity. These will notify the decision making system which looks at the data and decides what to do. Using this I can effectively turn off a monster's ability to hear and it would function as if it never had it. Some with vision. I could turn off it's ability to receive a list of objects it can see making it have to solely by hearing find a target.

The ai has a set of targets kept in a list and when it gets it's vision feedback it'll compare those objects to the target objects and then take the right actions to try to kill the player.

So in short all of these systems make up the AI system as a unit. If one system doesn't exist the unit needs rules to make sure it can still operate.

Best example of what I'm doing:
Left 4 dead AI


It does make sense from the perspective of keeping things simple, but I decided to take a different stance when I started. With the editor in mind, I will be able to construct my terrain, shape it as I wish. Additionally, the editor will allow me to place an entity (broad-term) anywhere on the scene. The entity being placed can be selected in two ways 1) pre-fabricated or 2) dynamically. The user will be able to create a monster by simply selecting and dragging components to the entity container, setting values and moving the entity in the scene where they want it to be placed. They'll be able to script in behavior or add AI elements by specifying path points, etc; all because of the components that a selected entity has. The users can create a silly entity and if they like what they have, they can easily with a simple click copy an entity to a pre-fabricated template. Later when they move on to another area, they can grab that entity from the pre-frabricated list instead and simply tweak parameters as they need. It's a big project no doubt, but that's the goal and with that in mind, I am of the opinion that entity components are somewhat dynamic. It certainly adds a bigger element of complexity :(

.


This is where considering certain parts as units would come in handy. If you give them the option of choosing what input the ai should receive so when the decision making part of the AI unit does it's thing it'll look to see if it has the ability to hear or see.

One current debate I have with all this is where to draw the line, particularly where it surrounds the render engine's components, such as lights, meshes, planes, and movable text, etc. Most of these objects require access to the scene manager for their respective render engine equivalent to be created. One way I considered is that all these "specialized subsystems" are child component systems of the render system. So when the render engine starts, there are about 10 different subsystems that start up with it. These subsystems handle various render-component types. Another alternative is that I have 1 system that manages all these components in various lists. The downside here though is I end up with a laundry list of logic methods in this one system for various component types; albeit they all depend heavily on the OGRE render engine to work. I just can't seem to put my finger on a good design for these types of components.


I'm not sure about OGRE but in Irrlicht things like text is handled by the GUIEnvironment. So maybe have that as either a separate system.

A light that can change it's state should be an entity and in Irrlicht I think is still considered a scene node so the renderComponent which stores said node can still hold a reference to it.

#69 crancran   Members   -  Reputation: 416

Like
0Likes
Like

Posted 23 November 2011 - 10:11 AM

I have come across a situation where I need to be able to control multiple entities as though they're a single cohesive unit. For example, if entity A moves forward by 5 units, then entity B should do the same despite the fact they're both different entities.

A lot of examples I have encountered tend to have events dispatched to the entity class which in turn iterates it's components and notifies each component of the incoming event. For one case, the programmer implemented a "link component" that allowed them to add the child entity ids for the followers and then added the link to the parent. As events were dispatched to the parent, the entity event handler invoked each component's event handler. The link component changed the destination entity id on the message and then dispatched it again for immediate delivery, effectively relaying the event to another entity with ease.

In my case, I am using the outboard component system approach. For those unfamiliar, there is no concept of an entity class, just the entity manager knows that a particular guid has been allocated but it is oblivious to the components that the guid has too. All component information is held within the particular component systems themselves for the guid. Additionally, each component system registers to have specific events fired to callback methods. So for example, PlaySoundEvent is fired and only the sound system cares about this and the method OnPlaySound(CEventPtr evt) is what gets invoked automatically. The component system extracts the entity guid from the event, looks up the component and performs whatever logic is needed.

Operating under the outboard scenario, there really isn't a way to handle this "link component" behavior, at least not in an obvious fashion as how the above programmer did. How have others handled this situation? I could reintroduce an entity class but I'd rather see if there is an alternative option that I am missing.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS