How to call a function after a certain amount of time has elapsed? (SOLVED)

Hello, I hope that everything is well. I have a (hopefully quick) question.

I am trying to make it so that a local function is called as true, then (x) seconds later it is changed to true… how might I go about doing this?

Thanks so much!

I’m not sure exactly what you’re trying to do, but if you want a timer, you can use timer.delay(). https://defold.com/ref/timer/

Ok, I will take a look into that, thanks. I am mainly trying to accomplish being able to switch a local variable from false to true after a certain amount of seconds. Thanks for your help!

why we cannot pause the timer after i set the timer? defold only give 2 functions for timer.

  1. timer.delay()
  2. timer.cancel()

i suggest you to create new functions like follows.

  1. timer.pause()
  2. timer.resume()

corona sdk engine has all of these four functions. please make it right.

3 Likes

Yeah, I agree it would be great to have such functions internally! Meanwhile, you can use wrapper functions for this, e.g. I wrote an example here: https://forum.defold.com/t/big-list-of-defold-pro-tips/1519/98

3 Likes

Hi there, i have a little question.
when i use timer.delay() function for a something, why should i write callback function without () ?
like follows…

local function create_obj()
    local player = factory.create("#factory_player",vmath.vector3(),nil,nil,nil)
end

timer.delay(10,true,create_obj)  -- in this code why did i use the callback function without () ?

please teach me if there is someone who knows well about that.
thank you

1 Like

If you add the () it would mean that you call the function immediately. You want to pass the function as a value to the timer.delay() function. It will be the timer.delay() function that then calls your callback when the timer triggers.

1 Like

thank you britzl, that is right…

can you give me some websites to learn that well

1 Like

It’s part of the Lua language. Here’s some resource to learn Lua: https://defold.com/manuals/lua/#lua-books-and-resources

2 Likes

ok, thank you very much, i am sure this will be very usful one…

1 Like