Legacy GM array problem

G

graviax

Guest
Code:
if(level_finish != 1)
{
    for(i=0;i<50;i++)
    {
        for(j=0;j<50;j++)
        {
            if(random_range(0,10)>8)
            {
                map[i,j] = 1;
            }
        }
    }
    for(i=0;i<50;i++)
    {
        for(j=0;j<50;j++)
        {
            if(map[i,j] == 1)//the joke
            {
                instance_create(i*16,j*16,obj_wall);
            }
        }
    }
    level_finish = 1;
}
so this is the code
and this is the error.
error.png
and I don't know how it can find a 51 between 0 and 50.
and the first double for loop seems to work so...
 

jo-thijs

Member
The [51, 46] is the range of your array.
51 is the first index that is invalid.

The index you get an error on is [0, 46], as there is no value placed there.
You set map[i, j] to 1 at random in the previous loops, but you don't set it to 0 otherwise.
 
T

TDSrock

Guest
The [51, 46] is the range of your array.
51 is the first index that is invalid.

The index you get an error on is [0, 46], as there is no value placed there.
You set map[i, j] to 1 at random in the previous loops, but you don't set it to 0 otherwise.
Pretty much this but to add a bit more information. You've essentially got some non declared values in your array. So if [50,50] is never initialized then you'll have a smaller array then [50,50] if [50,50] is initialized you'll have an array with tons of 1's and some null values. Adding an else statement that sets every slot to 0 will avoid both issues(the second might be an issue later if you decided to say [i,j] + 4. As you can't do any mathematical operations with null values.
 
Top