Variables And Variable Scope

Here you can learn about the different types of variables in GameMaker: Studio.

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 world!").

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:

More details can be found for these variable types on the following pages:

  1. Instance Variables
  2. Local Variables
  3. 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:

Back: GML Overview
Next: Addressing Variables in Other Instances
© Copyright YoYo Games Ltd. 2018 All Rights Reserved