Store a list of references to Entities in a component?

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.

use dynamicbuffers as an equivalent to arrays on entities

1 Like

I am using a logic along the following lines.

  1. have a system that takes player input, runs some calculations to see the area the clicks/drags affects
  2. the system from 1 affects the entities in the area and attaches a component to them (let’s call it Input, or Selected or whatever you feel). This system also always runs to update this component we attach; so it has a job selecting the entities with the Input component and updates that component with player Input actions because this component contains some data fields that retain the needed data from the Input (something like public float3 RightClickPointInWorld)
  3. have a system running on the entities with the Translation and Input/Selected/your component name; The do a ForEach that reads from Input and writes to Translation. So you take the meaningful Input data (e.g clickInWorldWhereINeedToGo) and then act on the entity Translation by further calculations

This is the way I understand data design at the moment (which might not be optimal and will keep an eye for other suggestions here) and I hope it’s useful for you.

Cheers!

1 Like

I can have a component with a dynamic buffer of entities? Can I access this dynamic buffer from within a job?

The component with dynamic buffer is called Dynamic Buffer and you can read about it here:
https://docs.unity3d.com/Packages/com.unity.entities@0.5/manual/dynamic_buffers.html

Yes, you can access the buffer from a job using the IJobForEachWithEntity_EB (and all others EBB, EBBB etc.); EB comes from Entity and Buffer.

1 Like