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.
Sets a random seed for the game.
Random Numbers
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.
Pop an instance of this in a room and test, each time you press spacebar, two new numbers will be chosen.
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
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);
This will choose a random number between 0 and 50, for example: 23.476 (this could contain more digits after the decimal point).
would return 3, as it rounds to the nearest whole number.
This will choose a whole number integer between 0 and 100 inclusive.
would set a whole number between 20 and 30 inclusive.
forces the seed to 123. Ideal at the start of a random level.
Basic Projects
- 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.
- B)
Make a single instance that randomly changes direction when a random alarm event triggers. Make the instance wrap around the room.
Advanced Project
- 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
Summary
You’ll have the ability to create variation within your game using various random functions.