• No results found

Chapter 28 The Game Maker Language (GML)

28.7 Arrays

You can use 1- and 2-dimensional arrays in GML. Simply put the index between square brackets for a 1-dimensional array, and the two indices with a comma between them for 2-dimensional arrays. At the moment you use an index the array is generated. Each array runs from index 0. So be careful with using large indices because memory for a large array will be reserved. Never use negative indices. The system puts a limit of 32000 on each index and 1000000 on the total size. So for example you can write the following:

{

a[0] = 1;

i = 1;

while (i < 10) {a[i] = 2*a[i-1]; i += 1;} b[4,6] = 32;

}

28.8 If statement

An if statement has the form

if (<expression>) <statement>

or

if (<expression>) <statement> else <statement>

The statement can also be a block. The expression will be evaluated. If the (rounded) value is <=0 (false) the statement after else is executed, otherwise (true) the other statement is executed. It is a good habit to always put curly brackets around the statements in the if statement. So best use

if (<expression>) { <statement> } else { <statement> } Example

{

if (x<200) {x += 4} else {x -= 4}; }

28.9 Repeat statement

A repeat statement has the form

repeat (<expression>) <statement>

The statement is repeated the number of times indicated by the rounded value of the expression.

Example

The following program creates five balls at random positions.

{

repeat (5) instance_create(random(400),random(400),ball); }

28.10 While statement

A while statement has the form

while (<expression>) <statement>

As long as the expression is true, the statement (which can also be a block) is executed. Be careful with your while loops. You can easily make them loop forever, in which case your game will hang and not react to any user input anymore.

Example

The following program tries to place the current object at a free position (this is about the same as the action to move an object to a random position).

{ while (!place_free(x,y)) { x = random(room_width); y = random(room_height); } }

28.11 Do statement

A do statement has the form

do <statement> until (<expression>)

The statement (which can also be a block) is executed until the expression is true. The statement is executed at least once. Be careful with your do loops. You can easily make

them loop forever, in which case your game will hang and not react to any user input anymore.

Example

The following program tries to place the current object at a free position (this is about the same as the action to move an object to a random position).

{ do { x = random(room_width); y = random(room_height); } until (place_free(x,y)) }

28.12 For statement

A for statement has the form

for (<statement1> ; <expression> ; <statement2>) <statement3>

This works as follows. First statement1 is executed. Then the expression is evaluated. If it is true, statement 3 is executed; then statement 2 and then the expression is evaluated again. This continues until the expression is false.

This may sound complicated. You should interpret this as follows. The first statement initializes the for- loop. The expression tests whether the loop should be ended. Statement2 is the step statement that goes to the next loop evaluation.

The most common use is to have a counter run through some range.

Example

The following program initializes an array of length 10 with the values 1-10.

{

for (i=0; i<9; i+=1) list[i] = i+1;

}

28.13 Switch statement

In a number of situations you want to let your action depend on a particular value. You can do this using a number of if statements but it is easier to use the switch statement. A switch statement has the following form:

switch (<expression>) {

case <expression1>: <statement1>; … ; break; case <expression2>: <statement2>; … ; break; …

}

This works as follows. First the expression is executed. Next it is compared with the results of the different expressions after the case statements. The execution continues after the first case statement with the correct value, until a break statement is encountered. If no case statement has the right value, execution is continued after the default statement. (No default statement is required. Note that multiple case statements can be placed for the same statement. Also, the break is not required. If there is no break statement the execution simply continues with the code for the next case statement.

Example

The following program takes action based on a key that is pressed. switch (keyboard_key) { case vk_left: case vk_numpad4: x -= 4; break; case vk_right: case vk_numpad6: x += 4; break; }

28.14 Break statement

The break statement has the form break

If used within a for-loop, a while- loop, a repeat- loop, a switch statement, or a with statement, it end this loop or statement. If used outside such a statement it ends the program (not the game).

28.15 Continue statement

The continue statement has the form continue

If used within a for- loop, a while- loop, a repeat-loop, or a with statement, it continues with the next value for the loop or with statement.

28.16 Exit statement

The exit statement has the form exit

It simply ends the execution of this script or piece of code. (It does not end the execution of the game! For this you need the function game_end();see below.)

In document Designing Games with Game Maker (Page 87-91)