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.
1
// variableName only exists within Main() since it's local
2
void Main() {
3
var variableName = 25;
4
// no other procedure will be able to recognize 'variableName'
5
AnotherProcedure();
6
}
7
8
void AnotherProcedure(int variableTest ) {
9
// This would cause an ERROR.
10
var test = variableName;
11
}
Copied!
However, variables can be given different scopes.

Variable Scopes

Name
Description
local
Default scope for variables.
global
Persists across all scripts while the game is running.
static
Value persists per procedure while the script is running.
const
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.
1
// variableName only exists within Main() since it's local
2
void Main() {
3
var variableName = 25;
4
// but since AnotherProcedure() takes an int...
5
AnotherProcedure( variableName );
6
}
7
8
// ... we can make a copy of its value with a different name
9
void AnotherProcedure(int variableTest ) {
10
// This would create a new local variable equal to 25
11
var test = variableTest;
12
}
Copied!

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.
1
// globalTest exists for as long as the game is running, even after Main()
2
global int globalTest = 50;
3
4
void Main() {
5
globalTest = globalTest + 30;
6
AnotherProcedure();
7
}
8
9
void AnotherProcedure() {
10
// This would create a new local variable equal to 80
11
var test = globalTest;
12
}
Copied!

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.
1
static int staticNumber;
2
3
void Main() {
4
// the first static int will be 10 higher every time the script is run
5
staticNumber = staticNumber + 10;
6
}
Copied!

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.
1
const int constantNumber = 99;
2
3
void Main() {
4
constantNumber = 0;
5
// test would still equal 99
6
var test = constantNumber;
7
}
Copied!

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:
1
void Main() {
2
OutTest( out x, out y );
3
var x + y; // result equaling 35
4
}
5
6
void OutTest( out int x, out int y )
7
{
8
x = 5;
9
y = 30;
10
}
Copied!
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.
Last modified 8mo ago