Local

From GMod Wiki

Revision as of 11:21, 27 August 2007 by Robbis 1 (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Local variables can be freely accessed by functions defined inside their scope. A local variable used by an inner function is called an upvalue, or external local variable, inside the inner function. Unlike global variables, local variables have their scope limited to the block where they are declared. A block is the body of a control structure, the body of a function, or a chunk (the file or string with the code where the variable is declared). It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones.

A common idiom in Lua is

 
local foo = foo
 

This code creates a local variable, foo, and initializes it with the value of the global variable foo. That idiom is useful when the chunk needs to preserve the original value of foo even if later some other function changes the value of the global foo; it also speeds up access to foo.

More Info

http://www.lua.org/pil/4.2.html

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox