Script

Game Maker has a built-in programming language.
Once you become more familiar with Game Maker and want to use it to its fullest extend, it is advisable to start learning to use this language.
There are two ways to use the language.

  • First of all you can create scripts.
These are pieces of code to which you give a name.
They are shown in the resource tree and can be saved to a file and loaded from a file.
They can be used to form a library that extends the possibilities of Game Maker.
  • Alternatively, you can add a code action to some event and type a piece of code there.
Adding code actions works in exactly the same way as adding scripts except for two differences.
Code actions don’t have a name and cannot use arguments.
Also they have the well-known field to indicate to what objects the action should apply.
For the rest you enter code in exactly the same way as in scripts.

So we further concentrate on scripts in this chapter.

As stated before, a script is written with code in GML (the built-in programming language) and is meant to perform a particular task.
Scripts can takes input-variables called arguments (sometimes called parameters).
To execute a script from any event, you can use either the script action or you can use code.
In the Script-action you specify the script you want to execute, together with the up to five arguments.
If you use a script from within a piece of code it is done the same way you call a GM-function.
In that case you can use up to 16 arguments.

Scripts can return one value.
This is often used to build calculating methods (mathematical methods).
The return keyword is used for this.
No code after the return keyword is executed!
When a script returns a value, you can also use it as a function when providing values in other actions.

To create a script in your game, choose Create Script from the Resources menu.
The following form will pop up (in the example we already added a little script that computed the product of the two arguments).

(Actually, this is the built-in script editor. In the preferences you can also indicate that you want to use an external editor.)
At the top right you can indicate the name of the script.
You have a little editor in which you can type the script.
Note that at the bottom a list is shown of all functions, built-in variables, and constants.
This helps you find the one you need.
You can double click one to add it (or use <Ctrl>P for the same purpose.)
Showing this list can be switched off in the preferences.
The editor has a number of useful properties, many available through buttons (press the right mouse button for some additional commands):

Multiple undo and redo either per key press or in groups (can be changed in the preferences)

Intelligent auto indent that aligns with the previous line (can be set in the preferences)

Intelligent tabbing that tabs till the first non space in the previous lines (can be set in the preferences)

Use <Ctrl>I to indent selected lines and <Shift> <Ctrl>I to unindent selected lines

Cut and paste

Search and replace

Use <Ctrl> + up, down, page-up, or page-down to scroll without changing the cursor position
Use F4 to open the script or resource whose name is at the cursor position (does not work in the code action; only in scripts)

Saving and loading the script as a text file

Also there is a button with which you can test whether the script is correct.
Not all aspects can be tested at this stage but the syntax of your script will be tested, together with the existence of functions used.
As you might have noticed, parts of the script text are colored.
The editor knows about existing objects, built-in variables and functions, etc.
Color- coding helps a lot in avoiding mistakes.
In particular, you see immediately if you misspelled some name or use a keyword as a variable.
Color-coding is though a bit slow.
In the preferences in the file menu you can switch color- coding on and off.
Here you can also change the color for the different components of the programs.
(If something goes wrong with color coding, press F12 twice, to switch it off and back on.)
Also you can change the font used in scripts and code.

Scripts are extremely useful to extend the possibilities of Game Maker.
This does though require that you design your scripts carefully.
Scripts can be stored in libraries that can be added to your game.
To import a library, use the item Import scripts from the file menu.
To save your scripts in the form of a library use Export scripts.
Script libraries are simple text files (although they have the extension .gml).
Preferably don’t edit them directly because they have a special structure.
Some libraries with useful scripts are included.
(To avoid unnecessary work when loading the game, after importing a library, best delete those scripts that you don’t use.)

When creating scripts you can easily make mistakes.
Always test the scripts by using the appropriate button.
When an error occurs during the execution of a script this is reported, with an indication of the type of error and the place.
Rarely you will see a popup with the text “Unexpected error occurred during the game”
This error message indicate that some problem occurred in windows or in the hardware.
Often the reason for this is infinite recursion, lack of memory or insufficient hardware, drivers or firmware.
Generally speaking, these errors have to do with problems outside the gm-environment.
If you need to check things more carefully, you can run the game in debug mode.
Now a form appears in which you can monitor lots of information in your game.

Under the Run menu you can pause the game, run it step by step and even restart it.
Under the Watch menu you can watch the value of certain expressions.
Use Add to type in some expression whose value is shown in each step of the game.
In this way you can see whether your game is doing things the right way.
You can watch many expressions.
You can save them for later use (e.g. after you made a correction to the game).
Under the Tools menu you find items to see even more information.
You can see a list of all instances in the game, you can watch all global variables (well, the most important ones) and the local variables of an instance (either use the object name or the id of the instance).
You can also view messages which you can send from your code using the function show_debug_message(str).
Finally you can give the game commands and change the speed of the game.
If you make complicated games you should really learn how to use the debug options.

Lascia un commento