@Asamak. You need to pay attention to and know the Lua scoping rules. The order of function declarations are important. For instance this won’t work:
function update(self, dt)
foo()
end
local function foo()
print("bar")
end
In the case above foo() must be declared before it’s used. This on the other hand works:
local function foo()
print("bar")
end
function update(self, dt)
foo()
end
If you have two functions that both need to call each other in under certain conditions you can forward declare one or both of the functions. This will not work:
local function bar()
foo() -- foo has not yet been declared
end
local function foo()
bar()
end
But this will:
local foo
local function bar()
foo()
end
foo = function()
bar()
end
You should make a practice of using local variables and functions as much as possible. If you don’t use the local
keyword there’s a high probability of unexpected results unless you know exactly what you’re doing. If you don’t use local
the variable/function will be globally available. If you use the same script on multiple game objects they will all read and write to the same variable, and that might not be desired. Read more about local variables here.
As an advanced bonus topic you should also try to learn the concept of closures and upvalues in Lua.