Perform first, then delay

So I want when the user touches a weapon, the weapon should fire bullets. I’m using:

timer.performWithDelay( 3000, fire, 0 )

The problem is that the timer waits first then executes the function, which means that when the touch event occurs the weapon will not fire instantly, but instead it will wait for 3 seconds. 

I couldn’t find another type of timer that does the opposite, which is to execute the function first and then make the delay. 

You could just do:

fire() timer.performWithDelay( 3000, fire, 0 )

which would call the function immediately then allow the timer to progress.

I strongly suggest you NOT use non-ending timers as a solution for everything.  Better to simply call timer again.

i.e. This (blech!)

local function fire() -- some code here -- This will never end so it isn't really useful and you'll need to cancel it which implies -- You'll need to track the timer handle end timer.performWithDelay( 3000, fire, 0 ) 

versus this (better):

local function fire() -- some code here -- Call same function again (now you can add smarts to skip this or do it again) timer.performWithDelay( 3000, fire ) end -- This still waits 3 seconds the first time timer.performWithDelay( 3000, fire ) 

Then, you can just call the function to get an immediate ‘firing of the weapon’:

local firing = false local function fire() -- some code here if( not firing ) then return end timer.performWithDelay( 3000, fire ) end local function onTouch( self, event ) if( event.phase == "began") then firing = true fire() elseif( event.phase == "ended" ) then firing = false end end local fireButton = display.newRect( 100, 100, 100, 100 ) fireButton.touch = onTouch fireButton:addEventListener( "touch" )

I already have almost the same structure with conditionals, but it’s not consistent, sometimes the player fires then wait the intended amount of time before firing again, and sometimes it fires and after less amount of time than expected it fires. 

It would have been more flexible if we can manipulate the timer function, for example, what if I want to spawn different kinds of enemies based on time, I would have used something like this:

timer.performWithDelay( enemy1, 3000, enemy2, 2000, enemy3, 4000 )
  1. Your problems are probably caused by scope and globals.

  2. I don’t understand that last question, however that is not how you use timer.performWithDelay()

https://docs.coronalabs.com/daily/api/library/timer/performWithDelay.html

I know that, I was saying that it would be better and more flexible if we were allowed to use it like this. 

I don’t agree w/ the assessment that it would be better that way, but…

Corona is an SDK (software development kit) and Lua is easy to use, so nothing is stopping you implementing your own timer.performWithDelay

function timer.performWithDelay2( ... ) for i = 1, #arg, 2 do timer.performWithDelay( arg[i+1], arg[i] ) end end local function func1() print("You can solve") end local function func2() print("These problems") end local function func3() print("On your own through coding.") end local function func4() print("That is the beauty of an SDK.") end timer.performWithDelay2( func1, 500, func2, 1000, func3, 1500, func4, 2000 )

Is this function some sort of extension to the timer library? 

Maybe because I’m used to it this way coming from SpriteKit. In SpriteKit there is that thing called “Actions” they are the equivalent of transitions in corona, but they have more functions than (move, scale, rotate) there is something called:

-“skAction.waitForDuration” 

-“skAction.runBlock” <-- run block of code.

You can create a sequence of actions and then run them on any object you want or the scene itself:

skAction.sequence([function, wait, function, wait, function, wait]) scene.runAction(sequence)

To me, it’s easier that way for something like spawn different kinds of enemies at different times, but if there is a more efficient way to achieve that, please suggest it to me.

That is a function I just wrote to do it the way you wanted to do timers.

Well, the way SpriteKit does it is neither here or there.  performWithDelay is unlikely to ever change as that would break all kinds of old code.

As I suggested, if you need helpers or extensions to existing libraries, simply add them.  That is a benefit of working with Lua.  It is SUPER easy to extend existing libraries and modules.

I get that, I was asking if this is how you make an extension in Lua, because I googled “Lua library extensions” and couldn’t find anything useful. 

Ah, I see. 

Yes, this is a way to extend the functionality of a library/module.

Lua is very friendly that way.  You can attach new functions and fields to any table and a module/library is nothing more than a table with fields and functions attached to it.

Wonderful, thanks a lot. 

I’m trying to cancel a timer but it doesn’t get canceled when it should:

local fireTimer local function onTouch( self, event ) if( event.phase == "began") then fireTimer =&nbsp;timer.performWithDelay( 3000, fire, 0&nbsp;)&nbsp;\<-- works fine elseif( event.phase == "ended" ) then timer.cancel( fireTimer ) \<-- doesn't work, it keeps going, "pause" doesn't work either end end

Can you put a print statement inside the elseif clause to make sure you’re getting to that part of the code?

the “ended” phase does get executed, it’s the first thing I checked. 

You could just do:

fire() timer.performWithDelay( 3000, fire, 0 )

which would call the function immediately then allow the timer to progress.

I strongly suggest you NOT use non-ending timers as a solution for everything.  Better to simply call timer again.

i.e. This (blech!)

local function fire() -- some code here -- This will never end so it isn't really useful and you'll need to cancel it which implies -- You'll need to track the timer handle end timer.performWithDelay( 3000, fire, 0 ) 

versus this (better):

local function fire() -- some code here -- Call same function again (now you can add smarts to skip this or do it again) timer.performWithDelay( 3000, fire ) end -- This still waits 3 seconds the first time timer.performWithDelay( 3000, fire ) 

Then, you can just call the function to get an immediate ‘firing of the weapon’:

local firing = false local function fire() -- some code here if( not firing ) then return end timer.performWithDelay( 3000, fire ) end local function onTouch( self, event ) if( event.phase == "began") then firing = true fire() elseif( event.phase == "ended" ) then firing = false end end local fireButton = display.newRect( 100, 100, 100, 100 ) fireButton.touch = onTouch fireButton:addEventListener( "touch" )

I already have almost the same structure with conditionals, but it’s not consistent, sometimes the player fires then wait the intended amount of time before firing again, and sometimes it fires and after less amount of time than expected it fires. 

It would have been more flexible if we can manipulate the timer function, for example, what if I want to spawn different kinds of enemies based on time, I would have used something like this:

timer.performWithDelay(&nbsp;enemy1,&nbsp;3000,&nbsp;enemy2, 2000, enemy3, 4000&nbsp;)
  1. Your problems are probably caused by scope and globals.

  2. I don’t understand that last question, however that is not how you use timer.performWithDelay()

https://docs.coronalabs.com/daily/api/library/timer/performWithDelay.html

I know that, I was saying that it would be better and more flexible if we were allowed to use it like this.