Component Design in Component Based Game Engine

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

I'm developing a turn-based strategy game using a component based game engine and have been wondering how to structure my components.

There are quite a lot of collection components in my game. E.g. an Army contains many units. Is it suitable to have an ArmyComponent containing many UnitCpmponents? Is that a suitable level of abstraction for a component?

Another thing I've been considering is the level of abstraction for the buildings I'll have in the game and also connected properties. A player can create a farm, which produces food. A player can also create a barrack to construct units, but this building also increases the offense points of troops by 10%. These types of properties can obviously change (although not likely that any other building than a farm will produce food), and as such this should be customizable easily. My question is what level of abstraction the components should be? Should there be a BuildingComponent, ProductionComponent, and ResourceComponent? Or should there be a FarmComponent, ProductionComponent and FoodComponent? Am I making my components too granular?

I obviously want to create a flexible system, but I don't want to over engineer either. Most buildings will be static, while their properties will change. That's why I'd like to hear your opinion of how you'd structure these relationships using a component based engine.

One more thing, and this question is about sub systems. I intend on making the sub systems handle the logic, and only have the components store data. Does anyone have some tips & tricks regarding how to think about sub systems? I've read that one should aim for a 1:1 relationship between components/sub systems, I am not sure this would be the case for me given my current setup with regards to my granularity of my components.

Advertisement

There are quite a lot of collection components in my game. E.g. an Army contains many units. Is it suitable to have an ArmyComponent containing many UnitCpmponents? Is that a suitable level of abstraction for a component?

A unit may logically belong to an army, but that doesn't mean that a unit object needs to literally be contained by an army object. Components on components is just asking for trouble anyway. A better approach would be to either have an ArmyID property on your UnitComponent, that indicates which army it belongs to, or have a separate ArmyUnitComponent altogether which contains the ArmyID. In either case, units don't necessarily have to belong to an army if the ArmyID is invalid (or the ArmyUnitComponent isn't present in the latter case).

Another thing I've been considering is the level of abstraction for the buildings I'll have in the game and also connected properties. A player can create a farm, which produces food. A player can also create a barrack to construct units, but this building also increases the offense points of troops by 10%. These types of properties can obviously change (although not likely that any other building than a farm will produce food), and as such this should be customizable easily. My question is what level of abstraction the components should be? Should there be a BuildingComponent, ProductionComponent, and ResourceComponent? Or should there be a FarmComponent, ProductionComponent and FoodComponent? Am I making my components too granular?

This is just my opinion, but I would probably design the components around how they affect the gameplay as a whole, rather than the specific building or object to which they are tied. For instance, a farm may produce food, but what if you eventually want to have some other object that produces food? Instead of creating a FarmComponent and then trying to adapt or pigeonhole that into some other object later on, just create a ProducesFoodComponent (not a great name but you get the idea) that you can add or remove to just about anything in order to increase food production in your game system by +X%. Same thing with unit offense and production. You get the most flexibility by designing components so that they can be added to just about anything and behave as you expect they would, while understanding that might not always be possible.

One more thing, and this question is about sub systems. I intend on making the sub systems handle the logic, and only have the components store data. Does anyone have some tips & tricks regarding how to think about sub systems? I've read that one should aim for a 1:1 relationship between components/sub systems, I am not sure this would be the case for me given my current setup with regards to my granularity of my components.

It isn't a hard and fast rule that there be a 1:1 correlation between components and systems. That just tends to be close to what happens when you move your logic out of the components. However it's entirely reasonable for components to simply be tags, in which case they wouldn't have any logic of their own, or perhaps for multiple systems to interact with the same component (however in that case you'd want to question whether or not that component can be split apart).

There are quite a lot of collection components in my game. E.g. an Army contains many units. Is it suitable to have an ArmyComponent containing many UnitCpmponents? Is that a suitable level of abstraction for a component?

