Search Unity

Do I need to use ''ref'' when pass ArchetypeChunk to another function?

Discussion in 'Entity Component System' started by NoDumbQuestion, Jan 2, 2019.

  1. NoDumbQuestion

    NoDumbQuestion

    Joined:
    Nov 10, 2017
    Posts:
    186
    I saw 'ArchetypeChunk' is unsafe struct. But the example i saw from coffeebraingame is just like below. So I got a little confuse, since struct is pass by value not sure if pass it without 'ref' might slower or Burst compiler can figure it out.

    I still new with the whole ECS things so if anyone have any better code example in current state of ECS. I will gladly take it

    Edit: I just realize we have IJobChunk. So I guess i will switch to that
    Code (CSharp):
    1. public class MoveForwardSystem : JobComponentSystem
    2. {
    3.     private ComponentGroup m_group;
    4.  
    5.     // Replace normal Inject group
    6.     protected override void OnCreateManager() {
    7.         this.m_group = GetComponentGroup(typeof(Position), typeof(Rotation), typeof(MoveForwardSpeed));
    8.     }
    9.  
    10.     protected override JobHandle OnUpdate(JobHandle inputDeps) {
    11.         NativeArray<ArchetypeChunk> chunks = m_group.CreateArchetypeChunkArray(Allocator.TempJob);
    12.         JobProcessMoveForward job = new JobProcessMoveForward() {
    13.             deltaTime = Time.deltaTime,
    14.             position  = GetArchetypeChunkComponentType<Position>(),
    15.             rotation  = GetArchetypeChunkComponentType<Rotation>(),
    16.             speed     = GetArchetypeChunkComponentType<MoveForwardSpeed>(),
    17.             chunks    = chunks
    18.         };
    19.         //Chunks got dispose after inside job complete
    20.         return job.Schedule(chunks.Length, 32, inputDeps);
    21.     }
    22.  
    23.     [BurstCompile]
    24.     private struct JobProcessMoveForward : IJobParallelFor
    25.     {
    26.         public            ArchetypeChunkComponentType<Position>         position;
    27.         [ReadOnly] public ArchetypeChunkComponentType<Rotation>         rotation;
    28.         [ReadOnly] public ArchetypeChunkComponentType<MoveForwardSpeed> speed;
    29.         [ReadOnly] public float                                         deltaTime;
    30.  
    31.         // Dispose
    32.         [ReadOnly] [DeallocateOnJobCompletion] public NativeArray<ArchetypeChunk> chunks;
    33.  
    34.         public void Execute(int index) { Process(chunks[index]); }
    35.  
    36.         private void Process(ArchetypeChunk chunk) { // Does this one need to use ref
    37.             var positions = chunk.GetNativeArray(position);
    38.             var rotations = chunk.GetNativeArray(rotation);
    39.             var speeds    = chunk.GetNativeArray(speed);
    40.  
    41.             for (int i = 0; i < chunk.Count; i++) {
    42.                 positions[i] = new Position() {
    43.                     Value = positions[i].Value +
    44.                             math.forward(rotations[i].Value) * speeds[i].moveSpeed * deltaTime
    45.                 };
    46.             }
    47.         }
    48.     }
    49. }
     
    Last edited: Jan 2, 2019
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    No, but this is something I consider could be quite a confusing for new users.

    Basically all these structs just hold pointers (which is why they're unsafe). If you pass a pointer by value it is still going to point to the same location in memory.
     
    NoDumbQuestion likes this.
  3. NoDumbQuestion

    NoDumbQuestion

    Joined:
    Nov 10, 2017
    Posts:
    186
    That make a lot of sense. Thanks for fast answer after new year eve.