In previous sections we have covered variables and their scoping rules but
little has been said about the different data types that a
variable can store. Therefore this chapter explains the different
types and what they can be used for.
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 GML function, it can also
return information. However this information can come in various
"flavours", like it can (for example) be a real number or it can be
a string. These different types of values being used are called
data types and when using GML they can be any of the
following:
A string is simply any text that has been placed in quotation
marks (either '...' or "..."). You can perform certain operations
on strings, like add two strings together to create a longer string
(concatenation) and you can also change the string properties and
even extract real numbers from it. For more information on the
string functions see: Reference -
Strings.
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 32bit floating point values (even integer values), so you
may experience slight rounding errors when dealing with these. For
more information on this and other number related functions, please
see: Reference - Real
Numbers.
Note that created variables in GameMaker are all stored as
double-precision floating point numbers, but you can use other
formats when dealing with extensions. These can be passed into
GameMaker: Studio from an extension and then checked using the
appropriate is_ function, listed at the bottom of this
page.
An array is a special data type that can be used to hold multiple values. You assign the array to a variable, and then "fill in" different indices of the array with values. This array can then be passed through to scripts and functions on a pass-by-reference basis, however should you modify the passed array, it will then become a copy of the original array and so will need to be returned back to the original variable for the array to be updated. For more information on arrays, please see: GML Overview - Arrays.
A boolean is simply a value that can either be true or
false. Note that currently GameMaker: Studio does
not support "true" boolean values, and actually accepts any real
number below 0.5 as a false value, and any real number equal to (or
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.
A pointer is a data type that "points" to a memory location. You
cannot do operations on a pointer and it is used 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#) |
pointer_invalid | This constant simply means that the value is not a valid pointer |
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 global scope variables (this is implicit, and they require no "global" suffix or globalvar declaration) and they have the following structure:
enum
<variable>{<constant> [=
<value>]}
In the following example, we create an enum for the colours of the rainbow and assign it various constants and default values:
enum rainbowcolors {
red,
orange,
yellow,
green,
blue,
indigo,
violet
}
The enum entries can only be real numbers or expressions with previous enums, and by default are numbered from 0 upwards, so our example given above would default to red = 0, orange = 1, yellow = 2, etc... You can also assign values to the enum variables at the time of creation, using real numbers or expressions:
enum rainbowcolors {
red = 5,
orange = 5 * 2,
yellow = 15,
green = 20,
blue = 25,
indigo = 30,
violet = 35 * enum_test.entry
}
Notice in the above example we use another enum to create an
expression for "violet". This only works if the enum being
referenced was created before the enum that is using it in
an expression, but it will not work for variables or functions,
since the enum value must be able to be evaluated as a constant on
compile.
To later access the value within a given enum type, you can use the
point "." method, like this:
var value = rainbowcolours.green;
Note that you cannot modify the values for any enum constant
after it has been created.
An undefined value (also known as a "null" value") is one where
an expression doesn't have a correct value, although it is
syntactically correct, and so must return something. For
example, say you have a ds_map and use the function
ds_map_find_value(). Now, what happens when the map does
not have the value being looked for? Well, since the function is
correctly formatted, and the issue is that the no such value
exists, then it would return the constant
undefined, and you can check for this constant as you
would check for true or any other value.
GameMaker: Studio permits you to check the data type of a given variable using the following functions: