How to restart a timer.performWithDelay function

So I have this bomb timer function that starts when the level starts. When you restart the level, it should restart the bomb timer. I tried to cancel the timer in the callGameOver function and when you restart the level, it should restart the timer.performWithDelay function in the startGame function. For some reason it just freezes when the callGameOver function is called. When I hit the restart button, the frozen bombs continue where they left off. What is an effective way to restart a timer.performWithDelay function? [import]uid: 69494 topic_id: 30328 reply_id: 330328[/import]

Would need to see code but normally you create timer:
bombTimer = timer.performWithDelay(etc)

Then cancel:
if bombTimer then timer.cancel(bombTimer) bombTimer = nil end

Are you calling the cancel in the game scene or gameover scene? Any errors in the terminal? [import]uid: 52491 topic_id: 30328 reply_id: 121608[/import]

I am calling the cancel in the gameover scene. The gameover screen pops up and then I click on restart but the bombs just freeze and continue where they left off when the game restarts.

Here is bomb timer located in the startGame function:

bombTimer = timer.performWithDelay(800, createBomb, 0)

In the callGameOver function, this is when I cancel the bomb timer:

timer.cancel(bombTimer).

When you click on the restart button, the onRelease = startGame. [import]uid: 69494 topic_id: 30328 reply_id: 121671[/import]

I forgot to mention that there aren’t any errors in the terminal. Everything seems fine and the game timer restarts because of this function: restartTimer = timer.performWithDelay(300, function()callGameOver(); end, 1). Just can’t figure out how to restart the bomb timer. [import]uid: 69494 topic_id: 30328 reply_id: 121672[/import]

Hmmmm. Are you removing all the bombs when you change scenes? Am wondering if you are creating them but inserting them into group you’re removing. [import]uid: 52491 topic_id: 30328 reply_id: 121766[/import]

I am creating them and put them into a group but I am not removing the group. How and where should I do that? [import]uid: 69494 topic_id: 30328 reply_id: 121772[/import]

Hi @neilw711,

Just a quick question: you did create a reference variable for bombTimer outside and above both of these functions, correct? If not, Lua will try to act upon these at the local (function) level, and they won’t be the same variable, thus the timer won’t cancel.

Also… and this might void the above advice… is the gameOver screen a Director or Storyboard module? If so, you need to pass this timer variable to that scene. If you don’t, once again, Lua won’t know what timer reference you’re trying to cancel.

Please let us know, thanks!
Brent [import]uid: 9747 topic_id: 30328 reply_id: 121806[/import]

@Brent,

I did create the variable outside of the functions. I am not currently using storyboard nor director class yet. Here’s the order:

[lua] local bombTimer

local function startGame()
bombTimer = timer.performWithDelay(800, createBomb, 0)
end

createBomb = function
etc etc
end

restartBtn = widget.newButton{
id = “restart”,
defaultColor = { 0, 128, 128 } ,
left = 0,
top = 30,
label = “Restart Game”,
labelColor = { default = { 255 }, over = { 0 } } ,
width = 100, height = 50,
cornerRadius = 8,
onRelease = startGame
}

callGameOver = function()
timer.cancel(bombTimer)
end

startGame()
[lua] [import]uid: 69494 topic_id: 30328 reply_id: 121840[/import]

I don’t see any problems with this code at first glance…

You could try making all of these functions (and the widget button) local instead of global (obviously “startGame()” is local, but try making all of them local). And, you might try making the button a standard button instead of using the widget library… it’s a long shot, but who knows, could be some little bug with the widget library.

This is definitely odd though… timers cancel properly for me, so my best guess is that this is a scope issue, local vs. global (the latter being avoided unless absolutely necessary!).

Brent
[import]uid: 9747 topic_id: 30328 reply_id: 121853[/import]

Thanks Brent, I’ll try that when I get home. Since we’re on the timer subject, quick question, how can I set the bomb timer to spawn bombs and then stop for 10 seconds then spawn bombs again? [import]uid: 69494 topic_id: 30328 reply_id: 121857[/import]

Hi again,

