A variable is a named value for the program. What that value is can vary over time, hence the name "variable". In GML, values can be numbers, words, or other structures of data.
Variables must be declared to an initial value prior to first use. Otherwise the program will be unable to associate a value with it, and the calling statement will fail.
To declare a variable, use the assignment operator: =
.
Data types categorize the value in a variable. GameMaker defines many data types, but the below three are the most common.
This data type is for values of numbers. GameMaker does not distinguish different types of numbers unlike other programming languages. Integers, Doubles, Floats, etc. are all just Reals to GML.
Values are either true of false.
This variable type is for values of words or sentences. It is distinguished with the surroding ""
upon assignment.
When a variable is declared with no prefix, its scope is for the instance or struct that declared it.
The var variable_name
prefix limits the variable's applicability to the block of code it is declared in..
The global.variable_name
prefix makes the variable applicable to the entire program.
GameMaker reserves several variables names for itself. These built-in variables are often well-established in what they mean and common across multiple potential game implementations. Example include x
and y
for an instance's coordinates, and speed
for how fast an instance is moving.
Variables should not be named such that they overlap with an exisitng built-in variable.
All object variables should be declared in the Create event.
Can a variable's value change from a number to a word?
Learn More:
Strong and weak typingGameMaker Manual - What is Programming
Wikipedia - Strong and weak typing