Game Development Reference
In-Depth Information
Writing a script
Writing a script is just like writing any other code. GML functions are the exact same in a
script as they are in an object; the only difference is the script is global to all objects and
not locked to any specific one. With this in mind, it is important not to accidentally try and
use a variable from an object.
We want to add two numbers together and then display a message to the player with the an-
swer. This can be done in one line of code; however, we will be extending it slightly to
make it more readable, and also easier to edit later on.
This is the code we will use:
There are four lines of code in the preceding screenshot, each of which take the entire pro-
cess one step at a time. Let's walk through these steps and see what's going on.
1. On line one, we create a new variable called
n1
. This variable is set to
5
, and
n1
stands for number one.
2. On line two, we repeat line one except with a variable called
n2
, which stands for
number two. We set this variable to
10
.
3. On line three, we create another variable named
answer
and we set its value to be
equal to
n1 + n2
, which would set this variable to
15
. This is where our equa-
tion takes place.
4. Finally, we show the result to the player using the
show_message
function.