• 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!

GML I cannot undestand how to use for loop.

E

Edwin

Guest
I am very dumb russian gopnik, so even after looking at the tutorials and manuals, i did not understood anything :(.

Could you please explain it for me, how stupid it would not sound, like for child?

This is from russian manual translated:
for loop first executes command 1, then checks the condition and if it is true, then executes command 3, after command 2, and then checks the condition again and if it is true, then executes command 3 again and then command 2.

My mind is blown up! Please help!
 

GMWolf

aka fel666
Code:
for(<expr1>; <expr2>; <expr3>) {
  <body>
}
is essentially the same as
Code:
<expr1>
while(<expr2>){

<body>
<expr3>
}
 
Code:
for (var i=0;i<10;i++) {
   show_debug_message(string(i));
}
Try that out in game maker and see what happens in your output console. A for loop takes a variable, performs a set of instructions (the code inside the for statement), then goes back to the start of the set of instructions, increments the variable by however much you've decided to increment it by and goes through the code block again, repeating until the variable passes some threshold you've set, then it exits the loop.
 
It's useful for cycling through lists, arrays, etc. As you've seen in the answers to your other questions, you can go through every value stored in an array using a for loop:
Code:
var array_size = array_length_1d(array); //Get the size of the array
for (var i=0;i<array_size;i++) { //First off, we initialise what value we want to start with, here i=0, then we tell the loop to stop after i >= array size, then we tell it to increment i by 1 each time it loops
  var number = array[i]; //First loop, i = 0, so we're accessing array[0], second loop i = 1, so we're accessing array[1], etc;
}
Some real power lies in nested for loops, as these let you loop through grid-like structures, rather than simple lists:
Code:
for (var xx=0;xx<room_width;xx+=32) { //Here we're starting at 0 and adding 32 each loop until we've gone past the room width
  for (var yy=0;yy<room_height;yy+=32) { //Same as above except with height
    draw_rectangle(xx,yy,xx+32,yy+32,true);
    /* On the first loop xx = 0 and yy = 0, so the rectangle will be drawn from 0,0 to 32,32
    On the second loop, xx = 0 and yy = 32, why? Because whatever for loop is the one inside will
    loop through fully before the outer for loop increments at all. So basically, on a grid, it will loop
    through 0,0   0,32    0,64    0,96 until it hits the height of the room, then it will become
    32,0    32,32     32,64     32,96, etc, thus passing through (and drawing a rectangle) at every 32x32
    point in the room. */
  }
}
 
P

Pyxus

Guest
I am very dumb russian gopnik, so even after looking at the tutorials and manuals, i did not understood anything :(.

Could you please explain it for me, how stupid it would not sound, like for child?

This is from russian manual translated:
for loop first executes command 1, then checks the condition and if it is true, then executes command 3, after command 2, and then checks the condition again and if it is true, then executes command 3 again and then command 2.

My mind is blown up! Please help!
I think the explanations above are great and hopefully they help you, but since you asked for it to be explained like a child Ill see if I can simplify it even further.

The for loop declaration
for (statement1; expression; statement2) statement 3;
- statement1: This statement initializes the for loop, typically var i = 0 is used, however you can use any variable at any value to initialize the loop. var k = 12, var pizza = 2, etc.
- expression: The expression determines how long the loop should occur, any expression can be used here. i<(something), i >(something), i=(something).
- statement2: This is the "step" that increments the loop so that it can perform each evaluation. i++, ++i, i--

In english you could read the for loop a bit like this:
for (variable i starting at a specific value; if variable i is less than the set value; increase the value of i by the stated amount) {
}

You can access the current value of i within the loop, which makes it useful for cycling through arrays and list, this is partially why a lot of for statements initialize at 0, since data structures start counting from 0.
 
Last edited by a moderator:

TheouAegis

Member
Now let's take things a step further.

Statement1 can be almost anything as long as it is all completely inside one block of code. A block is defined as a single statement, such as "var i=0;" or a script/function call, or a set of code contained within { and }, such as "{var i; i=0;" but you don't specifically have to declare variables here.

Expression can be a simple comparison, like "i<10;” or a complex, compound conditional, like "(x<room_width y<room_height) || instance_count<20;" but all that matters is it gives a true or false answer. When that answer is false, the loop will stop running, although this is not the only way to stop the loop. The expression is checked for each iteration of the loop, which makes it possible to create endless loops (this does not happen with repeat loops, but it can also happen in while loops). This also means if the expression calls any scripts/functions or indirectly refers to variables, it may be good practice to store the results of that expression or variable in a temporary variable first, which can be done in statement1. Just remember expression is run immediately after statement1.

Statement2 is the same deal as statement1. Typically this is some code that helps move the loop along directly, such as changing a variable in the expression, but it can be any code you want, as long as it is all contained in one block. It doesn't have to be related to statement1 nor expression.

You can skip the current step in the loop by using continue; which forces the loop to jump directly to the expression.

You can end the loop immediately by using break; which will stop the loop where it is at immediately.

Code:
for({draw_set_color(c_white); var foo=noone, bar=(current_second+45) mod 60;} current_second!=bar || foo>0; {x++; if x>room_width {x=0; y=++y mod room_height;}}) 
{
    foo = instance_place(x,y,all);
}
 
Top