Scope
global var number = 24;
A Scope is an area of a program where an object is recognized. Procedures have their own scope. To the rest of the script, any variables declared inside a procedure do not exist.
For instance, if you declare a variable in Main()
, you will not be able to use it from any other procedure. Also, unless you assign the value to a variable of greater scope, when the procedure ends, the value will be discarded.
However, variables can be given different scopes.
Variable Scopes
Name | Description |
| Default scope for variables. |
| Persists across all scripts while the game is running. |
| Value persists per procedure while the script is running. |
| Must be initialized with a value. Cannot be changed afterward. |
Local
In the example above, the value of variableName
cannot be accessed by OtherProcedure()
since it only exists to Main()
, where it was defined.
However, you can still pass it by reference to use it for math or initializing a variable in another procedure.
Global
global
variables are persist even after a script is done executing.
They stay in memory while the game is running, but aren't saved to your save file.
A practical use case would be keeping track of the last entered number after closing and re-opening a menu.
Static
static
variables can be declared but not initialized outside of a procedure.
These keep their value between executions while the game is running, but aren't saved to your save file.
Const
const
variables are constant, so their value cannot be changed after they're initialized.
This can be useful for referring to values you know in advance by a convenient name.
Out Variables
Aside from passing by reference or using global
, static
or const
, you can also use out variables to pass values between procedures, even void
. It would look like this:
As you can see, the variables were already declared in the parameters of OutTest()
using the out
keyword.
After being initialized with a value, the x
and y
variables are then passed to Main()
, once again using the out
keyword.
If this seems confusing, don't worry about it for now. You will not have to do this very often.
Conclusion
Now you should have a grasp on how data is stored and handled by scripts. You're ready to learn more advanced usages of these techniques, such as Arrays and Loops.
ArraysLast updated