In this case (a “pausing” timer), just pause bombTimer in the function “createBomb” using timer.pause(), then start another timer that executes a function to resume bombTimer after 10 seconds. You’ll want to assign this “restart” timer to another reference variable in case the user restarts the game while those 10 seconds are elapsing… in which case, you’d want to cancel that restart timer and the paused bombTimer.

So basically, this procedure:

  1. Start bombTimer (800 milliseconds, it appears to be set at).
  2. When the target function (createBomb) executes, pause bombTimer.
  3. Start a new timer (10 seconds) that executes a function like “resumeTimer()”
  4. When this function executes, resume bombTimer using timer.resume()

That should do it! If you still have problems with the timers not canceling properly, let me know because I have an alternative method which might work a bit better.

Brent
[import]uid: 9747 topic_id: 30328 reply_id: 121907[/import]

Would need to see code but normally you create timer:
bombTimer = timer.performWithDelay(etc)

Then cancel:
if bombTimer then timer.cancel(bombTimer) bombTimer = nil end

Are you calling the cancel in the game scene or gameover scene? Any errors in the terminal? [import]uid: 52491 topic_id: 30328 reply_id: 121608[/import]

I am calling the cancel in the gameover scene. The gameover screen pops up and then I click on restart but the bombs just freeze and continue where they left off when the game restarts.

Here is bomb timer located in the startGame function:

bombTimer = timer.performWithDelay(800, createBomb, 0)

In the callGameOver function, this is when I cancel the bomb timer:

timer.cancel(bombTimer).

When you click on the restart button, the onRelease = startGame. [import]uid: 69494 topic_id: 30328 reply_id: 121671[/import]

I forgot to mention that there aren’t any errors in the terminal. Everything seems fine and the game timer restarts because of this function: restartTimer = timer.performWithDelay(300, function()callGameOver(); end, 1). Just can’t figure out how to restart the bomb timer. [import]uid: 69494 topic_id: 30328 reply_id: 121672[/import]

Hmmmm. Are you removing all the bombs when you change scenes? Am wondering if you are creating them but inserting them into group you’re removing. [import]uid: 52491 topic_id: 30328 reply_id: 121766[/import]

I am creating them and put them into a group but I am not removing the group. How and where should I do that? [import]uid: 69494 topic_id: 30328 reply_id: 121772[/import]

Hi @neilw711,

Just a quick question: you did create a reference variable for bombTimer outside and above both of these functions, correct? If not, Lua will try to act upon these at the local (function) level, and they won’t be the same variable, thus the timer won’t cancel.

Also… and this might void the above advice… is the gameOver screen a Director or Storyboard module? If so, you need to pass this timer variable to that scene. If you don’t, once again, Lua won’t know what timer reference you’re trying to cancel.

Please let us know, thanks!
Brent [import]uid: 9747 topic_id: 30328 reply_id: 121806[/import]

@Brent,

I did create the variable outside of the functions. I am not currently using storyboard nor director class yet. Here’s the order:

[lua] local bombTimer

local function startGame()
bombTimer = timer.performWithDelay(800, createBomb, 0)
end

createBomb = function
etc etc
end

restartBtn = widget.newButton{
id = “restart”,
defaultColor = { 0, 128, 128 } ,
left = 0,
top = 30,
label = “Restart Game”,
labelColor = { default = { 255 }, over = { 0 } } ,
width = 100, height = 50,
cornerRadius = 8,
onRelease = startGame
}

callGameOver = function()
timer.cancel(bombTimer)
end

startGame()
[lua] [import]uid: 69494 topic_id: 30328 reply_id: 121840[/import]

I don’t see any problems with this code at first glance…

You could try making all of these functions (and the widget button) local instead of global (obviously “startGame()” is local, but try making all of them local). And, you might try making the button a standard button instead of using the widget library… it’s a long shot, but who knows, could be some little bug with the widget library.

This is definitely odd though… timers cancel properly for me, so my best guess is that this is a scope issue, local vs. global (the latter being avoided unless absolutely necessary!).

Brent
[import]uid: 9747 topic_id: 30328 reply_id: 121853[/import]

Thanks Brent, I’ll try that when I get home. Since we’re on the timer subject, quick question, how can I set the bomb timer to spawn bombs and then stop for 10 seconds then spawn bombs again? [import]uid: 69494 topic_id: 30328 reply_id: 121857[/import]