Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question array in Component

Discussion in 'Entity Component System' started by somebodySB, Aug 4, 2023.

  1. somebodySB

    somebodySB

    Joined:
    Nov 30, 2018
    Posts:
    13
    I want to use array in Component like this:

    Code (CSharp):
    1.     public struct WarehouseComponent : IComponentData
    2.     {
    3.         public int[] goods;
    4.     }
    and use it in system like this:
    Code (CSharp):
    1. private void Merge(WarehouseComponent warehouseA,int[] newGoods)
    2. {
    3.     for (int goodsId = 0; goodsId < 20; goodsId++)
    4.     {
    5.         warehouseA.goods[goodsId] += newGoods[goodsId];
    6.     }
    7. }
    As I learned,When you need array in component,you should use DynamicBuffer instead.However,DB is like list but array.You can't query an index out of count,but there are few goods type in each entity.If I use DB,I can't query goods by goodsId as Index.
    Inititalizing all goods as 0 count after adding DB to entity is one choice but I want to know other better solution.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,976
    For very tiny arrays with tiny elements, you can use FixedList or fixed arrays. However, seeing that loop of 20 ints which is 80 bytes (larger than LocalToWorld), you'd be better off with a DynamicBuffer and an [InternalBufferCapacity(0)]
     
  3. somebodySB

    somebodySB

    Joined:
    Nov 30, 2018
    Posts:
    13
    in fact,the struct of goods in my game is more than int.It cost 48 bytes per goods and there are 26 different types of goods.I learned this skill "[InternalBufferCapacity(0)]" just yesterday,and I tried it.
    However,I don't understand why I should control the size of entity cause I can use not more than 16k(a chunk size)for one entity.
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,976
    If the dynamic buffer is stored outside the chunk (forced with that attribute), then it doesn't count towards that 16k.
     
  5. somebodySB

    somebodySB

    Joined:
    Nov 30, 2018
    Posts:
    13
    I know this point.But I don't konw if there is other advantage of small entity.
     
  6. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    If you want a hashmap instead (like for an inventory logic), you could use DynamicHashMap from https://forum.unity.com/threads/core.1340078/;
    Key & value can be anything as long as it is unmanaged. That will ensure order;

    Otherwise not changing DB capacity should keep indexes in the same order.
    Or just sort them at some point in frame with custom comparer.

    You'd want to utilize chunk memory to the maximum. Chunk allocation is slow, so overfilling chunk will cause chunk allocation and buffer moving out of the chunk.

    If you sure you've got only one large entity that fits in the chunk all the time -> keep it in chunk. Otherwise - move DB away. In anycase, this can be done later on once you actually have all game related data attached and complete size is determined.
     
    Last edited: Aug 4, 2023
    somebodySB likes this.
  7. somebodySB

    somebodySB

    Joined:
    Nov 30, 2018
    Posts:
    13
    That helps a lot.Thank you again.