Component Design in Component Based Game Engine

Started by
31 comments, last by phiwer 10 years, 12 months ago

I would argue you shouldn't have a ChildrenComponent, rather, the components that make it logical for an entity to have children should store the children in that component.

For example, a Car entity could have a CarComponent, and part of it's data is a list of entities riding in it.

An Army entity would have an Army component, and it would have the list of solider entities in the Army.

At least, that's how I'd do it.

Will

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Advertisement

I'm not sure if there's much benefit in having a general-purpose ChildComponent. As phil_t pointed out, there are a number of different types of parent-child relationships, and it's difficult to find a generalization of the logic and data required for each without dealing in the specifics of any particular type of relationship. If you do go down that route, I'd say you could store a parent ID at most, and then have utility methods to perform the typical get-children, get-siblings, get-parent, etc. operations. However anything beyond that and it would seem as though you're dealing in the particulars, i.e. does the parent "own" the children? Are they attached in a hierarchy? Are the children contained within the parent?

What you can probably do in lieu of having a special ChildComponent is create IChild and IParent interfaces that are implemented by the various components that need to use these relationships. Then you can build a set of utility methods (possibly a full system) that deals in these interfaces alone. Each component that implements these interfaces can do so in such a way that makes the most sense for that type of relationship, and they can have their own systems that augment the general-purpose interface with specific methods that make sense for their relationship.

Let's take the case of gold as a component. Say you have a world of entities, and these entities in turn have some components which contain lists of entities.

To make this concrete (although a bit contrived):

World Entity (has WorldComponent)

WorldComponent in turn contains a list of Village Entities (has VillageComponent).

WorldComponent also has a list of Player Entities (has PlayComponent) that are out roaming the world but are not in villages.

VillageComponent also contains a list of Players, but these are specifically in villages.

There is also a GoldComponent, which each player has. Each village also has a GoldComponent (which the players can rob or such).

Now say I have a GoldSubsystem and there is a function GetTotalGold, which is suppose to compute all the gold for a specific entity and its children. So if I input a village I get all the gold in the village + all the gold of all the players. Nothing is static here, so sometime in the future I may decide to remove the GoldComponent from the village, but then I should still get the total gold of all the players in the village.

In this design, there must be some way of iterating through entities that are contained within components, and to query these if they contain specific components. This suggests that there should be an interface on the components to support this behaviour. Or am I modeling this wrong?

Let's take the case of gold as a component. Say you have a world of entities, and these entities in turn have some components which contain lists of entities.

To make this concrete (although a bit contrived):

World Entity (has WorldComponent)

WorldComponent in turn contains a list of Village Entities (has VillageComponent).

WorldComponent also has a list of Player Entities (has PlayComponent) that are out roaming the world but are not in villages.

VillageComponent also contains a list of Players, but these are specifically in villages.

There is also a GoldComponent, which each player has. Each village also has a GoldComponent (which the players can rob or such).

Now say I have a GoldSubsystem and there is a function GetTotalGold, which is suppose to compute all the gold for a specific entity and its children. So if I input a village I get all the gold in the village + all the gold of all the players. Nothing is static here, so sometime in the future I may decide to remove the GoldComponent from the village, but then I should still get the total gold of all the players in the village.

In this design, there must be some way of iterating through entities that are contained within components, and to query these if they contain specific components. This suggests that there should be an interface on the components to support this behaviour. Or am I modeling this wrong?

Maybe there's a pure way around this, but, for simplistic purposes, wouldn't you have the ability to query an entity to see if it contains a certain type of component? And then be able to process that specific component from that specific entity? I would think you'd have to if you wanted to do something like what you suggest.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Just to clarify, components shouldn't "contain" entities in the traditional sense. All the entities in the game should exist together in a flat structure, and the components should only store IDs to entities (there was also a recent article series on decoupling that covered this subject in particular).

Now say I have a GoldSubsystem and there is a function GetTotalGold, which is suppose to compute all the gold for a specific entity and its children. So if I input a village I get all the gold in the village + all the gold of all the players. Nothing is static here, so sometime in the future I may decide to remove the GoldComponent from the village, but then I should still get the total gold of all the players in the village.

In this design, there must be some way of iterating through entities that are contained within components, and to query these if they contain specific components. This suggests that there should be an interface on the components to support this behaviour. Or am I modeling this wrong?

You have the right idea, we just need to have the interfaces in the right place.

For this example, you can have the WorldComponent implement the IParent interface (it can be the parent of both villages and players), the VillageComponent implement both the IParent and IChild interfaces (it can be both the child of the world and a parent of players), and the PlayerComponent implement the IChild interface (it can be the child of both the world and villages).

You then create a ParentChildSystem which acts only on the components that implement these interfaces. Given a root object (such as the world or a village), you find all components on that entity which implement IParent. At the point, you then find all components across all entities that implement IChild, and reference any of the root object's IParent IDs. You then find all the IParent's on this new set of entities, and the IChild's on other entities that reference them, etc. It's basically a breadth-first search. Once you have the list of entities, the GoldSystem can find all the GoldComponents and tally them up, without concerning itself with parent/child relationships or hierarchy (since that's the ParenChildSystem's job).

The biggest downside here is that the generalized interfaces, combined with a lack of forward references from parent to child, can lead to objects being included that you didn't want, and rather inefficient searches (note the "all components across all entities" part of the search). The best way to fix this is to simply add the child IDs directly to the IParent component, and have the system keep the IParent and the IChild components in sync, as phil_t mentioned a few posts ago. It's still a BFS, except now you're not doing searches across all entities and components. This doesn't necessarily fix the pruning problem, where objects you don't want are returned in the search. As previously mentioned, there are different types of parent/child relationships, and you might only care about specific ones when doing certain searches. For instance, when counting gold, you probably don't want to consider objects that are just physically attached to the player or village, but perhaps objects that are actually "owned" by them. In that case, the interfaces can also expose what kind of relationship they are, and the search can prune them based on that.

If you want, you can also dispense with the interfaces altogether and have the GoldSystem know precisely which component types (i.e. WorldComponent, VillageComponent, PlayerComponent) it needs to query, as well as their exact relationships. For a small game this is the easiest approach, except you might find that some code gets duplicated as multiple systems end up querying the same parent/child hierarchies, since there is no interface type upon which to build a generalized solution.

In this design, there must be some way of iterating through entities that are contained within components, and to query these if they contain specific components. This suggests that there should be an interface on the components to support this behaviour. Or am I modeling this wrong?

Assuming there can be multiple parent-child relationships, then it sounds like your GetTotalGold function would need specific knowledge of the Village component (otherwise how would it know in which component to look for children?). That's not necessarily bad, and the Village component can still implement a child-iteration-interface (assuming the only children of a village are players). GetTotalGold could leverage a helper function that enumerates children for a specific component (as Zipster alluded to).

It's hard to offer concrete suggestions without knowing the full details of the game. But hopefully an entity/component framework makes it easy for you to adapt to new needs as you develop your game.

Some food for thought: off the top of my head, another implementation that doesn't require any parent/child stuff would be:

- It seems like villages are first-class concepts in your game. So it might make sense to have a VillageComponent that can be attached to an entity and that indicates which village it is associated with (and you don't necessarily need the big hierarchy with village entities with components that specify child players and child buildings, etc...). If it has no VillageComponent, then its off on its own.

- In that case, my GetTotalGold for a particular village would enumerate all entities that have a GoldComponent and VillageComponent. For each one that matches the specified village, add the gold to the running total.

- GetTotalGold for a single entity would just involve looking up the value in its GoldComponent.

(So basically I'm questioning whether I really need a "generic" GetTotalGold function)

This is a more database-y approach rather than OOP-y (perhaps a better fit with entity/component frameworks).

Some food for thought: off the top of my head, another implementation that doesn't require any parent/child stuff would be:
- It seems like villages are first-class concepts in your game. So it might make sense to have a VillageComponent that can be attached to an entity and that indicates which village it is associated with (and you don't necessarily need the big hierarchy with village entities with components that specify child players and child buildings, etc...). If it has no VillageComponent, then its off on its own.
- In that case, my GetTotalGold for a particular village would enumerate all entities that have a GoldComponent and VillageComponent. For each one that matches the specified village, add the gold to the running total.
- GetTotalGold for a single entity would just involve looking up the value in its GoldComponent.
(So basically I'm questioning whether I really need a "generic" GetTotalGold function)

FWIW, I like this approach better than having specific interfaces or components for parents/children. I still think the components themselves should dictate whether they are parents or children.

And, to clarify, when i said "query an entity to see if it contains a certain type of component", I obviously didn't mean it holds it, but rather has some reference to another entity (via EntityId possibly).

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Some food for thought: off the top of my head, another implementation that doesn't require any parent/child stuff would be:

- It seems like villages are first-class concepts in your game. So it might make sense to have a VillageComponent that can be attached to an entity and that indicates which village it is associated with (and you don't necessarily need the big hierarchy with village entities with components that specify child players and child buildings, etc...). If it has no VillageComponent, then its off on its own.

- In that case, my GetTotalGold for a particular village would enumerate all entities that have a GoldComponent and VillageComponent. For each one that matches the specified village, add the gold to the running total.

- GetTotalGold for a single entity would just involve looking up the value in its GoldComponent.

(So basically I'm questioning whether I really need a "generic" GetTotalGold function)

This is a more database-y approach rather than OOP-y (perhaps a better fit with entity/component frameworks).

I like this approach as well, given that it "fits" entity/component better. I sometimes fall back into OOP thinking when modeling my entity/component system.

Would you share the same VillageComponent between Players and the actual Village entity, or would you suggest creating a different component with the sole task of explaining that a player is present in a village (e.g. PlayerInVillageComponent)?

Just to clarify, components shouldn't "contain" entities in the traditional sense. All the entities in the game should exist together in a flat structure, and the components should only store IDs to entities (there was also a recent article series on decoupling that covered this subject in particular).

Now say I have a GoldSubsystem and there is a function GetTotalGold, which is suppose to compute all the gold for a specific entity and its children. So if I input a village I get all the gold in the village + all the gold of all the players. Nothing is static here, so sometime in the future I may decide to remove the GoldComponent from the village, but then I should still get the total gold of all the players in the village.

In this design, there must be some way of iterating through entities that are contained within components, and to query these if they contain specific components. This suggests that there should be an interface on the components to support this behaviour. Or am I modeling this wrong?

You have the right idea, we just need to have the interfaces in the right place.

For this example, you can have the WorldComponent implement the IParent interface (it can be the parent of both villages and players), the VillageComponent implement both the IParent and IChild interfaces (it can be both the child of the world and a parent of players), and the PlayerComponent implement the IChild interface (it can be the child of both the world and villages).

You then create a ParentChildSystem which acts only on the components that implement these interfaces. Given a root object (such as the world or a village), you find all components on that entity which implement IParent. At the point, you then find all components across all entities that implement IChild, and reference any of the root object's IParent IDs. You then find all the IParent's on this new set of entities, and the IChild's on other entities that reference them, etc. It's basically a breadth-first search. Once you have the list of entities, the GoldSystem can find all the GoldComponents and tally them up, without concerning itself with parent/child relationships or hierarchy (since that's the ParenChildSystem's job).

The biggest downside here is that the generalized interfaces, combined with a lack of forward references from parent to child, can lead to objects being included that you didn't want, and rather inefficient searches (note the "all components across all entities" part of the search). The best way to fix this is to simply add the child IDs directly to the IParent component, and have the system keep the IParent and the IChild components in sync, as phil_t mentioned a few posts ago. It's still a BFS, except now you're not doing searches across all entities and components. This doesn't necessarily fix the pruning problem, where objects you don't want are returned in the search. As previously mentioned, there are different types of parent/child relationships, and you might only care about specific ones when doing certain searches. For instance, when counting gold, you probably don't want to consider objects that are just physically attached to the player or village, but perhaps objects that are actually "owned" by them. In that case, the interfaces can also expose what kind of relationship they are, and the search can prune them based on that.

If you want, you can also dispense with the interfaces altogether and have the GoldSystem know precisely which component types (i.e. WorldComponent, VillageComponent, PlayerComponent) it needs to query, as well as their exact relationships. For a small game this is the easiest approach, except you might find that some code gets duplicated as multiple systems end up querying the same parent/child hierarchies, since there is no interface type upon which to build a generalized solution.

Interesting articles!

And yes, searching in general when using an EC system seems to be one issue I will have to look into, if I want it to be handle many players. Thanks for the tips!

Would you share the same VillageComponent between Players and the actual Village entity, or would you suggest creating a different component with the sole task of explaining that a player is present in a village (e.g. PlayerInVillageComponent)?

I would question the need for a "Village entity" ;-). What would this entity exist for?

"task of explaining that a player is present in a village" -> I wouldn't make a different component for this. But I don't really know what this is. Is this a blob of text that pops up whenever a player enters a village?

This topic is closed to new replies.

Advertisement