Book Image

Gamemaker Essentials

4 (1)
Book Image

Gamemaker Essentials

4 (1)

Overview of this book

Table of Contents (16 chapters)

Simple toggles


When debugging a game, it's often useful to be able to enable or disable things with the press of a button. Things such as lasers, turrets, and pausing the game can all be done with just the press of a button and a single line of code.

For this, we will use the not statement, which is also represented by an exclamation mark (!). The not statement is often used in an if statement to check whether something is false instead of true. The way it works is by simply multiplying the returned result by -1, making it negative if the number was positive or positive if the number was negative.

We can use this to create toggles. Here is some example code:

if (keyboard_check_released(vk_space)){
    on= !on;
}

The preceding code checks to see whether the player has released the spacebar key and if so, it will set the on variable to be the opposite of what it was before. This works by setting on to !on. In other words, by multiplying on with -1.

This code will work the exact same way:

if (keyboard_check_released...