# Composition heavy OOP vs pure entity component systems?

This topic is 1960 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I would like to say this is a great topic so far, with lots and lots of info contained and a lot of opinions. I was wondering if this is a good example of what would be a good entity architecture so that I see if I should go ahead with it or not: http://obviam.net/index.php/design-in-game-entities-object-composition-strategies-part-1/
http://obviam.net/index.php/design-in-game-entities-object-composition-strategies-part-2-the-state-pattern/

##### Share on other sites

I would like to say this is a great topic so far, with lots and lots of info contained and a lot of opinions. I was wondering if this is a good example of what would be a good entity architecture so that I see if I should go ahead with it or not: http://obviam.net/in...ategies-part-1/
http://obviam.net/in...-state-pattern/

Gabriel, that's kind of the beginning of the idea of Composition, but it's only partial IMO. It limits some because, what if you have a droid that you can ride on? Is it a droid? or a vehicle?

Using a true entity/component system, it can be both easily. Instead of having a is-a droid relation ship, and it would go into the droid list, you have a generic entity, built with a graphics component, physics component, droid component, and weapon component.

Then you have systems that act on these components. ie, the droid system handle entities with the droid component, and it handles the AI and movement (depending if droid component was built with wheels or track, or nothing), as well as attempting to shoot it's weapon.

If you add a vehicle component, then the vehicle system can work on that entity, and allows a player to ride on the droid, and shoot from the back of the droid.

That's the general idea. My dev journal has some articles about it, and there's plenty of other data too.

##### Share on other sites

[quote name='Gabriel Grom' timestamp='1345830128' post='4973051']
I would like to say this is a great topic so far, with lots and lots of info contained and a lot of opinions. I was wondering if this is a good example of what would be a good entity architecture so that I see if I should go ahead with it or not: http://obviam.net/in...ategies-part-1/
http://obviam.net/in...-state-pattern/

Gabriel, that's kind of the beginning of the idea of Composition, but it's only partial IMO. It limits some because, what if you have a droid that you can ride on? Is it a droid? or a vehicle?

Using a true entity/component system, it can be both easily. Instead of having a is-a droid relation ship, and it would go into the droid list, you have a generic entity, built with a graphics component, physics component, droid component, and weapon component.

Then you have systems that act on these components. ie, the droid system handle entities with the droid component, and it handles the AI and movement (depending if droid component was built with wheels or track, or nothing), as well as attempting to shoot it's weapon.

If you add a vehicle component, then the vehicle system can work on that entity, and allows a player to ride on the droid, and shoot from the back of the droid.

That's the general idea. My dev journal has some articles about it, and there's plenty of other data too.
[/quote]

Hey BeerNutts!

You make a good point. I might be missing the actual point though, but wouldn't just adding a Riding component solve the problem?

The reason why I feel the need to have predefined entities, and also to have tightly coupled components, such as for example the texture and rendering component, or the collision and movement component, like some people here mentioned is because of the two concepts I'm having trouble with, which are the layering of the game that Hodgman and Ryuu talked about, as well as inter component communication.

Also I'm having trouble with how I will differentiate between entities, such as player and AI or vehicle and person.

Things like updating the components also come to mind. Should I make a system for each type of component that updates the components of the same type, in which case should l iterate the types in order? For example iterate over the physics components and update them first, after which I iterate over the rendering components?

What I mean by that, and most of the above is, let's say that there is a vehicle or droid which players can ride. How will I be able to differentiate the players from the other vehicle components such as the collision component? If things are made as generic as you say, and perhaps here I misunderstood you, in that I understood you said that we should make the entities only have a reference to the generic Component class, rather than manually hardcode that a vehicle will have armor, characters, wheels so as to gain maximum flexibility.

If it is hardcoded, then the vehicle will be able to directly manage the characters' updates. What I have in mind is, that when a character is added to a vehicle, the player's control, or that of the AI is stripped, and given to the vehicle, which in turn has a controller, player or AI. I would have addCharacter() method in the vehicle entity or in the Riding component which will do what I just described.

