Game Maker Language
From Free net encyclopedia
GML (an acronym for "Game Maker Language") is a scripting programming language developed for use with a computer game creation application called Game Maker. It was originally created by Mark Overmars to supplement the drag-and-drop action system used in Game Maker. However, in the latest versions, all the drag-and-drop actions are based on GML rather than being separate from it.
GML has syntax and structures similar to other well-known programming languages like [[C++]] and Pascal, such as the switch structure and loops.
Many of Game Maker's features are accessible through the drag-and-drop interface, but GML extends the functionality by providing functions that are not usable through the drag-and-drop interface, such as file handling functions, online multiplayer functions and functions to call external DLLs.
In Game Maker, GML is used in conjunction with drag-and-drop. Unless the entire project is written in GML, scripts cannot be called without drag-and-drop actions, but all drag-and-drop actions may also be written in GML. Most projects consist of both GML and drag-and-drop actions, depending on the programmer's preferences.
GML is interpreted rather than compiled. GML is compiled to byte-code at program startup to speed up execution.
Contents |
Code Samples
Here is an example that would show "Hello World!" inside a popup dialog on screen:
show_message("Hello World!");
Another example that would write the same text on game window instead:
draw_text(0, 0, "Hello World!");
Here is a piece of code from a game using GML:
var xx, yy, nn; if (can_shoot == true) { can_shoot = false; alarm[0] = 5; xx = x + lengthdir_x(14, direction); yy = y + lengthdir_y(14, direction); nn = instance_create(xx, yy, obj_bullet); with (nn) { speed = obj_tank.speed + 3; direction = obj_tank.direction; } }
GML also supports multiple other ways to write code, so the previous example could also look like this:
var xx yy nn; if can_shoot = true then begin can_shoot := false alarm[0] := 5 xx := x + lengthdir_x(14, direction) yy = y + lengthdir_y(14, direction) nn := instance_create(xx, yy, obj_bullet) with nn begin speed := obj_tank.speed + 3 direction := obj_tank.direction end end
Libraries
In Game Maker, a set of drag-and-drop actions is called a library. In the GM interface, these libraries are displayed as tabs containing actions in the drag-and-drop interface. Each action is basically a GML script that user can easily use in the game and give values to it. Game Maker comes with a default library that contains all the common actions used by most game makers, but it is also possible to create your own libraries using the Library builder provided separately from Game Maker.
GML Syntax and Semantics
GML is a language similar to C++. It consists of statements and expressions and different kinds of structures. In GML statements are basically separated from each other by semicolon, but GM does not actually force this. Whenever GM is expecting a semicolon after statement, it will assume it is there, regardless of whether it actually exists. So, statements may be separated with whitespace also. Other than possibly separating statements from each other, GM mostly ignores whitespace in code (except inside quoted strings).
GML makes a difference between statements and expressions. For example g < 1;
is not a valid statement and GM will produce a parse error from that. Also, variable assignment is always a statement in GM, and cannot be used in expression. For example, the following line would always generate an error because it would first compare if the variable "answer" was the same as what was returned from the get_string function and then compare if the boolean value was equal to "Yes" (and comparison of real value and string will produce an error):
if ((answer = get_string("Yes or No?", "")) == "Yes")
Note that the equal sign "=" is a variable-assignment operator in statements and a boolean-comparison operator in expressions, unlike most C++ compilers which require "==" for boolean comparison. However, the double equal sign "==" may also be used as comparison operator (but not assignment operator).
Functions
GML has a relatively large built in set of functions available that cover most of the basic functionality. It is not actually possible to define your own functions in GML, but in GM you can create script resources which can be called as functions, basically doing the same thing.
The functions in Game Maker provide the ability to communicate easily with DirectX.
GM also has built in functions for calling external DLLs. So any feature that is not provided natively through GM can be added using DLLs.
Variables
GML does not have compulsory declaration of variables similar to many other programming languages. A variable is created whenever you first assign a value to it, such as foo = "bar";
.
GM has a large set of built in variables and constants. Each object in the game has a set of local variables such as "x" and "y". There are also some variables that are global which means that they relate to the whole game e.g. "score".
Scope
Even though you cannot define variables, you may define a scope of a variable using "var" statement like var local1, local2;
. This will define the variables "local1" and "local2" to only exist within the scope of the current script. If the variable is not defined as local to the current script, it is automatically assumed to be local to the current object, which is running the script. By using the constant "global" as an object, a variable may also be defined to be in global scope like global.variable = "bar";
(global variables must always be also referenced using the global object). Similarly you may reference variables of another object using otherobject.variable = "foo";
.
It is worth noting, that the current object is changed inside "with" structure. For example the following piece of code would produce "unknown variable" error, because the variable is not defined to the object in with statement:
foo = "bar"; with (other_object) { show_message(foo); }
However, we could avoid this by defining "foo" as local to the current script like:
var foo; foo = "bar"; with (other_object) { show_message(foo); }
Types
For the sake of simplicity GM only has three variable types. Every variable may hold each type of data.
- Strings are sets of ASCII characters that may be of any length. Only memory limits the size of strings.
- Real values are floating point numbers. However, ever since version 6, GM has suffered from inaccuracy problems with real values, which will cause many real value calculations to give slightly wrong results with big numbers.
- Arrays may be 1 or 2 dimensional arrays. Arrays may contain mix of strings and real values, but not arrays themselves. Arrays may not also be passed to a function and may not be returned from a function in GML. GM also has built in limits to index sizes. Neither index may not be bigger than 32000 and any single array may not contain more than total of 1000000 values.
Because GML has no boolean values, statements that require booleans values, such as "if" will evaluate any rounded value bigger than 0 as true, and 0 or any smaller value as false. Usually also any value that returns only true or false will return 1 if true and 0 if false. In GML "true" and "false" are just constants that contain values 1 and 0.
Memory Allocation
GML automatically allocates memory for variables on demand, and uses dynamic typing so that assignments with different types of values are possible. For example, one can create a variable as an integer, and change its type to a string in two lines:
intNumber=1; intNumber="some number";
Unfortunately it is not possible to undefine variables in GM and free memory through that way. However, it is possible to destroy objects during the game, and any variable that was associated to that object will also be removed from the memory. Also, any variable local to scripts is removed from the memory after the script is run. For these reasons it is usually advisable to use either script or object local variables to store information instead of global variables so that it may be freed from the memory after it is no longer used.
Instances and Resources
In Game Maker you can add plenty of different resources to a game, such as sprites, music, backgrounds, etc. However, GML has no particular pointer variable type to reference this in code. Thus, each resource and instance in Game Maker has a unique ID number, which is used to reference that particular resource or instance. Since it is possible to pass around numbers from one function to another, is also possible to pass instances and resources from one function to another. Upon the addition of a game resource, GM defines a constant (given upon creation of the resource, and editable afterwards) that contains that ID number for later use.
When creating resources or instances at runtime using GML, it is possible to pass the ID, returned by the respective creation function, to any variable.