Pre-ECS, I frequently use singletons to maintain a Dict<int, MyObj> of loaded objects.
When converting this to ECS and making it available in a job, I see three approaches
In job, call ComponentDataFromEntity and loop over entities testing componentdata
In job, inject SharedComponentData and use GetFilter
Replace Dict with NativeHashMap<int, Entity> and pass (readonly) to the job
Appreciate any feedback on these.
Jaochim advises against (2) in this post.
Think I’m missing a trick with SharedComponentData… Dict in SharedCompData?
I’m not sure if that would do the trick for you, but I would try to create a ComponentGroup with a ComponentArray Component and the EntityArray. If I understand correctly, the would give you all entities which have the Loaded component attached, so you would need to add that component to all your entities, but then you could easily iterate through them.
don’t access World.Active from inside the job. accessing a static variable throws up all the safety system and will break when Unity implements proper checks to prevent that (they will)
you can add a ComponentDataArray or ComponentDataFromEntity to the job if you need to access it.
or you can use IJobProcessComponentData that takes care of everything (and is parallel)
you can add an id component to each entity:
public struct DatabaseId : IComponentData { public int value; }
the global Dictionary should not be accessed from jobs anyway, you can keep it “outside ECS” i.e. in your DB interface layer (you use it for DB → ECS operations, and you use the DatabaseID component on the entities for your ECS → DB operations)