GML

From GMpedia.org Wiki

Jump to: navigation, search

This article is about GML, the scripting language of Game Maker.

Game Maker Language (GML) is a scripting programming language developed for use with a computer game creation application called Game Maker. There is also a java version in the G-java2 IDE called the G-java API, allowing java developers the functions of GML. 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").

Contents

[edit] 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 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.

[edit] 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 by semicolon, but GM does not force this. Whenever GM is expecting a semicolon after a statement, it will assume it is there, regardless of whether it exists or not. Other than separating statements from each other, GM mostly ignores whitespace in code (except inside strings).

GML makes a difference between statements and expressions. For example g < 1; is not a valid statement and GM will return an error. Also, variable assignment is always a statement in GM, and cannot be used in an expression. For example, the following line would always generate an error because it would first compare if the variable "answer" is the same as what was returned from the get_string function and then compare if the boolean value was equal to "Yes" (a comparison of a real value and a 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 an assignment operator.

Game Maker does not allow ?: Syntax, and the unary increment/decrement operators.

[edit] Functions

GM has a large library of built in functions available that covers most of the basic functionality. It is not possible to define new functions in GML, but in Game Maker one can create scripts 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. In Game Maker 7.0 a new extension mechanism was created that is intended to replace these functions.

[edit] 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 many 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".

[edit] Scope

It is possible to define the scope of a variable in GM. Variables may be defined as either local (relating only to their instance), or global to the whole game. Local variables, which exist only in the scope of the current instance are defined when first assigning a value to the variable like localVar = 5; or local.localVar = 5;. As of Version 7.0, global variables, which exist everywhere in the game, can be defined using the 'globalvar' statement like globalvar foo, foo2;. When scope of a variable is not defined, it is assumed to be local. Variables can also be defined as relating only to a certain script using the var keyword such as var foo, foo2;. Variables declared in this manner exist only in the script in which they are declared. The argument variables are automatically declared to the current script.

Referring to variables in other objects or instances can be done using instanceID.variable = "foo";. Similarly, a special object "global" can also be used to refer to global variables if needed (which was the only way to refer to them prior to version 7.0).

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) { 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); }

[edit] Types

For the sake of simplicity GML only has three variable types. Every variable may hold each type of data without any type declarations.

  • Strings are sets of ASCII characters that may be of any length. Only memory limits the size of strings.
  • Real values are signed floating point numbers. Since version 6.1, GM has also allowed hexadecimal representation of real values in code (preceded by '$'). In version 6.x real value handling had a bug which limited real value accuracy to be smaller than intended (which caused inaccurate results when calculating with large real values).
  • Arrays in GML 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 registered Game Maker. The data structures available are Stacks, Queues, Lists, Maps, Priority Queues, and Grids.

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.

[edit] 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 = "This value is now a string";

Unfortunately it is not possible to release variables in GM to free memory. It is possible, however, to delete instances of objects in game and free the memory alocated to their variables. Also, any variable declared in a script using the "var" keyword is released at the terminus of the script. For these reasons it is advised that one use variables local to either scripts (var) or instances (local) to store information rather than global variables to save memory.

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 are accessed through functions. Memory can be allocated by creating the data structure and adding items, and can also be manually freed.

[edit] Instances and Resources

Game 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.

[edit] Code Samples

Here is a simple 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 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)

draw_text(0, 0, "Hello World!");

Here is a piece of code from a game using GML:

//<-This is a comment
/* this is a C-Style comment. */

/*temporary variable declaration.
  A temporary variable will be released at the end of a script. 
  Note that this doesn't declare it to be of a specific type!*/
var xx, yy, nn;

/* A conditional. another way of writing this is "if (can_shoot)" since
   booleans are the same as integers */
if (can_shoot = true) /* "==" can be substituted as a comparison operator */
{  //This begins a block. You can also use "begin" like in pascal.

  /* Here you are setting the integer (psuedo-boolean) variable can_shoot
     to false, a constant equaling 0. */
  can_shoot = false;

  /*Here you are setting the built-in alarm variable's 0th indice to 5. 
    The alarm variable will count down to 0, and when it does hit 0, 
    the alarm0 event will be triggered.*/
  alarm[0] = 5;

  /*Here the temporary variable xx is defined implicitly as an integer, 
    and the lengthdir_x function is used.*/
  xx = x + lengthdir_x(14, direction);
  yy = y + lengthdir_y(14, direction);

  //This function creates a obj_bullet and then returns its instance id to nn.
  nn = instance_create(xx, yy, obj_bullet);

  /*The with statement allows you to access the fields of an object directly, 
    without having to write statements like nn.speed or nn.direction.*/
  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
  endif
end


Here is an example script from a platform game. Using this the player can walk on hills and bumpy terrain.

if !place_free(x-4,y)
    {  
    if place_free(x-4,y-4)
    {
      x-=4
      y-=4
  }
  else
  if place_free(x-3,y-5)
    {
      x-=3
      y-=5
  }
else
  if place_free(x-2,y-6)
    {
      x-=2
      y-=6
}
}
else
  x-=4

Here is an example of movement

{ if

   keyboard_check(vk_nokey)    //If no key is pressed
   motion_set(0,0)

{ if

   keyboard_check(vk_left)     //If the left arrow key is pressed
   motion_set(180,4)

{ if

   keyboard_check(vk_up)       //If the up key is pressed
   motion_set(90,4)

{ if

   keyboard_check(vk_right)    //If the right key is pressed
   motion_set(0,4)  

{ if

   keyboard_check(vk_down)     //If the down key is pressed
   motion_set(270,4)

}}}}}

[edit] The Manual

The 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 was preceded by image_xscale and image_yscale.

[edit] External links

Retrieved from "http://gmpedia.org/wiki/GML"
Personal tools