© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
B. TyersGameMaker Fundamentalshttps://doi.org/10.1007/978-1-4842-8713-2_20

20. Loops

Ben Tyers1  
(1)
Worthing, West Sussex, UK
 

A common need in programming is to repeat the same code multiple times. This functionality is available through using loops. There are four main loop functions in GML: do, while, for, and repeat. Each of these has its own strengths and weaknesses.

Loops are great for
  • Processing data in ds lists or arrays.

  • Performing the same action multiple times.

  • Performing a calculation until it is true.

  • Drawing data from lists, grids, or arrays.

A loop can result in many similar actions being executed in a very short time.

Note

It is important to make certain that loops run the correct number of times and then stop. It is possible through a coding mistake to create an infinite loop that runs forever. This will cause the game to stop working. Care needs to be taken to avoid this from happening.

Also note that a loop will complete itself within one frame of the game. If you are using large loops or using lots at a time, another approach may be required. For example, if you looped through a block of code 1000 times within a single frame, it may have undesired effects such as making your game stutter.

Repeat Loop

repeat can be used to process the same code block several times at once. For example:
repeat(5) //Will repeat the following block five times
{
      effect_create_above(ef_firework,100,100,2,choose(c_white,c_yellow,c_green,c_blue));
}

which would create five firework effects at once.

While Loop

A while loop will repeat until an expression is true or not true, for example:
while !place_free(x,y)
{
      x = random(room_width);
      y = random(room_height);
}

This would repeat until a free space is found.

Note

Ensure this loop will exit (complete) at some point, if not it will run continuously and freeze your game. As in the preceding example, ensure there are free places within the room.

For Loop

A for loop is used when you want to increment a value and perform an action. For example, in the following, where it will populate an array with the values 0 to 24 inclusive.
for (var i=0;i<25;i++)
{
      array[i]=i;
}

The preceding code will loop from 0 to 24 and then exit. It will place the numbers 0 to 24 within the array.

If you wanted to include the value 25 in the loop

you can use: i<=number

For example:
for (var i=0;i<=25;i++)
{
      array[i]=i;
}

which would populate the array with values 0 to 25 inclusive.

You could draw the contents in reverse order with
for (var i=25;i>=0;i--)
{
      draw_text(200,20+(25*i),array[25-i]);
}

Do Loop

Like a while loop, but will execute at least once until an expression returns as true:
do
{
      x = random(room_width);
      y = random(room_height);
}
until (place_free(x,y));

will try random positions until a free space is found.

Note

The do loop will always execute at least once, but in for or while, it can be skipped.

Basic Projects

  1. A)

    While loop. Set an object that randomly chooses a random position within 100 pixels of another object. Allow spacebar to find a new location.

     
  2. B)

    For loop. Make a list with 100 numbers in random order from 1 to 1000. Display this data on screen in four columns, in ascending order. Allow spacebar to choose a new set.

     

Advance Projects

  1. C)

    Set four random points in the room. Make an object visit each of these locations. Spacebar to restart.

     
  2. D)

    Make a system that takes in all the names of students in your class. Display onscreen in alphabetical order, one at a time every five seconds.

     

Useful Functions

If you want to gradually increase a value, you can use something like:
variable++;
In a Step Event
break

There will be times when you may wish to exit a loop early, for example, when a certain condition or value is met. break allows to do this.

Summary

You now are now aware of some appropriate uses of loops and how they can be implemented. You understand how to add, change, process, and display data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.38.24