Is there a way to get Component Data inside Job if i know the type only in process of Job

I have different types that represents my objects in a game like rocks or forests or whatever else. And i can’t predict how many this types i will have in a final game. There is a vision system in my project, and I need to somehow refer to the data on my rocks and houses and everything that was visible.

To do this i see two ways:

  • Store an entities of visible objects in entity lists in count of objects types. And then get ComponentData using EntityManager.
  • Search for visible objects of the type i interested in every moment that i need.

There is a point in my game when i need to say to unit that if he need wood he need to go to the forest and if he need the stone he need to go to rocks. But before he go i need to choose what forest are the best for him. So i need to get for example every visible object with type Forest and chose the best for current unit.

I have no problem when I do it in one thread, because I can get all the necessary ComponentData through the EntityManager.GetComponentData <T> (Entity) without anything extra. But I need to do this in multi-threading cause i have bunch of units, and there are problems here, because it seems I need to prepare all the possible data that I can use. For example, if I have 10 types of resources and 5 units that need some resources, and if 4 units need a stone, and 1 last unit needs a forest, then if I want to do it in parallel, I have to prepare all the object data all 10 types 8 of which are not used in this case (also i need to do this in count of players count cause each player see different objects). So the main question is: have you any ideas how to get the data that i need in a job and not prepare it every time?

And the second additional question is: what the best way to translate ComponentData from one job to another? I mean the case when i have 100 entities and after first job i need to compute only part of those 100 entities in a second job.

.

You can use ComponentDataFromEntity.Exists (entity) within a job to check if an entity has a particular component.

For second part, you can add a tag to them or just poll them all for a changed value or you could write to a data structure that you pass into the next job.
If there’s not many entities, probably just setting a value on the component and polling for it in the next job would be easiest, otherwise you have to sync on barrier on main thread first to add a tag.

I know what entity has a what ComponentData, but how can i get access to this ComponentData without preparing all possible

Adding a tag means adding a zero-sized ComponentData and it chages Archetype of an entity so it will be rewrited in another chunk and rewriting may be costly. Search for changed value may be just as costly. Writing to data structure and then pass it to next job seems like best descision independent of entities count. But the only way i see is: get Component Data Array through ComponentGroup and then save an indices of items we need for next job.

If you use IJobProcessData or IJobProcessDataWithEntity you can get the data.

I know about this interfaces, but i need to know what type of data i will use in IJobProcessComponentData. I don’t want to make access to all data i can probably use. For example i have 100 units and 50% of them wants to eat, so i access to data of food source, and 50% of them wants to get a stone, so i access data of stone source, and also i have 10 another type of data (wood source, gold source, etc) that can be needed to access, but i don’t know what set of components i will use before the job.

I can separate the job of resource mining and prepare the pull of resources that i will compute and then run only those jobs that i need, but this means that i have 1 job for 1 resource type.

Maybe I didn’t clearly explain my problem. So i added a scheme)

4214068--374146--Cases.png