# How to give a value to all elements of an array?

This problem is encountered in Game-maker studio:

I created an array using the following iteration:

V_X=0 //these are coordinates of my "pointer"
V_Y=0

repeat(100)
{
A_CA[V_X,V_Y]=choose(-1,0,1) //now I declare the value of the pointed element in the array

if V_X<10 //here I move the "pointer"
{
V_X+=1
}
if V_X=10
{
V_Y+=1
V_X=0
}
}


My goal is, at some point in the game, I would like to give the same value (0) to every element of the array, preferably without using the "repeat" command above, because I think that may hamper performance.

Firstly, you need use for, not repeat. In this case for will works more fast.

If array not initialized (not created), you need fill it from end to begin. Otherwise it will reallocate memory on each record, so it will work very slow (for any JS-targets, like HTML5/Tizen/etc, it's not true - there you need use normal order).

for (var i=9; i>=0; i--)
{
for (var j=9; j>=0; j--)
{
array[j, i] = value;
}
}


If array already initialized, then order is not important.

No ways to fill array by value except iterate each element in loop. But 100 elements - it's not much. No reason for worry about it.

Other way - you can use grids and function ds_grid_clear for fill it.