GameMaker Manual - GameMaker.info (2024)

GameMaker has a built-in programming language called GML. Once you become morefamiliar with GameMaker and want to use it to its fullest extend, itis advisable to start learning to use this language. For more details in theGML language see Part 4 of this documentation.

There are two ways touse the language. First of all you can create scripts. These are pieces ofcode to which you give a name. They are shown in the resource tree and can besaved to a file and loaded from a file. They can even be used to form a librarythat extends the possibilities of GameMaker. Alternatively, you canadd a code action to some event and type a piece of code there. Adding codeactions works in exactly the same way as adding scripts except for twodifferences. Code actions don't have a name and cannot use arguments. Alsothey have the well-known field to indicate to what objects the action shouldapply. For the rest you enter code in exactly the same way as in scripts. Werecommend you to use scripts for all but simple pieces of code as GameMakerhas many more features that deal with 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 usethe script action. In the script action you specifythe script you want to execute, together with the up to five arguments.

You can also execute a script from within a piece of code (a different script) in the same way youcall a GM-function. In that case you can use up to 16 arguments.Scripts can return a 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 whenproviding values in other actions.

Scripts are extremely useful to extend the possibilities of GameMaker.This does though require that you design your scripts carefully. Scriptscan be stored in files that can be added to your game. To import ascript file, use the item Import scripts from the Scripts menu. To save yourscripts in the form of a file use Export scripts. Script librariesare simple text files (although they have the extension .gml). Preferablydon't edit them directly because they have a special structure.

To create a script in your game, choose Create Script from the Resources menu.A code editor will appear that will be described next.

The code editor

When creating or editing a script (or a code action) the following code editor formwill appear (in the example we already added alittle script that computed the product of the two arguments).

GameMaker Manual - GameMaker.info (1)

At the top right you can indicate the name of the script. You should give allof your scripts a sensible name. Make sure the name only consists of letter, digits,and the _ symbol. It should not start with a digit. In this way you can use thescript later as a function in code.

At the top you have a toolbar with some useful buttons. From left to right youhave the OK button to close the editor, saving the changes, buttons to load thescript from a text file, save it to a text file, and print it.

Next there are undo and redo buttons. The editor will basically remember allchanges you did and you can undo them (or redo the undone changes). You can changethe maximal number of undo operations in the Preferences. Here you can alsoindicate whether you want to group the undo operations (such that e.g. typinga line of text can be undone in one step) or that every keystroke is separatesaved. You can use <Ctrl> + Z for undo and <Ctrl> + <Shift> + Z forredo.

Next to this there are buttons to cut, copy, and paste text, as you should be familiarwith. The final two buttons are for finding and replacing and for code checking. These willbe explained below in detail.

Below the toolbar there is the area where you can type the text. At the left there are theline numbers. (You can switch these on and off using the F9 key.) The line with the cursoron it is marked in bold. The dark red bar behind certain line numbers indicated that theselines have changed. You will notice that you can type text anywhere on the lines. Soa line does not really have an end. This tends to be useful when alligning text. Thereare many editing possibilities, that will be described below.

At the bottom there is the status bar. This shows from left to right the current line number,total number of lines and cursor position. Whether you are in Insert mode or Overwrite mode. (InOverwrite mode also the cursor chnanges to make it clear that you are overwriting the existing text.)Next there is the point size for the font used. You can use F7 and F8 to decrease or increase thefont size. (You have more control over the font size and type in the Preference.)

Editing tools

The editor has many editing possibilities. Some of these can also be accessed through thepop-up menu that appears when you press the right mouse button. Here are the most important ones:

  • Multiple undo and redo either per key press or in groups
  • The arrow keys move you around in the text. Use <Ctrl> with the left and right arrow key to move to the beginning of the previousor next word. Use <Ctrl> with the up and down key to also scrollthe text. Hold the <Shift> key to select text.
  • Double click the line number to select the complete line.
  • The Home key moves to the beginning of the line and the End key to the end of the line. With <Ctrl> they move to the start and end of thescript. Hold the <Shift> key to select text.
  • You can scroll through the text with the mouse wheel. With <Ctrl> the cursor scrolls with the text. Hold the <Shift> key to select text.
  • Intelligent auto indent that aligns with the previous line when you use the return key of the backspace key (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 <TAB> to indent selected lines and <Shift> + <TAB> to unindent selected lines.
  • Hold <Ctrl> with the Backspace or Delete key to delete until the beginning of the previous or next word.
  • You can drag a selection with the mouse. (Use b><Ctrl> to copy it.) The new location is immediately shown.
  • Cut, copy and paste of text between script forms and other programs.ograms.
  • Use F12 to open the script or resource whose name is at the cursor position.

Color coding

As you might have noticed, parts of the script text are colored. The editorknows about existing objects, built-in variables and functions, etc. Color-coding helps a lot in avoiding mistakes. In particular, you see immediatelyif you misspelled some name or use a keyword as a variable.If you don't like the color coding, in the Preferences you can switchit on and off (you can also use F10). In the Preferences you can also change thecolor for the different components of the code.

Function and variable help

contains many built-in functions and variables and you add your ownresources and scripts to this. So it might be hard to remember all of them. Fortunatelythe editor helps you with this. When you type part of a name (at least two characters) andwait a little, a list is shown of all possibilities. For example, as follows:

GameMaker Manual - GameMaker.info (2)

You can continue typing and the list will adapt. You can use the up and down keys to scroll throughthe list and press Enter to replace the text you are typing with the selected function or variable name.

You can switch this feature on and off in the preferences. You can always force the list to appearby pressing <Ctrl> + Space, and you can cancel it at anytime by pressing <ESC>.

A second form of help is given when you are typing in the arguments for a function. In this case, in thestatus bar at the bottom the function with its arguments is given. The current argument you are typing isshown in bold. In this way you will never forget arguments or get the order wrong.ong.

Find and replace

It is common that you want to find some piece of text in your code or replace some text with another one.To this end you can either press the Find button in the toolbar, or press <CTRL>+F. A panel will show up at the right of the form, as follows:

GameMaker Manual - GameMaker.info (3)

You can type the find string at the top. Note that in the code all occurrences of the find string are immediately shownwith a yellow background. (You can switch this off in the Preferences.) As usual you can indicate whether the searchmust be case sensitive (which is normally what you want as variable and function names in GML are also case sensitive)and whether you want to only search for whole words. Note that the editor remembers your most recent searches./p>

With the buttons with the blue arrows you can now move to the previous, next, first, and last occurrence of thefind string. It will be selected in the text such that you can e.g. delete it.

When you want to replace occurrences, also type a text in the replace box. Now you can use the buttons to replace theprevious occurrence, the next, the first, the last, or all occurrences of the find text. Remember you can alwaysuse Undo to undo the replacements.

Code snippets

Code snippets are small pieces of code that you will often use. You can add them in the code through the snippet menu.To this end press F2. A menu appears from which you can select the snippet. It will be inserted at the cursor inyour code. Some pieces consists of capital letters only. These must still be replaced by you. The first one isselected for replacement. To replace another, simply double click on it.

You can define your own code snippets if you want. The snippets are stored in the file snippets.txt inthe folder in which GameMaker is installed. You can edit this file with any text editor. Each snippet ison a separate line and there should be no blank lines. It starts with the name of the snippet (shown in the menu)followed by the colon (:) followed by the actual snippet. Use # for a newline and put the text that needs tobe selected first between | symbols. See the examples that are already in the file. (Better make a backup ofit before you start changing it.)

Error checking

In the toolbar at the top there is a button with which you can switch error checking on or off.When error checking is on, GameMaker constantly tests the code you are typing and reportsthe first error it finds at the bottom of the window. Also the line number for the line with theerror is drawn in red. You can click on the error message to go to the correct line and position tocorrect the error. Note that not all aspects can be tested at this stage but the syntax of yourscript will be tested, together with the existence of functions used.

When you start typing a script, error checking might be annoying so you can switch it off. But when thescript is almost ready you better switch it on to correct all the errors. Note that also thecolor coding will help you catch errors. Finally, when you put your cursor at a bracket the editorwill show you the corresponding opening or closing bracket. If it cannot find it, it is shown in red.(You can switch this off in the Preferences.))

Debugging

When creating scripts you can easily make mistakes. Always test the scripts byusing the error checking button. However, this will not catch all errors. Soyou will need to run the game to test your code.

When an error occurs during the execution of ascript this is reported, with an indication of the type of error, the script oraction it occurs in, and the line and position.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 inthe hardware. Often the reason for this is infinite recursion, lack ofmemory or insufficient hardware, drivers or firmware.Generally speaking, these errors have to do with problems outside the GameMaker environment.

If you need to check things more carefully, you can run the game indebug mode. Now a form appears in which you can monitor lots of informationin your game..

GameMaker Manual - GameMaker.info (4)

Under the Run menu you can pause the game, run it step by step andeven restart it. Under the Watch menu you can watch the value ofcertain expressions. Use Add to type in some expression whose valueis shown in each step of the game. In this way you can see whether your gameis doing things the right way. You can watch many expressions. You can savethem for later use (e.g. after you made a correction to the game). Under theTools menu you find items to see even more information. You can see alist 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 usethe object name or the id of the instance). You can also view messages whichyou can send from your code using the function show_debug_message(str).Finally you can give the game commands and change the speed of thegame. If you make complicated games you should really learn how to use thedebug options.

« Time Lines | Extension Packages »

GameMaker Manual - GameMaker.info (2024)
Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 5267

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.