Creating Scripts
Scripts are amazing! They are also easy to use! If you have ever programmed in a different language, a script is similar to a function, in that it allows you to write a block of code once and then run that block of code multiple times by executing the script it is contained in. One of the interesting things about scripts is that they are global in scope.
When I say global, I mean that if you create a script, any object in your game could call that script. If you see lots of repeated code in your game, then it might be a good idea to create a script for that code and then just call the script whenever needed.
Creating a script is pretty easy. You can use the shortcut keys shift+ctrl+c to add a new script to your game, or you can click the page icon with the green play symbol on it.
After adding a new script, you will need to give the script its own identifier. Scripts, just like variables, have an identifier. Let’s create a script called scr_move_right. I’m going to put the name (identifier) of the script at the upper lefthand side of the code block so that you know it is a script and you know what it is called.
scr_move_right
/// scr_move_right() x+=4;
As stated before, the scr_move_right on the upper lefthand side of the code block is the actual identifier for the script. In GameMaker, you will place the identifier for the script in this location. The triple slash comment has a special meaning at the top of a script file, as I described in Chapter 1. This comment provides the autocomplete
functionality in GameMaker and can also be used to tell you the order of arguments that need to be passed to the script. The last line in the script simply moves the player four pixels to the right. This basic script isn’t super useful, but let me show you one that I use quite often in my projects.
scr_get_depth
///scr_get_depth() depth=-y;
This is a neat little script that will give objects that are closer to the bottom of the screen a lower depth (meaning that they will be drawn on top of objects that are closer to the top of the screen). This gives the game a sense of depth and, when used correctly, can create a cool visual effect.
Script Arguments
Just like functions, scripts can be passed arguments. Let’s upgrade the script we wrote in the last section from scr_move_right to scr_move and pass it some arguments.
scr_move
/// scr_move(xmov,ymov) x+=argument[0];
y+=argument[1];
There is an array in the local scope of each script called argument. It contains the values of the arguments that were passed to the script in the order in which they were passed. The first value passed in will be assigned to argument[0], the second to argument[1], and so on.
I usually assign these values to local variables in order to help with the readability of the script.
scr_move
/// scr_move(xmov,ymov) var xmov=argument[0];
var ymov=argument[1];
x+=xmov;
y+=ymov;
It may seem a little redundant in this particular script (and maybe it is), but in a script where the argument is being used in multiple places, it is easier to understand a named variable than the generic argument[0].
Scripts Return Values
Scripts can also return a value. This means that you can pass data to the script, do something with that data to get a result, and then have that result pop back out. For example, you might create a script like this.
scr_add
///scr_add(n1,n2);
return argument[0]+argument[1];
You might not understand all of what is going on here, but know that this script takes two arguments (values) and then returns the sum. After creating this script, you could call it like this:
obj_add_button: Mouse Pressed Event
sum=scr_add(5,7);// sum now holds the value 12
As you can see, this script will actually return the result of the two arguments added together by the script.
Executing Scripts
Let me show you how you can call a script inside your code. There are two ways to do so. The easiest way is to write the identifier for the script followed closely by two parenthesis. These two parenthesis are called the script call operator; they will cause the script to be executed.
scr_get_depth();
Let’s look at the second way you can call a script. This way involves a new function inside GameMaker Language called the execute_script function. Make sure that when you pass the script into this function you do not add the brackets to the end.
execute_script(scr_get_depth);
Now, you might be wondering why on earth you would ever use this when it takes longer to type. The benefit of this method is that you can run a script that has been assigned to a variable.
var my_script = scr_get_depth; execute_script(my_script);
This is useful when you have a parent object that runs different scripts for each of its child objects (see chapter 8 for more information on the child/parent relationship in objects) depending on what you want them to do. Make sure that you don’t use the script call operator when assigning the script to a variable or executing it inside the execute_script function.
Also, note that when you assign a script to a variable (as seen in the code above) you do not use the brackets on the end of the script. The brackets represents the script call operator or function call operator; therefore, they would cause GameMaker to execute the script and attempt to assign its returned value to the variable, instead of assigning the script’s id to the variable.
This chapter on scripts has been short and sweet, but hopefully you learned something from it that you can apply in your future projects. I love using scripts to help organize my code, and I hope that you can find them useful as well.