choose

Randomly chooses one of of a set of arguments.

Syntax:

choose(val0, val1, val2... max_val);


Argument Description
val0... max_val An input value that can be string, integer, variable or constant.


Returns: One of the given arguments.


Description

Sometimes you want to specify something other than numbers for a random selection, or the numbers you want are not in any real order or within any set range. In these cases you would use choose() to generate a random result. For example, say you want to create an object with a random sprite at the start, then you could use this function to set the sprite index to one of a set of sprites. Note that you can have as many as you require (more arguments will mean that the function will be slower to parse).

NOTE: This function will return the same value every time the game is run afresh due to the fact that GameMaker: Studio generates the same initial random seed every time to make debugging code a far easier task. To avoid this behaviour use randomize at the start of your game.


Example:

sprite_index = choose(spr_Cactus, spr_Flower, spr_Tree, spr_Shrub);
hp = choose(5, 8, 15, 32, 40); name = choose("John", "Steven", "Graham", "Jack", "Emily", "Tina", "Jill", "Helen");

The above code uses choose to set a number of properties for the instance.


Back: Real Number Functions
Next: random
© Copyright YoYo Games Ltd. 2018 All Rights Reserved