Search Unity

Question Array in IComponentData

Discussion in 'Entity Component System' started by hellengoodd, Oct 16, 2022.

  1. hellengoodd

    hellengoodd

    Joined:
    May 31, 2017
    Posts:
    51
    If there are many fields in a component data are array, is the following is the best way to write it?
    or use DynamicBuffer or NativeArray or BlobAsset is the best way?

    Code (CSharp):
    1. public unsafe struct PrefabSpawner : IComponentData
    2. {
    3.     public float SpawnsRemaining;
    4.     public float SpawnsPerSecond;
    5.     const int kMaxFlags = 10;
    6.     public fixed int SpawnsFlag[ kMaxFlags ];
    7.     const int kMaxBuffs = 15;
    8.     public fixed uint buffIds[ kMaxBuffs ];
    9.     public int animIsEmote;
    10. }
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,882
    How do you define "best"?

    You have five fields in that struct, that isn't "many".
    Do you need the full 32-bits for the two fixed fields? If not, you should use the smallest datatype, ie byte or ushort. Can't imagine you're going to exceed 65,536 types of unique buffs in your game, and probably don't need 32 different spawn flags per entity.

    Btw, those const technically aren't fields, they will not be part of the entity's memory chunk.

    animIsEmote sounds like it should be a bool, or could maybe even be a bit in the flags field?
    SpawnsRemaining sounds like a counter and if so, should be an integer type, unless it means "RemainingTimeUntilNextSpawn" or something

    Anyhow, I learned that by now it seems we can use fixed to declare Burst compatible arrays. Or am I mistaken?
     
    Last edited: Oct 16, 2022
  3. hellengoodd

    hellengoodd

    Joined:
    May 31, 2017
    Posts:
    51
    I would like to modify my question, the following code is written in C++, if I want to change to C# IComponentData, how would I change it?
    Code (CSharp):
    1. public struct Test
    2. {
    3.     public float member1;
    4.     public float member2;
    5.     const int kMaxMember3 = 10;
    6.     public int member3[kMaxMember3];
    7.     const int kMaxMember4 = 15;
    8.     public uint member4[kMaxMember4];
    9.     public int member5;
    10.     const int kMaxMember6 = 6;
    11.     public float member6[kMaxMember6];
    12.     const int kMaxMember7 = 8;
    13.     public byte member7[kMaxMember7];
    14. }
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    No it is not.

    Your first post looked valid, just very possibly not optimal for your use case. However, none of us here understand your use case, so we can't give you better suggestions.
     
  5. hellengoodd

    hellengoodd

    Joined:
    May 31, 2017
    Posts:
    51
    I just want to know if it was you, what would you write...

    Would you write that:
    Code (CSharp):
    1.     public unsafe struct Test : IComponentData
    2.     {
    3.         public float member1;
    4.         public float member2;
    5.         const int kMaxMember3 = 10;
    6.         public fixed int member3[kMaxMember3];
    7.         const int kMaxMember4 = 15;
    8.         public fixed uint member4[kMaxMember4];
    9.         public int member5;
    10.         const int kMaxMember6 = 6;
    11.         public fixed float member6[kMaxMember6];
    12.         const int kMaxMember7 = 8;
    13.         public fixed byte member7[kMaxMember7];
    14.     }
    or this?
    Code (CSharp):
    1.     public struct Test : IComponentData
    2.     {
    3.         public float member1;
    4.         public float member2;
    5.         public NativeArray<int> member3;
    6.         public NativeArray<uint> member4;
    7.         public int member5;
    8.         public NativeArray<float> member6;
    9.         public NativeArray<byte> member7;
    10.     }
    or this?
    Code (CSharp):
    1.     public struct Test : IComponentData
    2.     {
    3.         public float member1;
    4.         public float member2;
    5.         public BlobArray<int> member3;
    6.         public BlobArray<uint> member4;
    7.         public int member5;
    8.         public BlobArray<float> member6;
    9.         public BlobArray<byte> member7;
    10.     }
    or other?
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    I take into account how the data would be used before writing it. Since you aren't providing me that information, I would delete the code.

    Suppose in some alternate universe you provided me such information, there is a very slim chance I would write A. Most likely I would do something different.

    I wouldn't write B. I would use a feature in my framework called Collection Components if I wanted to use safety-tracked containers.

    C is illegal. BlobArrays may only live in blob storage.
     
  7. hellengoodd

    hellengoodd

    Joined:
    May 31, 2017
    Posts:
    51
    Thank you for your answer, but I still can't understand it without any example...