Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NativeArray Allocation Type Question

Discussion in 'Entity Component System' started by jGate99, Oct 12, 2019.

  1. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,936
    Hi there,

    I have a simple grid based game i coded, worked well and now i started replacing it with Job System (without ECS and Burst). Currently i declare a 100x100 2d grid int[,] and keep it memory for reusing where grid[0,0] = 0 means this cell is empty and grid[0,0] = 1 means this cell is occoupied
    and then i run different loops to see which area is occupied and which is free etc etc


    So now i want to use a Job for that and job works on NativeArray which means ill have ot use a 1D Array. So my question is should i use Allocation.Persistance (as currently i insantiate and keep that grid in memory whole time) or i use Allocation.TempJob (Creating Destroying in each game session)

    Thanks
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    It’s depend. But for your case, allocate it in oncreate and dispose it in ondestroy.
     
    jGate99 likes this.
  3. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,936
    Why shouldnt i use Allocator.Persistance? why i destroy it?
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    You should use Persistent allocator, it’s exactly what I told. You allocate it in system creation and dispose only when system destroying. Cause it will be long lived container, there is no reason in allocating/deallocating every frame.
     
    jGate99 likes this.
  5. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,936
    Sorry for the confusion, i dont want to dellactore every frame.

    Let me try to explain further

    Original game initalize the 2d array and keep it in memory for the whole lifetime of the game because user will select level, this array will use during gameplay, user win/lose, user select other level then this array be in use for the rest of gameplay session.

    So coming from that experience, i "assume" that

    1- using persistance array which will remain there for whole application life is right thing

    OR

    2- should i create it fresh every time a level is start, reuse during level, and destroy coming out of level


    So i want to know which'd be better approach for a mobile game?
     
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Again it’s depend on many circumstances. Possible and fine - both. It’s depends on size of array, how many time it require for allocation, fine this time between levels for you or not, you need flush data or not etc. etc.
     
    jGate99 likes this.
  7. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,936
    As people will b spending most of their time in gameplay, so i "assume" that i should take [1] approach.
    But i want to be sure as thiis whole paradigm is new for me
     
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    1 will be fine
     
    jGate99 likes this.
  9. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,936
    Thank you very much :)