How do variables and objects persist in Lua?
Members - Reputation: 152
Posted 14 November 2012 - 03:02 PM
Sorry if this is a confusing question.
Crossbones+ - Reputation: 3061
Posted 14 November 2012 - 04:50 PM
Now, when you declare a variable local, it is only alive during the execution of that function. Say you have this function:
function foo() local bar=4 end
The only time bar is valid is when foo is executing. Before foo executes, bar doesn't exist. After foo exits, bar is marked for garbage collection. Similarly, any variable declared local to a Lua file is only valid when the compiled chunk loaded from that file is actually executing. Once it is no longer executing, any locals will be garbage collected.
If you want a value to be persistent across multiple calls to the chunk, it has to exist outside of the local scope of the chunk.
Edited by FLeBlanc, 14 November 2012 - 04:50 PM.