• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[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