I have seen in other ECS frameworks that systems have a sort of void OnEntityAdded(Entity entity) and void OnEntityRemoved(Entity entity) methods to perform some action when an entity is added to or removed from a system. Is there something similar in entities framework? I couldn't find anything similar on the documentation. Thanks!
#Is there any way to detect when an entity is added to a system and perform some action on it?
1 messages · Page 1 of 1 (latest)
Entities do not belong to systems
They exist in chunks of specific archetypes
You can query all chunks by relevant components
but to check whether entities are added to that query you'd need a complex solution
In short there is no such functionality and it's not recommended
Instead there is another approach
You can declare 2 types
One is marker with setup data
Other holds data
then you have a system which looks for ones without data component and adds it
Another system looks for entities with data components, but without markers. And disposes of data
Marker should be normal icd
Data shouod be cleanup component
Cleanup components do not let you destroy entity until you explixitly remove it from entity
There is one another approach. You can use OrderChangeFilter with your query (or something named like that), that filter returns only chunks which has added or removed entities. It doesn't mean that entity didn't exist before, but it means that entity was added or removed from chunk (due to any kind of structural changes, including entity creation from scratch)
ok so i read this question hours ago but was out and just got home to answer it and issue and tonymax have answered with the 2 most common ways of doing this
however there is actually a method of getting created/destroyed entities
EntityManager.GetCreatedAndDestroyedEntities
/// <summary>
/// Detects the created and destroyed entities compared to last time the method was called with the given state.
/// </summary>
/// <remarks>
/// Entities must be fully destroyed, if cleanup components keep it alive it still counts as not yet destroyed.
/// <see cref="EntityCommandBuffer"/> instances that have not been played back will have no effect on this until they are played back.
/// </remarks>
/// <param name="state">The same state list must be passed when you call this method, it remembers the entities that were already notified created and destroyed.</param>
/// <param name="createdEntities">The entities that were created.</param>
/// <param name="destroyedEntities">The entities that were destroyed.</param>
public void GetCreatedAndDestroyedEntities(NativeList<int> state, NativeList<Entity> createdEntities, NativeList<Entity> destroyedEntities)```
🤔
interesting
would an nice way to make a reactive pipeline
read, fill into buffer. Run systems on this buffer. flush it
It is possible to use this and write a singular dispatcher for this if that is your preferred method
i think most people use Issues suggestion of a component to detect new entities
i use that for some types of entities but for high performant (lots of entities) I actually use TonyMax's suggestion
I don't think GetCreatedAndDestroyedEntities is overly performant as I believe it looks all entities and checks their version vs the state buffer
it is done in a bursted job though at least
job? so it can be threaded?
oh yeah there is an async version
public JobHandle GetCreatedAndDestroyedEntitiesAsync(NativeList<int> state, NativeList<Entity> createdEntities, NativeList<Entity> destroyedEntities)
and state list is just some persistent list you have to pass all the time?