Faster way to reset 2d arrays?

blisstix5

Member
Is there a faster way to set all values on a 2d array to -1? I'm using it to store IDs on a 1024x1024 grid but when they all need to be reset every few seconds to -1 the FPS drops. Maybe a map or grid data stucture would be better?
Code:
//reset array
gridSize=1024;
for(var row=0;row<gridSize;row++) {
    for(var column=0;column<gridSize;column++) {
        
        myArray[row,column] = -1;       
    }

}
 

TheouAegis

Member
array_create(gridSize, array_create(gridSize,-1))

That's the only way I can think of, but it creates nested 1D arrays, not a standard 2D array. Or you try to use a default value of 0 instead and just use the default initialization.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Depending on your target platform, try initializing the largest index first. Otherwise you'll end up resizing the grid with every addition, which can be slow.

Alternatively, keep a separate copy of the array and copy it over to this one when you need to reset it - that may be faster.
 

blisstix5

Member
Depending on your target platform, try initializing the largest index first. Otherwise you'll end up resizing the grid with every addition, which can be slow.

Alternatively, keep a separate copy of the array and copy it over to this one when you need to reset it - that may be faster.
Yeh this seems this fasted way , copying it over when reset needed
 
Top