Search Unity

How to have components containing arrays in ECS?

Discussion in 'Entity Component System' started by faldor20, May 15, 2019.

  1. faldor20

    faldor20

    Joined:
    Feb 9, 2017
    Posts:
    6
    Hey so i have been having some trouble with the ecs system and arrays, hopefully someone will be abel to shed some light on this.

    Basically i need to use arrays inside components in a project i am doing and they weren't working as expected so i created a simple test project. Have a bunch of objects move following a set of commands defined inside an array. this would use shared component data because many of the objects would be doing the exact same actions. The component used can be seen below.
    Code (CSharp):
    1.     public struct MoveActions : ISharedComponentData {
    2.              [Range(-90f, 90f)]
    3.              public NativeArray<float> rotations;
    4.              public NativeArray<float> distances;
    5.          }
    However i have struggled to actually access the arrays. If i use a normal array, when i pass the component into a job it errors because it should be a native array. If i use a nativearray(as you see above) it ends up being uninitialized and errors. My solution to that was to use a component data proxy like this:
    Code (CSharp):
    1.      [RequiresEntityConversion]
    2.          public class MoveActionsProxy : MonoBehaviour, IConvertGameObjectToEntity {
    3.              [Range(-90f, 90f)]
    4.              public float[] Rotations;
    5.              public float[] Distances;
    6.              public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    7.              {
    8.              dstManager.AddSharedComponentData(entity, new MoveActions
    9.                  {
    10.                      firstDistance = FirstDistance,
    11.                      firstRotation = FirstRotation,
    12.                      rotations == new NativeArray<float>(Rotations, Allocator.Persistent),
    13.                      distances = new NativeArray<float>(Distances, Allocator.Persistent)
    14.                  });
    15.          }
    However once i use the component in a system the component appears to be empty and still gives me an error saying it is uninitialized. Basically i have no idea what to do at this point and any help would be fabulous.
    (i also posted this on unity answers but i felt like this may be a better place to get help)
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
  3. wangyucheng1992

    wangyucheng1992

    Joined:
    Feb 19, 2019
    Posts:
    59
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
  5. faldor20

    faldor20

    Joined:
    Feb 9, 2017
    Posts:
    6
    Hey so i went and did some research and looked at some stuff. i can't use dynamic buffers because i want to use the functionality of shared components. All i want to do is extend the unity boid example and have it so you can chose from a list of boid behaviors. currently it just always does the same ones i want to be able to populate a list with a bunch of enums each representing a boid action to be performed. That means dynamic buffers don't work.

    i can get shared components to work fine as long as i pull the arrays from the component and turn them into native arrays in the update loop and then pass the native-arrays into the job.
    However when i do that for some reason the shared components end up split into one component per boid. The sharing doesn't work even though the data is the same. if anyone has an ideas what i should do here, please help.

    PS: A boid is an entity that follows a simple set of behavioral rules relating to position and proximity to simulate flocking behavior
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    Why you are saying enums don't work with dynamic buffer? Enums are nothing more than integer. Just define numeric values to each enum option.

    I use enums and dynamic buffers this way completely fine with burst.
     
  7. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    967