Unity dots, how can I loop though a bunch of entities and then for each loop through a bunch of entities associated with first entity?

For an RTS game, lets say I have individual units, and unit_groups which store information which all of the units in the group share.

So for example, I select a group with a mouse, and then click somewhere. The group will store a position, and all of the units of this group will move to that position.

Can I store a list of all the units in the group entity in one of its components? This is so that in a job, I could just go ForEach(Unit_Group), and then loop through all of the units in the group and move them.

Inside a job:

For each unit group

For each unit associated with this group

Do some work on this unit

For reference, I am trying to replicate what the Total War series does with their unit groups and individual units.

What I think I’ll do is just make a separate IComponentData for each group, and just have a separate job for each group as that way I can easily filter out the groups I dont need, at the cost of extra repetitive code.

Here’s one idea of how to think about it:

  1. on user action, you might calculate some information about the movement of the group. If there is a formation or such, you might precalculate the destination of each individual unit. You might add a tag component to each of the units to indicate they are now “alive” and moving so should get processed by the move job. You might consider using a shared component data to store information about the group.
  2. Optional: you might occasionally update the calculation on #1 if there are situations where you need to reroute, this could even cross frames (might not have to fit in a single frame if it’s complex enough)
  3. In the move job, you can loop through all units that are moving and have them move, using their own navigation data and possibly group data in the shared component.
  4. Once a unit has arrived and should stop, you can remove the move tag.

So, instead of thinking about looping through groups, it might work to think of looping through all active units and each would have access to its group data to calculate appropriately. And if you need to calculate group data on a regular basis, have a group job that updates the shared group data, and a unit job that depends on it and uses that group data for each unit.