A unit may logically belong to an army, but that doesn't mean that a unit object needs to literally be contained by an army object. Components on components is just asking for trouble anyway. A better approach would be to either have an ArmyID property on your UnitComponent, that indicates which army it belongs to, or have a separate ArmyUnitComponent altogether which contains the ArmyID. In either case, units don't necessarily have to belong to an army if the ArmyID is invalid (or the ArmyUnitComponent isn't present in the latter case).

Am I trying to hard to create a classic domain model? Are there any "best practises" with regards to e.g. collections when using a component based engine? Is it more common to see denormalized data structures in component based engines?

Another thing I've been considering is the level of abstraction for the buildings I'll have in the game and also connected properties. A player can create a farm, which produces food. A player can also create a barrack to construct units, but this building also increases the offense points of troops by 10%. These types of properties can obviously change (although not likely that any other building than a farm will produce food), and as such this should be customizable easily. My question is what level of abstraction the components should be? Should there be a BuildingComponent, ProductionComponent, and ResourceComponent? Or should there be a FarmComponent, ProductionComponent and FoodComponent? Am I making my components too granular?

This is just my opinion, but I would probably design the components around how they affect the gameplay as a whole, rather than the specific building or object to which they are tied. For instance, a farm may produce food, but what if you eventually want to have some other object that produces food? Instead of creating a FarmComponent and then trying to adapt or pigeonhole that into some other object later on, just create a ProducesFoodComponent (not a great name but you get the idea) that you can add or remove to just about anything in order to increase food production in your game system by +X%. Same thing with unit offense and production. You get the most flexibility by designing components so that they can be added to just about anything and behave as you expect they would, while understanding that might not always be possible.

In this case, would building be an entity connected to a player via a PlayerBuildingComponent (or such)? I'm having difficulty actually separating entities and components at times, usually when relationships are involved (which I have quite many of).

Concrete example of how it works in my game: Say a player has x farms. There is no interest for the player to see these farms as unique, so I would group them into some data structure which says: BuildingType: Farm, Amount: x. But then behaviour needs to be added to this entity (or component, depending on your answer to the above question), so we add ProducesFoodComponent to this entity. Am I on the right track with regards to making the Building an entity in this case, and then linking this entity building to my player entity via a PlayerBuilding Component on either the Player or Building entity? If I don't do separate the building data structure into an entity, I'll get components having children of components, which does not seem right.

One more thing, and this question is about sub systems. I intend on making the sub systems handle the logic, and only have the components store data. Does anyone have some tips & tricks regarding how to think about sub systems? I've read that one should aim for a 1:1 relationship between components/sub systems, I am not sure this would be the case for me given my current setup with regards to my granularity of my components.

It isn't a hard and fast rule that there be a 1:1 correlation between components and systems. That just tends to be close to what happens when you move your logic out of the components. However it's entirely reasonable for components to simply be tags, in which case they wouldn't have any logic of their own, or perhaps for multiple systems to interact with the same component (however in that case you'd want to question whether or not that component can be split apart).

Ok. I'll think some more about subsystems and see if I come up with any more questions regarding this issue.

Thanks for the answers so far. Really helpful having someone to discuss these issues with!

Am I trying to hard to create a classic domain model? Are there any "best practises" with regards to e.g. collections when using a component based engine? Is it more common to see denormalized data structures in component based engines?

I'm not sure if there's an established set of best practices, because even now there are a lot of different interpretations and implementations of the "component-based" model. In the interest of full disclosure, for a little over a year I was doing some very heavy database work as part of a project at work, and really grew to love relational data models and the separation of data and logic, so obviously this influenced what I believe to be one of the better implementations of a component-based design. In the case of collections, a collection isn't so much a literal object that contains other objects, as much as it is a result set of a particular query, namely one where you retrieve all objects with CollectionID = 'x' (or in our case, ArmyID = 'x'). The possible values of 'x', or rather the set of army ID's, can either be inferred from the distinct set of army ID's present across all UnitComponent's, or be stored on entities that contain an ArmyComponent. In the latter case you establish an informal constraint on army ID's, and the notion of "valid" versus "invalid" ID's, but whether or not you need that type of constraint is up to you. Usually you'd only have the ArmyComponent if an army needed additional information, and wasn't just a logical grouping or joining of units.

In this case, would building be an entity connected to a player via a PlayerBuildingComponent (or such)? I'm having difficulty actually separating entities and components at times, usually when relationships are involved (which I have quite many of).

Ideally, an entity is just a logical grouping of components, so there really isn't an entity "object" to add data to because it's a logical grouping, similar to the relationship between armies and units. It's all components. For player ownership, you'd have a PlayerOwnedComponent that can be put on any entity to indicate that it belongs to a particular player. For buildings, you would have a separate BuildingComponent that handles data just related to being a building. The rule of thumb is, if you have to ask whether or not a component needs to be separated into smaller components, it probably does smile.png It's a bit like the single responsibility principle, only when applied to data.

Concrete example of how it works in my game: Say a player has x farms. There is no interest for the player to see these farms as unique, so I would group them into some data structure which says: BuildingType: Farm, Amount: x. But then behaviour needs to be added to this entity (or component, depending on your answer to the above question), so we add ProducesFoodComponent to this entity. Am I on the right track with regards to making the Building an entity in this case, and then linking this entity building to my player entity via a PlayerBuilding Component on either the Player or Building entity? If I don't do separate the building data structure into an entity, I'll get components having children of components, which does not seem right.

