• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED For Loop problem

Imperial

Member
GML:
function search_change_engine()
{
    var xoffset = 320;
    var yoffset = 48;
    var spacing = 95;
    var xx = room_width/2;
    var yy = 225;
   
    var dialog = new UI_Dialog("page 1",{family: font_config_arabic_lg});
    global.ui_elements[0] = new UI_Button(xx - xoffset,yy + yoffset,"page 1");
    global.ui_elements[1] = new UI_Button(xx - xoffset,yy + spacing + yoffset,"page 1");
    global.ui_elements[2] = new UI_Button(xx - xoffset,yy + (spacing * 2) + yoffset,"page 1");
    global.ui_elements[3] = new UI_Button(xx - xoffset,yy + (spacing * 3) + yoffset,"page 1");
   
    global.ui_elements[4] = new UI_Button(xx,yy + yoffset,"page 1");
    global.ui_elements[5] = new UI_Button(xx,yy + spacing + yoffset,"page 1");
    global.ui_elements[6] = new UI_Button(xx,yy + (spacing * 2) + yoffset,"page 1");
    global.ui_elements[7] = new UI_Button(xx,yy + (spacing * 3) + yoffset,"page 1");
    global.ui_elements[8] = new UI_Button(xx,yy + (spacing * 4) + yoffset,"page 1");
    global.ui_elements[9] = new UI_Button(xx,yy + (spacing * 5) + yoffset,"page 1");
   
    global.ui_elements[10] = new UI_Button(xx + xoffset,yy + yoffset,"page 1");
    global.ui_elements[11] = new UI_Button(xx + xoffset,yy + spacing + yoffset,"page 1");
    global.ui_elements[12] = new UI_Button(xx + xoffset,yy + (spacing * 2) + yoffset,"page 1");
    global.ui_elements[13] = new UI_Button(xx + xoffset,yy + (spacing * 3) + yoffset,"page 1");
    global.ui_elements[14] = new UI_Button(xx + xoffset,yy + (spacing * 4) + yoffset,"page 1");
   
    for(i = 0; i < array_length(global.ui_elements); i++)
    {
        global.ui_elements[i].callback = function()
        {
             global.opt_btn.sprite = asset_get_index("page 1" + res_get_type(global.display_width));
        }
    }
   
    dialog.add(new UI_Text(xx - xoffset,152 + yoffset,"page 1"));
    dialog.add(new UI_Text(xx,152 + yoffset,"page 1"));
    dialog.add(new UI_Text(xx + xoffset,152 + yoffset,"page 1"));
    dialog.add(global.ui_elements);
   
    global.ui_can_scroll = 1;
}
Execution Error - Variable Index [15] out of range [15] - -5.ui_elements(100008,15)

what I'm doing wrong in your opinion
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
You're trying to access element 15 when the range is 15. Arrays start at 0, so the highest element you can access is 14.

Somewhere in your code, you're trying to access an element beyond highest element you can access, e.g. a loop that runs from 0 to 15.
 
Last edited:

TheouAegis

Member
But is it the for loop? The loop is LESS than 15, so it only reads up through index 14. I think the error is pointing outside the loop somewhere that references I.
 

Imperial

Member
But is it the for loop? The loop is LESS than 15, so it only reads up through index 14. I think the error is pointing outside the loop somewhere that references I.
But the For Loop condition is i < array_length(global.ui_elements); , so the iteration should stop at 14, do you agree ?

@TsukaYuriko do you agree ?
 

TsukaYuriko

☄️
Forum Staff
Moderator
I analyzed the error message, not the code. The error message is clear in that you're trying to access element 15, which doesn't exist, so some of your code has to do that - not necessarily this code. (Edit: There's actually some inconsistency in my original reply. Will edit. Thanks for pointing that out.)

If it's not caused by this code, it's caused by some other code. No way for me to tell because you didn't post the full error message that includes the part that tells you what's going wrong where.
 
Last edited:

FrostyCat

Redemption Seeker
The most obvious problem I can see is the instance-scoped i. See this reply for an example of what can go wrong.
Variables holding intermediate, non-lasting values should always be put in local scope. Otherwise, these intermediate variables will risk crowding out other more long-lasting instance variables, or worse, trespassing each other across events or function calls. An example of such trespassing is demonstrated here:
GML:
function d6(n) {
    total = 0;
    for (i = 0; i < n; i++) {
        total += irandom_range(1, 6);
    }
    return total;
}
GML:
rolls = array_create(50);
for (i = 0; i < 50; i++) {
    rolls[i] = d6(2);
}
Do you think the above fills a 50-entry array with the results of 50 double-dice rolls? Wrong answer! It crashes flat out, and the code doesn't even touch most of the 50 slots. The i in the d6 is actively overwriting the i from the outside for loop, making it impossible to go beyond 2, let alone get anywhere near 50 --- the classic condition for an infinite loop.

Changing i = 0; and total = 0; (instance scope) to var i = 0; and var total = 0; (local scope) in both pieces is all that's needed to keep the two i variables apart and prevent the crash. Yet Little Town doesn't seem to think it's important to teach that. You are bound to write something similar to the above if you are as casual about variable scope as Little Town's author is, and most people would have no idea what's going on.
If you do not want the i in your code to interfere with variables elsewhere also named i, use var.
GML:
for(var i = 0; i < array_length(global.ui_elements); i++)
 
Just put var in front of your i, when calling your loop.
Do this in every loop you have, otherwise, you'll most be likely have a lot of these coming your way!
for(var i=0; i<_len; i++){ ... }

I don't know if it's THIS piece of code that's crashing, tho, you should look it all over with *search and replace*
 

gnysek

Member
Error message in first post was missing line and column in which this error happened, that could be a great help in future, as it directly pointed in which line you made this error...
 
Top