Like any programming language GML uses variables as the basic
unit for most programming operations. Variables are used to store
information in the devices memory for later (or instant) use, and
they are given a name so that you can refer to them in functions
and programs. A variable in GML can store either a real number
(like 100, 2.456575, -56 etc...) or a string (like "Hello
A variable is something that we use to store a value for use in one or more operations or functions. Think of "pi", for example... it is a real world variable that holds the value 3.14159265(etc...). Why? Well, it's much easier to say to someone "pi" than "three point one four one five nine two six five"! So, naming things make life a lot simpler and it also means that should the value of that variable ever change, we don't have to change the number everywhere as the variable name is still the same. In GML a variable has a name that must start with a letter and can contain only letters, numbers, and the underscore symbol '_' with a maximum length of 64 symbols. So, valid variables are things like fish, foo_bar, num1, and invalid variables would be 6fish, foo bar, or *num. Now, In many programing languages you need to "declare" a variable before you can use it. This basically means that you tell the computer the name you wish to use so that it can assign a place in memory to store whatever value you decide to hold in that variable. With GML, that is not always necessary as it depends on the scope of the variable. There are four main variable categories when you program with GameMaker: Studio and each has its own scope (which is can be considered as its area of operation, or reach). These variables and their scope are outlined below:
- instance: these are the most common variables and are defined within an instance. These are unique to that instance and can be used in any event and any function within that instance.
- local: these variables must be declared using the "var" function. A local variable is only valid for the event or script resource in which it is created. So GameMaker: Studio will create the variable, use it for the duration of the event and then "forget" it again, meaning that if you try to use it later you will get an "unknown variable" error.
- global: a global variable is one that belongs to the game world itself and not to any one instance. It has to be declared as global first, but after that any instance can check or change the variable and the value will always reflect the last operation done on it, no matter what instance or code box has performed the operation.
- built in variables: these are special variables that are "built into" the objects and the rooms in the game world and they can be instance only or global in scope (but never local) .
More details can be found for these variable types on the
- Instance Variables
- Local Variables
- Global Variables
There are a few functions designed to help you when dealing with instance variables and global variables, although these are generally more associated with debugging than anything else: