Local: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
A local variable only exists within the function it is specified. Therefore, it exists between "on function" and the functions "end" point.
A local variable only exists within the scope it is specified. The scope is the 'level' it is visible for the script and thus readable and editable.


Example:
==Usage==
<syntaxhighlight lang="lua">local playermoney = getPlayerMoney ( source )</syntaxhighlight>
You should use local variables anytime it is possible, when you don't need to access them globally. This saves you from having troubles with duplicate variable names.
 
==Examples==
<syntaxhighlight lang="lua">
function showMoney(source)
local playermoney = getPlayerMoney ( source )
outputChatBox(playermoney)
end
</syntaxhighlight>
 
The ''playermoney'' variable only exists within the 'showMoney' function.
 
 
<syntaxhighlight lang="lua">
outputChatBox("This is your status:",player)
if (showHealth == true) then
local health = getPlayerHealth(player)
outputChatBox("Heahlth: "..health,player)
end
</syntaxhighlight>
 
The ''health'' variable only exists within the ''if''-condition block, that is between the 'then' and the 'end'.

Revision as of 19:27, 7 August 2007

A local variable only exists within the scope it is specified. The scope is the 'level' it is visible for the script and thus readable and editable.

Usage

You should use local variables anytime it is possible, when you don't need to access them globally. This saves you from having troubles with duplicate variable names.

Examples

function showMoney(source)
	local playermoney = getPlayerMoney ( source )
	outputChatBox(playermoney)
end

The playermoney variable only exists within the 'showMoney' function.


outputChatBox("This is your status:",player)
if (showHealth == true) then
	local health = getPlayerHealth(player)
	outputChatBox("Heahlth: "..health,player)
end

The health variable only exists within the if-condition block, that is between the 'then' and the 'end'.