Game Development Reference
In-Depth Information
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(vk_space)){
on*=-1;
}
These toggles can be applied to anything that needs a variable to turn on and off. They be-
come extremely useful when debugging a game as they allow the programmer to enable
and disable parts of the game at any time and see exactly where the issues are taking place.