Search Unity

Is there ever a reason to use ComponentType.Create over ComponentType.ReadOnly

Discussion in 'Entity Component System' started by Brainshack, Jan 3, 2019.

  1. Brainshack

    Brainshack

    Joined:
    Mar 1, 2013
    Posts:
    33
    Hi there,

    so I am currently trying to move from [Inject] to GetComponentGroup. I have always wondered if there ever was a use-case to not use Readonly (both when using Inject and GetComponentGroup). As far as I could figure out, changing values in ComponentDataArrays won't have any effect regardless. As far as I can tell you will always have to use SetComponentData to Update Component Data. Am I missing something here?
     
  2. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    I'm fairly certain you can update entities using ComponentDataArrays (example).

    Note: You can also use chunk iteration with component groups now.
     
  3. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    Indexing cda doesn't return a reference. You have to set the new value separately:
    Code (CSharp):
    1. var p = positions[i];
    2. p.Value += new float3(1, 0, 0);
    3. positions[i] = p;
     
  4. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Hmm. So is the use case here that if you are mutating data directly like that you have to declare your data with ComponentType.Create, but if you are using PostUpdateCommands.SetComponent you can declare you data with ComponentType.ReadOnly?
     
  5. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    The CommandBuffer is always executed with exclusive access to all entity data and might do whatever it wants to it. You don't have declare any access method when using a CommandBuffer. Using CDA allows multithreaded access and needs to know which systems may run in parallel without potential race conditions.