|
Game Maker Language (GML) 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. A common misconception is that languages such as Pascal, ASM, and C++ can be directly used in GML. This is incorrect, and is a common mistake due to GML's ability to utilise Pascal and C++ style syntax (e.g. "&&" is interchangeable with "and").
Code Samples Here is an example that would show "Hello World!" inside a popup dialog on screen: Another example that would write the same text on the game window instead: (Note: because of the way GameMaker handles drawing functions, this code would have to be placed in the Draw event of an object) Here is a piece of code from a game using GML: GML also supports multiple other ways to write code, so the previous example could also look like this: Here is an example script from a platform game. Using this the player can walk on hills and bumpy terrain. 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 a GML script that user can use in the game and give values to it. Game Maker comes with a default library that contains the common actions used by most game makers, but it is also possible to create 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. GML statements can be separated from each other by semicolon, but GM does not force this. Whenever GM is expecting a semicolon after statement, it will assume it is there, regardless of whether it exists. So, statements may be separated with whitespace also. Other than 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 built in library of 1178 functions available that cover most of the basic functionality. It is not possible to define new functions in GML, but in GM one can create script resources which can be called as functions. The functions in Game Maker allow communication 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 a programmer 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 one cannot define variables, one 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 one may reference variables of another object using otherobject.variable = "foo";.Note 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) However, we could avoid this by defining "foo" as local to the current script like: var foo; foo = "bar"; with (other_object) Types For the sake of simplicity GML only has three variable types. Every variable may hold each type of data without any type declarations. Because GML has no boolean values, statements that require boolean values, such as "if" will evaluate any rounded value bigger than 0 as true, and 0 or any smaller value as false. Usually, 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 one 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. The Manual The Manual that accompanies GameMaker is a very complete document that has information on all the functions available in GameMaker, with the exception of a few functions that aren't supposed to be used but are only accesible for staying compliant with past versions (e.g. image_scale, which is outdated because of image_xscale and image_yscale, which give more control). | ||||||||
|
| |||||||||
![]() |
|
| |