Doraku / DefaultEcs

Entity Component System framework aiming for syntax and usage simplicity with maximum performance for game development.
MIT No Attribution
658 stars 62 forks source link

Difference between "WhenAdded" and "SubscribeComponentAdded" ? #134

Closed genaray closed 2 years ago

genaray commented 2 years ago

Is there a difference between those two ?

I lately tried to react on newly created entities like this :


[Update]
private void Update(float state, [Added] ref Resource resource){ ... }

But it never triggered when i created a new entity and set the Resource component. However, the SubscribeComponentAdded works fine. So i wonder if theres a difference between them :)

Doraku commented 2 years ago

When... is a special rule used when created entity containers (set, map, ...) to make them reactive, they should contains entities respecting those rules (so for a WhenAdded<Resource>, only entities which have had a Resource component added to them since the last time the Complete method of the container was called). This is for delayed processing in a system, you have to enumerate the content yourself when you want to (in the case of a generated system like your code, you need to call the Update(state) of the system like any other). If you didn't get anything even when adding Resource component on entities, maybe something is not working properly :( SubscribeComponentAdded is to add a callback for when a component is added on an entity, regardless of this entity full composition, and is called synchronously.