Author Topic: Variables  (Read 455 times)

What is the difference between a Global variable and a Local variable? In-depth please.

'Global' and 'local' refer to something called 'scope'.

In TS, scope isn't nearly as important as in, say, C++, but it's still useful to know about.

Global scope will be accessible anywhere. Defining a global variable means that no matter what, so long as it exists, you can use it where you need it. The dollar sign ($) precedes variables with global scope in TS.

Local scope is within single scripts or functions and deleted afterward. For instance,

if this is your server.cs
Code: [Select]
%clams = true;
exec("./plants.cs");

you can't do something like this with successful results in plants.cs
Code: [Select]
echo(%clams); //clams doesn't exist in this scope because it's a different script, would echo a blank line
However, if you instead did this:
server.cs
Code: [Select]
$clams = true;
exec("./plants.cs");

plants.cs
Code: [Select]
echo($clams); //would echo 1 because clams is now in global scope and can be used anywhere

If you define

$MyModOn = 1;

You can check/use this from any function.

If you did something like

function calcStuff()
{
    %theCalculation = 10;
}

You cannot reference that variable from anywhere else. You'd either have to return it or store it in a global variable as this local variable here would get deleted at the end of the function.

Any variable prefixed with a $ sign (global) can be used anywhere, in any context.
However if a variable is prefixed with a % sign (local), it cannot be used outside of the current invocation of the function, because it is removed at the end.
Avoid using local variables outside of functions, especially because this can sometimes crash dedicated servers.

Any variable prefixed with a $ sign (global) can be used anywhere, in any context.
However if a variable is prefixed with a % sign (local), it cannot be used outside of the current invocation of the function, because it is removed at the end.
Avoid using local variables outside of functions, especially because this can sometimes crash dedicated servers.
'Global' and 'local' refer to something called 'scope'.

In TS, scope isn't nearly as important as in, say, C++, but it's still useful to know about.

Global scope will be accessible anywhere. Defining a global variable means that no matter what, so long as it exists, you can use it where you need it. The dollar sign ($) precedes variables with global scope in TS.

Local scope is within single scripts or functions and deleted afterward. For instance,

if this is your server.cs
Code: [Select]
%clams = true;
exec("./plants.cs");

you can't do something like this with successful results in plants.cs
Code: [Select]
echo(%clams); //clams doesn't exist in this scope because it's a different script, would echo a blank line
However, if you instead did this:
server.cs
Code: [Select]
$clams = true;
exec("./plants.cs");

plants.cs
Code: [Select]
echo($clams); //would echo 1 because clams is now in global scope and can be used anywhere
If you define

$MyModOn = 1;

You can check/use this from any function.

If you did something like

function calcStuff()
{
    %theCalculation = 10;
}

You cannot reference that variable from anywhere else. You'd either have to return it or store it in a global variable as this local variable here would get deleted at the end of the function.


Thanks!