Let me add on to this.
When you see an API call that expects a function, it’s looking for the “Address of the function”. In Lua when you put () after a function name, it runs the function and passes the results of that function to the API call, which more often than not, isn’t a function. Most functions don’t return anything. If you don’t use () you get the address of the function. Consider this:
local function doSomething( someNumber ) local someOtherNumber = someNumber + 10 end timer.performWithDelay( 1000, doSomething( 10 ) )
What happens in this case, is the function doSomething runs immediately. someOtherNumber is calculated but the function doesn’t return anything, so the system will return “nil” for you. This means you really are saying:
timer.performWithDelay( 1000, nil )
Now timer.performWithDelay() has nothing to do. Let’s look at another example:
local function doSomething( someNumber ) local someOtherNumber = someNumber + 10 return someOtherNumber end timer.performWithDelay( 1000, doSomething( 10 ) )
In this case, doSomething is run immediately, 10 is added to 10, temporarily stored in the variableSomeOtherNumber and then that value is returned. This makes the timer really this:
timer.performWithDelay( 1000, 20 )
20 is an invalid memory address.
Now let’s say the doSomething function is at memory location 0x98761234. If you do:
timer.performWithDelay( 1000, doSomething ) -- no parameters passed
the timer call becomes:
timer.performWithDelay( 1000, 0x98761234 )
and the timer.performWithDelay() API now has a function to execute.
When you write:
timer.performWithDelay( 1000, function() doSomething(10); end )
The function() end pair creates an anonymous function, that is it’s not storing the address to the function in a variable, but it **IS** returning an address to a function to timer.performWithDelay(). Then in 1000 ms, that anonymous function is run which calls doSomething( 10 ), passing the value to the function.
Hope this helps you understand the problem and why Nick’s solution works.
Rob