The compiler reads your code like a book, from start to finish. If you don't declare variables before you use them, they'll always be nil (unless they exist in a higher scope). As an example:
Lua Code:
-- myVariable is declared, but is nil
local myVariable
-- this function can access myVariable, because myVariable was declared beforehand,
-- even though myVariable has no value assigned to it.
local function whatIsMyVariable()
print("My variable is: ", myVariable)
end
whatIsMyVariable()
-- prints: My variable is nil
myVariable = "abc"
-- myVariable is given a value
whatIsMyVariable()
-- prints: My variable is abc
Lua Code:
-- this function has no idea what myVariable is,
-- because it didn't exist before this function was declared.
local function whatIsMyVariable()
print("My variable is: ", myVariable)
end
local myVariable
whatIsMyVariable()
-- prints: My variable is nil
myVariable = "abc"
-- myVariable is given a value
whatIsMyVariable()
-- prints: My variable is nil