Repeat

The Game Maker repeat is one of the different loops in Game Maker. It's syntax is as follows: repeat( ) {      }

The repeat loop repeats the   a   of times. No infinity-loops can occur, but the problem with the repeat loop is that it loops for a constant number.

Example
Let us say that we are making a game, and in it we want to have 10 trees in random places. There are two options we could take:

We could A write (,,obj_tree); 10 times, or B we could use the repeat loop to have the same statement executed 10 times by writing the following code: repeat (10) { instance_create(random(room_width),random(room_height),obj_tree); }

This code creates an instance of obj_tree at a random x position between 0 and room_width and at a random y position between 0 and room_height. What's more is that it repeats this 10 times. Now say that we actually want 15 trees all we have to do is change 10 to 15 and we now have 15 trees.

One of the most powerful uses of the repeat loop is using it in conjunction with the random function. Perhaps we want a random amount of trees. We can do that by putting a random function in the place of 10. Creating a random amount of trees is something which would have been horribly more difficult without using the repeat statement (and other more powerful statements).

Final Notes
The repeat statement is a simplified version of the other loops. Instead of needing a count variable to keep track of the number of iterations the loop has gone through, it takes care of that. The only downside to that is without using a count variable you do not know at any time which iteration it is currently at. Also, you can break out of the repeat loop like any other loop using the break statement, but, generally speaking, the use of the break statement would eliminate the reason you are using this loop statement over another more powerful loop statement.