This relates back to what I mentioned earlier about logical groupings and queries. You wouldn't have an actual data structure that groups farms, or even cares that they are farms. All the system cares about, is entities that have ProducesFoodComponents, and entites that belong to the player of interest (which will have a PlayerOwnedComponent of the correct player ID). Depending on your level of normalization, it will do a query not far off from this, to get the total food production amount for a player with ID 'x' (I apologize ahead of time for any syntax errors in my SQL!):


 select sum(food.food_amount) from produces_food_components as food inner join player_owned_components as player on food.entity_id = player.entity_id where player.owner_player_id = x

Now of course your implementation might not use an actual query language, but that's the essence of how systems would retrieve the information they need from the collection of components and relations.

Am I trying to hard to create a classic domain model? Are there any "best practises" with regards to e.g. collections when using a component based engine? Is it more common to see denormalized data structures in component based engines?

I'm not sure if there's an established set of best practices, because even now there are a lot of different interpretations and implementations of the "component-based" model. In the interest of full disclosure, for a little over a year I was doing some very heavy database work as part of a project at work, and really grew to love relational data models and the separation of data and logic, so obviously this influenced what I believe to be one of the better implementations of a component-based design. In the case of collections, a collection isn't so much a literal object that contains other objects, as much as it is a result set of a particular query, namely one where you retrieve all objects with CollectionID = 'x' (or in our case, ArmyID = 'x'). The possible values of 'x', or rather the set of army ID's, can either be inferred from the distinct set of army ID's present across all UnitComponent's, or be stored on entities that contain an ArmyComponent. In the latter case you establish an informal constraint on army ID's, and the notion of "valid" versus "invalid" ID's, but whether or not you need that type of constraint is up to you. Usually you'd only have the ArmyComponent if an army needed additional information, and wasn't just a logical grouping or joining of units.

>In this case, would building be an entity connected to a player via a PlayerBuildingComponent (or such)? I'm having difficulty actually separating entities and components at times, usually when relationships are involved (which I have quite many of).

Ideally, an entity is just a logical grouping of components, so there really isn't an entity "object" to add data to because it's a logical grouping, similar to the relationship between armies and units. It's all components. For player ownership, you'd have a PlayerOwnedComponent that can be put on any entity to indicate that it belongs to a particular player. For buildings, you would have a separate BuildingComponent that handles data just related to being a building. The rule of thumb is, if you have to ask whether or not a component needs to be separated into smaller components, it probably does smile.png It's a bit like the single responsibility principle, only when applied to data.

Concrete example of how it works in my game: Say a player has x farms. There is no interest for the player to see these farms as unique, so I would group them into some data structure which says: BuildingType: Farm, Amount: x. But then behaviour needs to be added to this entity (or component, depending on your answer to the above question), so we add ProducesFoodComponent to this entity. Am I on the right track with regards to making the Building an entity in this case, and then linking this entity building to my player entity via a PlayerBuilding Component on either the Player or Building entity? If I don't do separate the building data structure into an entity, I'll get components having children of components, which does not seem right.

This relates back to what I mentioned earlier about logical groupings and queries. You wouldn't have an actual data structure that groups farms, or even cares that they are farms. All the system cares about, is entities that have ProducesFoodComponents, and entites that belong to the player of interest (which will have a PlayerOwnedComponent of the correct player ID). Depending on your level of normalization, it will do a query not far off from this, to get the total food production amount for a player with ID 'x' (I apologize ahead of time for any syntax errors in my SQL!):


 select sum(food.food_amount) from produces_food_components as food inner join player_owned_components as player on food.entity_id = player.entity_id where player.owner_player_id = x

Now of course your implementation might not use an actual query language, but that's the essence of how systems would retrieve the information they need from the collection of components and relations.

Thanks for the response, and sorry for not replying earlier; 10 month old baby to care of while at the same time trying to code when time allows. :)

I've decided to quote all you responded with, given that I'm now zooming in on what I need to do. Basically it sounds like classical database design, but where entities have been separated into a specific ID table, and where you then just add any data to this entity with the component tables.

So in essence any object which needs to have a uniqueness to it needs to be an identity?

A follow up question to this is that of templates. Given that someone will be creating template "classes" to decide from: The player can chose a race for example, which needs to be predefined. One way of doing this would be to have xml files which you then use to insert into the component model, or perhaps even have a component which classifies certain entities as templates. Any experience using templates with component engines?

I've decided to quote all you responded with, given that I'm now zooming in on what I need to do. Basically it sounds like classical database design, but where entities have been separated into a specific ID table, and where you then just add any data to this entity with the component tables.

