Function (Game Maker)

A function in Game Maker is a set of code that accomplishes a specific task. There are no user-defined functions in Game Maker, all functions are built-in. To make your own functions, you have to use scripts.

Basic Functions

The most simple syntax for a function is function_name(). These functions require no arguments and return nothing. One such function is instance_destroy(), which destroys the current instance.

instance_destroy();

Arguments

Some functions require arguments. Arguments are pieces of data that a function requires to work. Literals, constants, and variables can be used as function arguments. For example, draw_set_color() requires a color (in this example, a constant) :

draw_set_color(c_white);

A function can require more than one argument :

draw_text(100,100,"This is located at 100,100");

The arguments in the above example are literals.

Finally, a variable can be used as an argument :

message = "This is an argument in a variable";
show_message(message);

Return Values

Some functions return values. These values can be assigned to a variable or used an in if statement :

rand_numb = random(19);

In this example, random() generates a real number between and 18.999. Then, that number is assigned to the variable rand_numb.

if (random(50) < 20)
{
    show_message("Random is less than 20");
}

In this example, random() generates a real number between and 49.999. Then, that number is compared to 20 to see if it less than ( < ) 20. If it is, show_message() shows a message.

Nested Functions

Functions can be nested, that is, they can be used to create the arguments used by other functions. For example, random() generates a real number. But if you wanted to generate an integer you will have to use floor() to convert the real number to an integer. We could do this :

rand_numb = random(52);
rand_numb = floor(rand_numb);

This is the long way of doing it. By nesting functions, you can save time and make your code slightly easier to read :

rand_numb = floor(random(52));

This works because the first function to execute is the one in the "center". This is how this resolves :

rand_numb = floor(random(52));
// first, random() generates a random number
rand_numb = floor(34.509);
// then, floor coverts that number to an integer
rand_numb = 34
// finally, it is assigned to rand_numb, which now contains the 
// integer 34

Related Pages

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.