LibrariesIn Game Maker, a set of drag-and-drop actions is called a library. In the GM interface, these libraries are displayed as tabs containing icons called actions. Each action is a GML script or function that user can use in the game. Game Maker comes with a default set of libraries that contain the common actions used by most games; it is also possible to create libraries using the Library builder provided separately from Game Maker. GML syntax and semanticsGML is structurally similar to C-based languages in its use of code blocks, function calls, variable assignments, operator syntax, and so on. GML makes a difference between statements and expressions. For example
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 an assignment operator. GML also allows increment/decrement operators. For example, the code
is the same as
The same function applies to the operators Game Maker does not allow ?: syntax. GML statements can be separated by semicolon, but Game Maker does not force this. FunctionsGame Maker has a large library of built-in functions available for basic functionality. The programmer can also create scripts which are called in the same way functions are. The drawing functions in GML make use of the Direct3D API. GM also has built-in functions for calling external DLLs. Any feature that is not provided natively through Game Maker can be added using DLLs. VariablesNormally, GML does not require that variables be declared as with many other programming languages. A variable is created whenever a programmer first assign a value to it, as with GM has many built in variables and constants. Each instance in the game has a set of built-in local variables such as "x" and "y". There are also some built-in global variables such as "score" which exist independent of individual instances. Unlike global variables which are defined by the programmer, these built-in globals do not contain the "global." prefix. Arrays may also be declared in GML and may be 1 or 2 dimensional. Arrays may contain a mix of strings and real values, but not arrays themselves. Arrays may not be passed to a function and may not be returned from a function in GML. GM also has built in limits on index sizes. Indexes may not be bigger than 32,000 and any single array may not contain more than total of 1,000,000 values. GML also features functions used to create and edit six simple data structures. These functions are only available to users who have the Pro version of Game Maker. The data structures available are Stacks, Queues, Lists, Maps, Priority Queues, and Grids. TypesFor the sake of simplicity GML only has two 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 value larger than 0.5 as true, and 0.5 or any smaller value as false. The constants "true" and "false" may be used in place of 1 and 0 in GML. ScopeGML is thought to be an object-oriented language by many people, however, it is in fact not true. In GML, there are two types of variable locality: locality to an instance, and locality to a script (or any other piece of code that has its own container). Being local to an instance means that a variable is tied to that particular instance and can be called only with a prefix identifying the instance; being local to a script means that a variable can only be referenced within that script (and expires when the script finishes processing), since scripts do not have identifiers that are accessible to the user. When the term "local" is used without further specification, it usually refers to instance locality. By default, a variable is local to an instance but not local to the script in which it is used. To make a variable accessible for all instances, it can either be accessed through the global namespace ( Variables that are local to an instance can be accessed outside of that instances actions by prefixing the variable name with an instance identifier ( The current instance namespace can be changed using the "with" construct. For example, the following piece of code, placed in a collision event, could be used to destroy the other instance involved. (Note that in generating a collision event, Game Maker automatically creates the variable "other" as a reference to the other object involved.
with (other) { instance_destroy(); }
Note that when a variable is declared local to a particular script, it loses its association with the instance that called the script and becomes instance-independent. For example, the following code would work correctly even though the variable
var foo;
foo = "bar";
with (someOtherInstance) { show_message(foo); }
Memory allocationGML 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:
Unfortunately, there is no command for releasing a variable to free memory. When an instance is destroyed or a script finishes processing, however, any variables local to that instance or script are released. Hence to conserve memory, it is advisable that one use variables local to either instances or scripts to store information rather than global variables. For storing and manipulating larger amounts of data more efficiently, Game Maker has some built in data structures, such as stacks, queues, lists, maps, priority queues and grids. These structures are created, modified, and destroyed through built-in functions. There are also functions for sorting these structures, respective to each structure type. This can be particularly beneficial for speed optimization since the pre-compiled functions avoid the need to cycle through many loops of interpreted code. Instances and resourcesGame Maker does not support pointers to reference locations in memory. Thus, each resource and instance in Game Maker has a unique ID number, which is used to reference that particular resource or instance. This ID number can be used by scripts and functions to reference a particular instance or resource. Upon creation of a resource in GM the name of the resource is defined as a constant which references that resource (for objects the first instance is referenced). The ID of a particular instance of an object can be found using the variable "id". When creating resources or instances at runtime, its unique ID is returned and may then be passed to other variables and functions. 3d in Game MakerAlthough Game Maker was originally created for two-dimensional programming, the PRO version also has 3D functionality. The standard action library doesn't cover 3D programming, so users must either use GML code or download additional libraries. Game Maker uses Direct3D (D3D) for both 2D and 3D drawing. Code examplesHere is a simple piece of code that would display "Hello World!" in a popup message box.
Another example that would display the same text in the game window instead. Note that by default Game Maker redraws the background continuously, so using the default setting this code would need to be attached to the draw event for drawing to work properly.
Here is a piece of code from a game using GML:
GML supports many variations in syntax. As a demonstration, the previous example could also be written like this:
Here is an example of basic keyboard-controller movement. The motion_set function takes two arguments: direction (degrees) and speed (pixels per step). Calling this function will assign an instance with a speed and direction (both built-in local variables), which Game Maker uses to update the instance's position automatically each step. (An instance's position can also be modified directly using the built-in local x and y variables.)
Here is an example of a more complex script from a platform game. Using this, the player can walk over hills and bumpy terrain.
Here is a piece of code that draws a simple 3d floor. (The function
The manualThe manual that accompanies Game Maker is a very complete document that has information on all the functions available in Game Maker, with the exception of deprecated functions and built in variables left in for backwards compatibility, such as image_scale, which has been succeeded by image_xscale and image_yscale. External links
| |