© 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_16

16. Random

Ben Tyers1  
(1)
Worthing, West Sussex, UK
 
Depending on your game’s design, you want the game to play exactly the same each time it is played. Or you may want it to be different every time, for example, in matching puzzle game. This can be achieved with the random function. Randomization can be used for a lot of things such as:
  • Choosing a random sound effect

  • Spawning an enemy at a random location

  • Adding diversity to your game

  • Selecting an effect to use

  • Making AI move/change direction

  • Choose an attack from a selection

  • Move an object to a new location

  • Level generation

Depending upon the design of your game, you want things to happen in the same sequence each time the game is played, or you may want some randomness.

GameMaker will always generate the same sequence of random results.

To make testing and debugging easier, if you don’t use the randomize() function, the game will have the sequence of randomness. Note that this does not apply to compiled games.

To force it to create different randomness, you can run the following code once at game start:
randomize();

Sets a random seed for the game.

Random Numbers

In your game, you may have something in a Create Event:
/// @description Set up
starting_x=irandom(800);
starting_y=irandom(400);

This would set a whole random value for starting_x between 0 and 800 inclusive, and starting_y between 0 and 400.

Now each time the game is run, starting_x and starting_y will have different random values.

Make an object and put the preceding code in a Create Event.

In a Step Event put:
/// @description Restart room
if keyboard_check_pressed(vk_space)
{
      room_restart();
}
And in Draw Event:
/// @description Draw values
draw_text(200,200,"Stating X "+string(starting_x)+" Starting Y "+string(starting_y));

Pop an instance of this in a room and test, each time you press spacebar, two new numbers will be chosen.

You can also choose from a sequence:
attack=choose(1,1,1,1,2,2,2,3);

This will choose a number at random. There will be a 50%(4/8) chance of getting a '1,' a 37.5%(3/8) chance of getting a '2,' and a 12.5%(1/8) chance of getting a '3’.

Other Random Uses

The preceding method is not just limited to numerical values, it can be used for colors or assets. You can, for example,
  • Choose a color – my_colour=choose(c_red,c_green,c_blue);

  • Choose a random sound – snd_explosion=choose(snd_exp_1,snd_exp_2);

  • Deciding which enemy to spawn – enemy=choose(obj_enemy_1,obj_enemy_2);

  • Selecting a music track – music_track=choose(snd_music_1,snd_music_2,snd_music_3);

You can choose a number at random:
number=random(50);

This will choose a random number between 0 and 50, for example: 23.476 (this could contain more digits after the decimal point).

If you are generating real numbers with decimals, the following is useful:
value=floor(8.8);
which rounds down a value, so would return 8.
value=ceil(6.7);
which rounds up a value, so would return 7.
value=round (2.7);

would return 3, as it rounds to the nearest whole number.

Often you will want to work with whole integers, this can be done with:
value=irandom(100);

This will choose a whole number integer between 0 and 100 inclusive.

You can also select a whole number within a range of numbers to choose from:
value=irandom_range(20,30);

would set a whole number between 20 and 30 inclusive.

In some games, which have the level design created randomly, you may want to set a seed to force the same sequence of random numbers. You can do this with, for example:
random_set_seed(123);

forces the seed to 123. Ideal at the start of a random level.

Basic Projects

  1. A)

    Set an object that jumps to a new position upon being clicked. Ensure the new location is no closer than 50 pixels to any border.

     
  2. B)

    Make a single instance that randomly changes direction when a random alarm event triggers. Make the instance wrap around the room.

     

Advanced Project

  1. C)
    Set a system that chooses six random lottery numbers from 49. Draw onscreen, inside a colored circle, colored depending on the number. Allow key X to choose a new random set of balls.:
    • -9 white

    • 10-19 blue

    • 20-29 green

    • 30-39 red

    • 40-49 yellow

     

Useful Functions

You can choose a random number within a range:
random_range();
You can also set a random seed to force a certain randomized sequence.
random_get_seed();

Summary

You’ll have the ability to create variation within your game using various random functions.

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

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