Add / Remove components design question

80 views
Skip to first unread message

Michele Rullo

unread,
Sep 11, 2014, 8:46:46 AM9/11/14
to ash-fr...@googlegroups.com
 I have a general question regarding ECS paradigm. This is a scenario I would like to avoid:

There are Goblin creatures. A Goblin has several components attached to it (such as Motion, Position, Animation and so on..). At the time of creation of a Goblin, I initialize each component with the initial data. During the game it can happen that a component should be removed from the entity (such as Animation Component). Such an action implies that I loose all the data that I set before. This means that when I have to re-add the removed component, I have to re-initialize it with the initial data. In order to restore the initial data I need to know that the entity was, indeed, a Goblin. So my core-code becomes data-dependent, and it looses generality.

I would like to avoid this. Do you have any interesting (and elegant!) solutions?

I hope the question is clear, thank you in advance.

James Wrightson

unread,
Sep 11, 2014, 3:28:56 PM9/11/14
to ash-fr...@googlegroups.com
Well what you are aiming for is a way of add/remove a component throughout your game so the goblins state changes.

You could use multiple "entity state machines" to store them or alternatively,

A "ComponentGroup" component, that stores a dictionary of component instances.

As long as that component stays inside the goblin, your systems can re-add a removed
component from that group.

Think of it as "placing a component on/off a shelf".

Michele Rullo

unread,
Sep 12, 2014, 5:58:59 AM9/12/14
to ash-fr...@googlegroups.com
I definitively like the "Component Group" solution. It could be an associative array with entity ID's as keys. As soon as I have to remove a component I push a copy of it inside the dictionary. When I have to re-add it I simply query the dictionary with my entity ID. Am I missing something?

Thank you.

James Wrightson

unread,
Sep 12, 2014, 8:12:42 AM9/12/14
to ash-fr...@googlegroups.com
Yep it is as simple as a lookup on that component :)

James Wrightson

unread,
Sep 12, 2014, 8:47:07 AM9/12/14
to ash-fr...@googlegroups.com
That is correct, very simple solution this time :D

Damion Murray

unread,
Sep 12, 2014, 8:30:07 PM9/12/14
to ash-fr...@googlegroups.com

Here are a few other options: 

Your question seems to imply that there are properties in components that should be retained while others are being discarded. Take a closer look at the component(s) in question. Can the properties you want to retain be split off into a different component?

If splitting off properties into another component is not an option you also have the option of creating a flag/marker/tag component (there are many names for this construct).

A flag component has no real data in it, but is required by a system to process a given set of components. Let's take your Goblin entity's AnimationComponent for example, with a flag component in play your AnimationSystem will expect a node with (at least) the following components:
  • AnimationFlagComponent
  • AnimationComponent 
If you remove the AnimationFlagComponent the AnimationComponent won't be processed by the AnimationSystem even though its still attached to the Goblin entity. If you want to re-enable the Goblin's animation simple add back a AnimationFlagComponent to the Goblin entity and voila your Goblin is again animated. 

Given your situation, these approaches are much cleaner than 'pooling' your components unnecessarily. But hold on to the pooling idea though, there are use cases that require it.

Michele Rullo

unread,
Sep 13, 2014, 4:55:22 AM9/13/14
to ash-fr...@googlegroups.com
Thank you very much for your answer, this is probably a better solution: simple and effective.

Damion Murray

unread,
Sep 14, 2014, 12:17:05 AM9/14/14
to ash-fr...@googlegroups.com
Glad my advice was useful to you. 

Come to think of it, it may be wise to have a flag component for each system. That way its easy to control if you want an entity to be processed by a system or not.
My current project is setup this way.

Hmmm...I think the technique should be formalized as a kind of ECS design pattern. 

We need a name for it though...
Flag component pattern?
Anyone care to come up with something better?

James Wrightson

unread,
Sep 14, 2014, 5:50:48 AM9/14/14
to ash-fr...@googlegroups.com
Hey Damion,

Nice solution :)

The technique is called "marking", check around the rest of the user group to see where.

(although not official or anything).
Reply all
Reply to author
Forward
0 new messages