Data Types

In previous section we covered variables and their scoping rules but little has been said about the different data types that a variable can store. Therefore this section explains the different types and what they can be used for.

NOTE You can use the function typeof to get the data type that a variable holds.

Before continuing, let's just briefly explain what we mean by "data types". When you create a variable it can be used to hold information, and when you call a function, it can also stored returned information. However, this information can come in various "flavours" - for example, it can be a real number or it can be a string. These different types of values being used are called data types and when using the GameMaker Language they can be any of the following:

Real NumbersReal Numbers

Real numbers are any value that is not a string, nor any of the other possible data types. So, 124, 45639.566546456, 0, -45.5, etc. are all examples of real numbers. All real numbers are stored as 64-bit double-precision floating point values (or integer values), and the compiler will optimise where possible (for example, 0.0 will be optimised to the integer value of 0).

When dealing with any value that is not an integer, you may experience slight rounding errors due to the nature of floating point maths. For more information on this and other number related functions, please see: GML Reference - Number Functions.

NOTE On the HTML5 target, all real numbers are doubles.

NOTE While created variables in GameMaker are all stored as double-precision floating point numbers or integers, you can still use other formats when dealing with extensions. These can be passed into GameMaker from an extension and then checked using the appropriate is_*() function, a list of which can be found under Data Type Functions.

  

BooleanBoolean

A boolean is simply a value that can either be true or false. Note that currently GameMaker will interpret a real number equal to or below 0.5 as a false value, and any real number greater than 0.5 as being true. This does not mean however that you should be checking 1 and 0 (or any other real number) for true and false, as you are also provided with the constants true and false, which should always be used in your code to prevent any issues should real boolean data types be added in a future update.

You can convert any real number into an implicitly boolean value using the following function:

 

StringsStrings

 

ArraysArrays

 

StructsStructs

 

Method VariablesMethod Variables

 

int64int64

An "int64" is a 64-bit integer that can be created using int64 (by passing in a non-64-bit real number) or when reading a buffer_u64 value from a buffer.

This can be used in places where a 64-bit integer is strictly required, or when you want to work with bit-shifting and need those 64 bit positions.

Any bitwise operations, even when run on non-64-bit values, will always return a 64-bit integer.

Divisions on int64 values will also return integers (e.g.: int64(5) / int64(2) = 2).

References for data structures and assets (such as objects, sprites, etc.) are stored as int64s, where the first 32 bits contain information about the type of resource, and the next 32 bits contain the ID for the resource. See the next section "Handles" for more info.

 

HandlesHandles

A handle contains a reference to one of the following types of resources:

You get a handle when you create a new resource (with a _create() function) or reference an existing resource in your code (like referencing an object, getting an instance through a function, etc.).

A handle is a 64-bit integer, where the first 32 bits contain information about the type of resource, and the next 32 bits contain the index number of the resource.

If you convert the handle to a number (using real or int64), you get the index number.

The type information in a handle is used to make sure that you pass the correct type of resource into a function (e.g. making sure you pass a DS list into ds_list_add instead of a DS map, grid or something else).

If you convert a handle to a string or print it as an output, you will see a string in the format "ref <type> <id>", e.g. "ref ds_list 1".

You can convert a string that's correctly formatted as "ref <type> <id>" back to a handle using the handle_parse function.

 

Hexadecimal LiteralsHexadecimal Literals

GameMaker will accept hexadecimal literals as legitimate values. Hexadecimal values are especially common when working with colours, but can be used anywhere a positive integer value is required. Hexadecimal values can be formatted in the following two ways, where abcd would be the actual hex value:

$abcd
0xabcd

For example, the following decimal values can be expressed as hexadecimal as shown:

11406 -> $2c8e, 0x2c8e
16777215 -> $ffffff, 0xffffff


A hexadecimal value can also begin with a hash/pound symbol (#), however when written this way, its value will not equal a similar hex value written using a previously shown format ($ or 0x). This is due to the way colours are interpreted in GML, which required the format for hash/pound hex values to be changed so that CSS colours could be written in an #RRGGBB format. For more information, see Hex Colours.

For example, the following two are not equal:

$2c8edd != #2c8edd

For them to refer to the same decimal value, you would have to swap the first two and last two characters:

$2c8edd == #dd8e2c

 

Binary LiteralsBinary Literals

You can write binary literals with the 0b prefix:

var _six = 0b0010 | 0b0100; // produces 0b0110, or 6

The above code shows two binary values, 0010 and 0100, being used in a bitwise "OR" operation.

 

PointerPointer

A pointer is a data type that "points" to a memory location. You cannot do operations on a pointer in GameMaker and it is used only for some very specific functions, like getting a texture or buffer address from memory for another function. For examples of functions that return a pointer you can see buffer_get_address or sprite_get_texture.

There is also a function to check if a value is a pointer (see "Checking Data Types", below) and a function to convert a value into a pointer:

You may also use (and get returned) the following built-in constants when using pointers:

Constant Description
pointer_null This constant indicates that the pointer is not pointing to anything meaningful (the same as NULL in C++ or null in C#). This value is falsy.
pointer_invalid This constant simply means that the value is not a valid pointer

 

EnumEnum

An enum is an "enumerator", and it essentially permits you to create your own limited data type with a list of constant values. Enums are explained in depth on the page for Constants.

 

UndefinedUndefined

ds_map_find_value

 

NaNNaN

Note that since NaN is not a number, it cannot be compared to itself, so comparisons such as NaN == NaN will return false. Same goes for an array comparison such as this:

show_debug_message(array_equals([NaN], [NaN]));

// Output: 0 (false)

 

InfinityInfinity

The constant infinity refers to a number that is considered infinite, such as the result you would get when dividing any floating point value by zero, eg: 1.0/0.

Note that the infinity constant is equal to itself, so infinity == infinity will return true.

AnyAny

The "Any" data type can be found on many pages in the manual, e.g. in the arguments, or as a return value. It indicates that any type of value is accepted, or can be returned.

Underscores in Literals

You can use underscores (_) in numeric literals, such as real numbers, hexadecimal and binary values. These are ignored during compilation and only serve as visual separators. Here are a few examples:

var _integer = 100_000_000; // same as 100000000
var _float   = 3_141.59; // same as 3141.59
var _hexadec = 0xDEAD_BEEF; // same as 0xDEADBEEF
var _binary  = 0b01101000_01101001; // same as 0b0110100001101001

All the underscores used in the literals shown above only make them easier to read, and don't change anything about their functionality.

Related Pages

You can check the data type of any variable using the functions listed on the following the page:

You can also find arithmetic type tables that show the results of different operations using mixed variable data types here: