Post by Quintaxel on Feb 4, 2017 13:58:52 GMT 1
bb
In Lua, if you do not declare a variable as local, then the scope of this variable will be, in practice, your map script. So the scope of this global variable will be all functions in your script.
Now the 'local' declaration is used to limit the scope of a variable to a block of data.
The most commonly block of data is a function.
If you declare variables as local then the scope of these variables is limited to the block of data were they are declared.
So if you declare a local variable in a function then that variable will only be valid for that specific function.
You can also further limit the scope of a variable if you declare it an even smaller blocks like a <while.... do> loop or a <for...do> block. If needed we can go deeper into this.
Suppose you have declared the variable <bb> as a global variable in you map script
The scope of the varbiable bb is the whole script, so all functions.
So in this case the value of the variable bb will be 100 for all functions.
bb = 100;
function a ()
DisplayTrace ("The value of bb = %g",bb); --Displays the value 100
Suicide();
end;
function b ()
DisplayTrace ("The value of bb = %g",bb); --Displays the value 100
Suicide ();
end;
It's important to now that if you declare bb as a local variable in a specific function (or block) then the value given to the local value bb will override the global value (100 in the above example). By declaring bb as local in a function you narrowed down it's scope to that particular function. It all sound more complicated than it is. Just take a look at the example below.
bb = 100;
function a ()
local bb = bb * 10; -- The local value of bb = the global value of 100 x 10
DisplayTrace ("The value of bb = %g",bb); --Displays the value 1000
Suicide();
end;
function b ()
DisplayTrace ("The value of bb = %g",bb); --Displays the global value 100
Suicide ();
end;
Global variables do not have to be explicitly declared as global. All you need to do is assign a value to a global variable to create it
x = 10; --> x is a global variable with the value 10
x --> x is a global variable with the value nil. If you do not give a value to the variable then is gets the value nil by default.
It's good practice to use local variables when you can as they can be accessed faster by Lua.
I hope this helps
edit: KeepitSimple's reply was faster and....simple so better
In Lua, if you do not declare a variable as local, then the scope of this variable will be, in practice, your map script. So the scope of this global variable will be all functions in your script.
Now the 'local' declaration is used to limit the scope of a variable to a block of data.
The most commonly block of data is a function.
If you declare variables as local then the scope of these variables is limited to the block of data were they are declared.
So if you declare a local variable in a function then that variable will only be valid for that specific function.
You can also further limit the scope of a variable if you declare it an even smaller blocks like a <while.... do> loop or a <for...do> block. If needed we can go deeper into this.
Suppose you have declared the variable <bb> as a global variable in you map script
The scope of the varbiable bb is the whole script, so all functions.
So in this case the value of the variable bb will be 100 for all functions.
bb = 100;
function a ()
DisplayTrace ("The value of bb = %g",bb); --Displays the value 100
Suicide();
end;
function b ()
DisplayTrace ("The value of bb = %g",bb); --Displays the value 100
Suicide ();
end;
It's important to now that if you declare bb as a local variable in a specific function (or block) then the value given to the local value bb will override the global value (100 in the above example). By declaring bb as local in a function you narrowed down it's scope to that particular function. It all sound more complicated than it is. Just take a look at the example below.
bb = 100;
function a ()
local bb = bb * 10; -- The local value of bb = the global value of 100 x 10
DisplayTrace ("The value of bb = %g",bb); --Displays the value 1000
Suicide();
end;
function b ()
DisplayTrace ("The value of bb = %g",bb); --Displays the global value 100
Suicide ();
end;
Global variables do not have to be explicitly declared as global. All you need to do is assign a value to a global variable to create it
x = 10; --> x is a global variable with the value 10
x --> x is a global variable with the value nil. If you do not give a value to the variable then is gets the value nil by default.
It's good practice to use local variables when you can as they can be accessed faster by Lua.
I hope this helps
edit: KeepitSimple's reply was faster and....simple so better