Ideally, yes, your entity "table" would only contain a single ID field, and each "row" in the component "tables" would reference an entity ID. An entity is then just the collection of all component "rows" with a particular entity ID. I put those terms in quotes because in all likelihood you wouldn't be using an actual database (i.e. MySQL, PostgreSQL, SQL Server, etc.), but rather something which implements a similar relational model.

So in essence any object which needs to have a uniqueness to it needs to be an identity?

Well, yes, the entity ID is unique, which by definition gives it identity... if I interpreted your statement correctly?

A follow up question to this is that of templates. Given that someone will be creating template "classes" to decide from: The player can chose a race for example, which needs to be predefined. One way of doing this would be to have xml files which you then use to insert into the component model, or perhaps even have a component which classifies certain entities as templates. Any experience using templates with component engines?

That's certainly one way of handling templates -- you create a template entity (commonly referred to as an 'archetype') from which you clone to create new entities.

There's also another approach based on the flyweight pattern where you can create two versions of each component "table", one which holds static shared data (max health, max speed, etc.) and one which holds dynamic individual data (current health, current speed, position, etc.). You then have two types of entities, your "type" entities which store shared data, and your "instance" entities would represent the actual objects in the game, and reference "type" entities for their shared data. It's analogous to the difference between a class' member data (per-instance), and it's static data (per-type). Essentially, you're just implementing static data in a relational model.

Lastly, there's a hybrid approach where you use archetypes, and each entity refers to the archetype from which is was cloned. Then, you allow each component field to be "nullable", where a "null" values indicates that the archetype value should be referenced instead of the entity in question's value. This should give you the best of both worlds in terms of memory and flexibility, since you can share data when it's identic to the archetype value and set it locally when it changes (akin to copy-on-write), at the cost of more complexity in the value set/get logic.

I have a pretty good idea of how I'll manage my components and entities now.

Moving on there's the question of subsystems. I've been making some plans for how to structure the game in terms of components, and have come to the following question/thought: If a player constructs a building, this building is in a state known as construction, where the player does not gain full benefits of a fully built house. So in essence it's in a particular state for a while and then progressing to another once completed.

I suppose that much of the in-game logic of these things happen in subsystems?

Let's me try and model the previous question with how it would perhaps look with entities/components.

We have a player entity, and some components to represent that this player has a building in construction then moving on to become fully built. My way of modeling this would as follows:

One entity for the player. This entity would have a PlayerBuildingComponent connected to the entity below.

One entity for the building in construction. This would be marked as a building by having a BuildingComponent (With variables for name + total amount). It would also have a ConstructionComponent with amount of time left until completed. The building is not yet fully utilizing it's potential, so it can only house 100 people while being constructed, it thus has a PopulationCapacityComponent with amount set to 100.

Time passes on and the building becomes completed, but it should now house 200 people instead of 100. Is this the responsibility of the ConstructionSubSystem normally? I really don't want to move any logic to my components, so I'm keen on putting this kind of logic in the subsystem.

One entity for the player. This entity would have a PlayerBuildingComponent connected to the entity below.

I wouldn't model the one-to-many relationship by having the "one" reference the "many" in this case, but rather the other way around. The player entity wouldn't directly store which buildings it owns -- in order to retrieve that information it has to perform a query with a join, along the lines of what I posted earlier. This also enables a much looser coupling between players and buildings, since the player entity doesn't have to updated at all whenever buildings are created and destroyed.

One entity for the building in construction. This would be marked as a building by having a BuildingComponent (With variables for name + total amount). It would also have a ConstructionComponent with amount of time left until completed. The building is not yet fully utilizing it's potential, so it can only house 100 people while being constructed, it thus has a PopulationCapacityComponent with amount set to 100.

The name of the building should probably go in a separate NameComponent, because it isn't something that's particular to it being a building (just about everything has a name). I'm not sure what you mean by "total amount", but if it has to do with construction then it should go in the ConstructionComponent. If it's building capacity, then yes that makes sense to keep in the BuildingComponent. Whether or not you need a separate component altogether for capacity, depends on whether or not other types of objects can hold people. If it's just buildings, then you don't need a separate component. How you split up these components will be very specific to the gameplay and at what granularity you want functionality to be customizable and interchangeable.

Time passes on and the building becomes completed, but it should now house 200 people instead of 100. Is this the responsibility of the ConstructionSubSystem normally? I really don't want to move any logic to my components, so I'm keen on putting this kind of logic in the subsystem.