If however, the vehicle only knows generic components, and vehicle is generic in its self, then how will I be able to know that the component being added is a character, and how will I know that the vehicle has a Riding component? Don't get me wrong, I'm not asking how to do those things, but the solution I am imagining in my head entails a lot of boilerplate and messy code, which is essentially a massive Component class to accommodate for every possible action or scenario in the game.

 public void addComponent(Component Receiving, Component Attaching) { ...... if(Receiving.has(RidingComponent) if(Attaching.is(CharacterComponent) RidingComponent.addComponent(Attaching, type CharacterComponenet); ......... } 

And that's just a small snippet of the method, where other lines would test for rendering, collision, animation, etc...

My opinion is that for small-medium sized games, something akin to say.. the size of Warcraft 2 or Red Alert like game, over-engineering by going completely generic and flexible at the cost of code complexity does not really pay off.

Then again I've never done a component system before. Edited by Gabriel Grom

##### Share on other sites

If however, the vehicle only knows generic components, and vehicle is generic in its self, then how will I be able to know that the component being added is a character, and how will I know that the vehicle has a Riding component? Don't get me wrong, I'm not asking how to do those things, but the solution I am imagining in my head entails a lot of boilerplate and messy code, which is essentially a massive Component class to accommodate for every possible action or scenario in the game.

If components know about their owners, then you can move their dependency tests to be inside of them. Say with a method, Component::Attach(Entity), that tests the entity to which this component is being attached for dependencies and does whatever other magic it wants to do. If components do not know about their owners, you can move that logic to be external from the Component class with a free function, like AddCharacter(Entity), that executes your dependency tests and such. Alternatively, you could go more data driven, and define the relationships between components in some softer format, like ComponentAttacher::DefineConstraint(Component receiving, Component attaching, function constraint), and then make all of your connections through ComponentAttacher.

No matter what though, such logic definitely does not go in the base component.

##### Share on other sites
public void addComponent(Component Receiving, Component Attaching) { ...... if(Receiving.has(RidingComponent) if(Attaching.is(CharacterComponent) RidingComponent.addComponent(Attaching, type CharacterComponenet); ......... } 
I personally group "component/entity" designs into two categories -- those that try and perform "magic" linking of components to each other, and those that require the entity creator to manually connect components to each other.
class MyEntity { ComponentA a = new ComponentA(); ComponentB b = new ComponentB(); ComponentC c = new ComponentC(); void Init_1() { a.InspectParentToPerformMagicLinking( this );//uses reflection to look at my members b.InspectParentToPerformMagicLinking( this );// to automatically make any required connections c.InspectParentToPerformMagicLinking( this );// without my explicit knowledge of how this works } void Init_2() { a.ExplictlyLinkYouToC( c );//I, the entity author, know that A needs a C. b.ExplicitlyLinkYouToA( a );// and that B needs an A. } };IMHO, the first option violates too much of my deeply ingrained engineering intuition to be considered. Explicitly plugging things in seems superior in almost every way to me... Ideally these explicit links, and the entity data-structures themselves should be loaded from data/scripts, but can be hard-coded as above.
Also I'm having trouble with how I will differentiate between entities, such as player and AI or vehicle and person.[/quote]If you need to differentiate between them, don't put them all in some generic collection (also don't inherit them from a common base if they don't need it). Am I right in paraphrasing that this is the same as: "I've inherited vastly different classes from a common base, but now I can't use them for different purposes"? If so, it's the same flaw that entity inheritance-trees had.
Put vehicles in a vehicle-list, and players in a player-list, if needed.
we should make the entities only have a reference to the generic Component class[/quote]I've seen designs such as this, but IMHO it's a harmful idea. Imagine replacing all of your [font=courier new,courier,monospace]floats[/font], [font=courier new,courier,monospace]int[/font]s, [font=courier new,courier,monospace]Array[/font]s, etc, with [font=courier new,courier,monospace]object[/font]... and then having to use casts absolutely everywhere. It defeats the purpose of having a type system.
If it is hardcoded, then the vehicle will be able to directly manage the characters' updates. What I have in mind is, that when a character is added to a vehicle, the player's control, or that of the AI is stripped, and given to the vehicle, which in turn has a controller, player or AI. I would have addCharacter() method in the vehicle entity or in the Riding component which will do what I just described[/quote]N.B. as well as "hard-coding" these kinds of specific relationships in your C++/C#/Java etc codebase, it can also be "soft-coded" in your scripting language or data files. IMHO, this is much better than creating an over-engineered framework. Edited by Hodgman

##### Share on other sites
Hey all~ I am back after a somewhat long absence, as I've been busy acclimating into my new job. I wanted to update you guys who took the time to help me with what happened! (And I might hope for a little feedback for how right (or how less wrong) I did things.)

So, after some more search, I fell upon the Apollo entity framework. It is built by the same folks who has made the Artemis entity framework, but is more 'object-oriented' in its thinking. I fiddle around with it in the past couple of weeks, and I must say I really do prefer it a lot over Artemis, which I wasn't planning on using.

This on the other, I loved! I'm not using the platform in its entirety (yet), and I have made some modifications of my own to suit my programming taste (to the builders, etc..)

I have made a small, small demo where I integrated it with LibGDX and Box2D, and I have the code up on Github for those who want to make me feel bad by telling me how bad it is.

My architecture is essentially having the Apollo world contain the Box2D world (though I keep a separate reference for the Box2D world for convenience).

I have a Renderable component, along with a collidable component. The collision component depends on the Renderable component, but for the time being only, and only for practicing/experimenting with things, but they should be completely independent, and only separately depend on the Transform component which holds the position.

I have a collision detection listener which doesn't itself act on the bodies, but triggers a flag in the Collidable component and forwards it the needed information to handle the collision (the other body, its type, etc..)

I haven't used the event listeners built into Apollo, so I cant' say anything about that, though I might use it later on. Sorry about that, Gabriel, but I hope I can still answer some of your questions in the code.

I'm using builders like Apollo recommends, though I have modified those to take several parameters. This broke the methods related to builders (which was rather convenient) but it's nothing fatal. (To those who made Apollo, my sincerest apologies for butchering your framework!!)

So yeah, that's pretty much it! Don't judge too harshly.~

I wonder if I should start my own game dev diary.. But I don't want to infect people with bad code. xD Edited by Midori Ryuu

##### Share on other sites

I wonder if I should start my own game dev diary.. But I don't want to infect people with bad code. xD

well, i think you SHOULD start one. this way you will learn much more. don't be shy. there's no such thing as bad code, only code that has not been polished yet