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.
// variableName only exists within Main() since it's local
void Main() {
var variableName = 25;
// no other procedure will be able to recognize 'variableName'
AnotherProcedure();
}
void AnotherProcedure(int variableTest ) {
// This would cause an ERROR.
var test = variableName;
}
However, variables can be given different scopes.
Variable Scopes
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.
// variableName only exists within Main() since it's local
void Main() {
var variableName = 25;
// but since AnotherProcedure() takes an int...
AnotherProcedure( variableName );
}
// ... we can make a copy of its value with a different name
void AnotherProcedure(int variableTest ) {
// This would create a new local variable equal to 25
var test = variableTest;
}
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.
// globalTest exists for as long as the game is running, even after Main()
global int globalTest = 50;
void Main() {
globalTest = globalTest + 30;
AnotherProcedure();
}
void AnotherProcedure() {
// This would create a new local variable equal to 80
var test = globalTest;
}
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.
static int staticNumber;
void Main() {
// the first static int will be 10 higher every time the script is run
staticNumber = staticNumber + 10;
}
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.
const int constantNumber = 99;
void Main() {
constantNumber = 0;
// test would still equal 99
var test = constantNumber;
}
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:
void Main() {
OutTest( out x, out y );
var x + y; // result equaling 35
}
void OutTest( out int x, out int y )
{
x = 5;
y = 30;
}
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.
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
Was this helpful?