If the capacity changes as a result of construction being completed, then yes your construction system would increase the capacity. If the capacity goes down because the building is run-down and ill-maintained, let's say, then the "run-down" system would change the capacity. The BuildingComponent could then store the various various capacities, or multipiers (however you want that to work), so the different systems know how the building (namely its capacity) should change in these circumstances.

If the capacity changes as a result of construction being completed, then yes your construction system would increase the capacity. If the capacity goes down because the building is run-down and ill-maintained, let's say, then the "run-down" system would change the capacity. The BuildingComponent could then store the various various capacities, or multipiers (however you want that to work), so the different systems know how the building (namely its capacity) should change in these circumstances.

I'd be wary of having multiple systems modifying the same property of a Component. Instead, I would have one system that is responsible for setting the current population capacity based on various states (e.g. whether a building is in construction, how long has been since it was maintained, etc...).

Of course this doesn't necessarily mean you have a construction system, a run-down system (to track how long it's been since it was maintained), a population-setting system, etc.... They could all be different stages of the Update cycle of the same "building" system (assuming they all rely on the same set of components).

One entity for the player. This entity would have a PlayerBuildingComponent connected to the entity below.

I wouldn't model the one-to-many relationship by having the "one" reference the "many" in this case, but rather the other way around. The player entity wouldn't directly store which buildings it owns -- in order to retrieve that information it has to perform a query with a join, along the lines of what I posted earlier. This also enables a much looser coupling between players and buildings, since the player entity doesn't have to updated at all whenever buildings are created and destroyed.

Ah yes ofcourse. Higher cohesion.

One entity for the building in construction. This would be marked as a building by having a BuildingComponent (With variables for name + total amount). It would also have a ConstructionComponent with amount of time left until completed. The building is not yet fully utilizing it's potential, so it can only house 100 people while being constructed, it thus has a PopulationCapacityComponent with amount set to 100.

The name of the building should probably go in a separate NameComponent, because it isn't something that's particular to it being a building (just about everything has a name). I'm not sure what you mean by "total amount", but if it has to do with construction then it should go in the ConstructionComponent. If it's building capacity, then yes that makes sense to keep in the BuildingComponent. Whether or not you need a separate component altogether for capacity, depends on whether or not other types of objects can hold people. If it's just buildings, then you don't need a separate component. How you split up these components will be very specific to the gameplay and at what granularity you want functionality to be customizable and interchangeable.

Total amount would be the number of buildings of type x contained in this entity. I don't want a separate row for each type x building, but instead group them into a column (total_amount) instead.

Here's the data (not necessarily defined as a whole component) contained for each building:

type/name of building, total number of buildings, state = {in construction, developed} , living capacity (varies depending on state), days until completed (> 0 if in construction)

There is one thing I forgot to mention. A player has Land which he/she constructs buildings on. Perhaps a more suitable model would simply be to have a LandComponent with aformentioned data, and if land has not been built then type/name would be set to "Land" and state to Unexploited or something similar?

Each building would also have bonuses that would take effect when the building is completed. Say a building increases attacking effect by x%, then some AttackSubSystem would need this information when a player attacks. But the building's bonus does not go into effect until it is completed. How does one model this state change best, while still maintaining high cohesion in the AttackSubSystem (not having to know too much about the building's logic)? I have two options here that spring to mind: 1) The AttackComponent has two columns, one for active value, and one for "desired/aim/target"-value. The subsystem always uses the active value, while the BuildingSubSystem copies target value to active value when the building is completed. 2) Another AttackModifierComponent is introduced: AttackModifiedInProgressComponent. This component would be connected to the building when first constructed and then later converted to an AttackModifierComponent.

Option 2 looks really ugly and adds complexity IMO. Any other options?

Time passes on and the building becomes completed, but it should now house 200 people instead of 100. Is this the responsibility of the ConstructionSubSystem normally? I really don't want to move any logic to my components, so I'm keen on putting this kind of logic in the subsystem.

If the capacity changes as a result of construction being completed, then yes your construction system would increase the capacity. If the capacity goes down because the building is run-down and ill-maintained, let's say, then the "run-down" system would change the capacity. The BuildingComponent could then store the various various capacities, or multipiers (however you want that to work), so the different systems know how the building (namely its capacity) should change in these circumstances.

I'm somewhat afraid of several subsystems acting on the same data, although in some cases I don't see any other way (common resources for example). Perhaps the multiplier suggestion would be possible during state change as well (read question/suggestion above with regards to in construction/developed)? When a building is in construction you would add a BonusMultiplier of 0.0 to the AttackModifier (if that building would have any such modifiers), and then change this to 1.0 once the building is completed.

This topic is closed to new replies.

Advertisement