Scope
global var number = 24;
Last updated
Was this helpful?
global var number = 24;
Last updated
Was this helpful?
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 inside a do not exist.
For instance, if you declare a variable in , you will not be able to use it from any other . Also, unless you assign the value to a variable of greater scope, when the ends, the value will be discarded.
However, can be given different scopes.
Name
Description
local
global
Persists across all scripts while the game is running.
static
const
Must be initialized with a value. Cannot be changed afterward.
A practical use case would be keeping track of the last entered number after closing and re-opening a menu.
Default scope for .
Value persists per while the script is running.
In the example above, the value of variableName
cannot be accessed by OtherProcedure()
since it only exists to , where it was defined.
However, you can still pass it by reference to use it for math or in another.
global
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.
static
can be declared but not initialized outside of a .
These keep their value between executions while the game is running, but aren't saved to your save file.
const
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.
Aside from or using global
, static
or const
, you can also use out variables to pass values between , even void
. It would look like this:
As you can see, the were already 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.
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 and .