The scope, or focus, is where all your scripting takes place. A scope is marked by a opening curly bracket { and a matching closing curly bracket } everything in between these brackets is the focus of the script. Every script must have at least one scope but it can have as many as you want.
void main()
{
// This is the main scope, every script must have this scope
}
Every variable declared within the main scope becomes "global" or "usable" to the entire script. Note that it is NOT global to all scripts, just the script that it is declared in. You can have a scope within a scope,
void main()
{
// Variables define here can be used anywhere in the script
{
// This is a second scope, variables defined here are only usable within this scope.
}
}
Note the comment, // This is a second scope, variables defined here are only usable within this scope. Inside the second scope. This means that if I declare a variable inside that scope, I can't use it inside the main scope. Here is an example of a common mistake by Noobs,
void main()
{
{
int I = 10;
}
PrintInteger(I);
}
Try to compile that script and you will get a ERROR: VARIABLE DEFINED WITHOUT TYPE error because the variable I is declared inside the second scope and not accessible by the main scope so the compiler has no idea what the letter I is. Likewise, doing this;
void main()
{
int I;
{
int I = 10;
}
PrintInteger(I);
}
Will allow the script to compile, however the variable that prints by PrintInteger(I) won't be what you might expect. The Value printed is 0.
But the same variable was declared twice, shouldn't I get an error? Nope, because they are declared in different scopes, the variables are seen as two different variables. So int I in the main scope is not the same variable as int I in the second scope.
Don't worry too much if you don't fully understand this, it is just important to know and keep tucked in the back of your mind. This is also a very good reason why all variables should be Declared at the top of the main scope - as I will cover more in the next section, unless you have specific reason to declare them within a second scope - but that's much later.