NativeArray Allocation Type Question

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

It’s depend. But for your case, allocate it in oncreate and dispose it in ondestroy.

1 Like

Why shouldnt i use Allocator.Persistance? why i destroy it?

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.

1 Like

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?

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.

1 Like

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

1 will be fine

1 Like

Thank you very much :slight_smile: