[Solved] [Lua] Running a function with a timer

Status
Not open for further replies.
Level 9
Joined
Jul 30, 2018
Messages
445
Hey!

I have another Lua related question. Why does this work:
Lua:
function TestTimer()
    TimerStart(CreateTimer(), 1, true, function()
        print("Timer Message")
    end)
end
But not this:
Lua:
function TimerMessage()
    print("Timer Message")
end

function TestTimer()
    TimerStart(CreateTimer(), 1, true, TimerMessage())
end

The latter one will run once, but not periodically.
 
Because currently you call the function TimerMessage() when you are starting the Timer, the returned value is then taken as callback for TimerStart, in the current code that is nothing (nil).
Probably you only want to give the timer a reference to that function. Remove "()" inside TimerStart, then TimerMessage is not called and the function is given to the timer.
 
Status
Not open for further replies.
Top