[SOLVED] Timers Problem

Hello Community, I’m using 2 timers, one of those timers calls the other one (many times), but I have a problem pausing the second timer, because the first timer creates a lot of copies of the second timer and the pause function just pauses the last timer created and I need to pause all those timers. Here’s a sample code:

local timer1  
local timer2  
  
timer1 = timer.performWithDelay ( 1000, startTimer2, 5 )  
  
local function startTimer2 ()  
timer2 = timer.performWithDelay ( 4000, test, 1 )  
end  
  
local function test ()  
print ( "Timer" )  
end  
  
local function pause ()  
timer.pause ( timer2 )  
end  

Note: There are some mistakes to run the code correctly, but there you can see what I trying to do.

I need to pause and resume every timer ( timer2 ) created.

Thanks…

[import]uid: 81091 topic_id: 18908 reply_id: 318908[/import]

You should create a table and store the timers, each with a unique name, then you can use a for loop to pause them all.

Peach :slight_smile: [import]uid: 52491 topic_id: 18908 reply_id: 72861[/import]

But, if I need infinities timers?

(… and sorry, how can I use for loop? This is my first time programming). [import]uid: 81091 topic_id: 18908 reply_id: 73272[/import]

  
gameTimers = {}  
  
local function findTimerSlot()  
 for i = 1, #gameTimers do  
 if gameTimers[i] == nil then  
 return i  
 end  
 end  
 return #gameTimers + 1  
end  
  
local function pauseAllTimers()  
 for i = 1, #gameTimers do  
 if gameTimers[i] ~= nil then  
 timer.pause(gameTimers[i])  
 end  
 end  
end  
  
...  
-- create a new timer:  
  
idx = findTimerSlot()  
gameTimers[idx] = timer.performWithDelay(...)  
  

or some such logic.

When your timer expires or cancels, you probably should make sure to shove a “nil” back into that slot. [import]uid: 19626 topic_id: 18908 reply_id: 73280[/import]

Something like this:

local timer1  
local timer\_table = {}  
   
timer1 = timer.performWithDelay ( 1000, startTimer2, 5 )  
   
local function startTimer2 ()  
 timer\_table[#timer\_table + 1] = timer.performWithDelay ( 4000, test, 1 )  
end  
   
local function test ()  
 print ( "Timer" )  
end  
   
local function pause ()  
 for key,my\_timer in pairs(timer\_table) do  
 timer.pause ( my\_timer )  
 end  
end  

Essentially it pushes a new timer into the table each time you would have started a new object and stored it in timer2. Then when you want to pause it iterates through each timer in the table and pauses it.

EDIT: Looks like robmiracle beat me to it with the pause. Basic concept is the same. [import]uid: 100558 topic_id: 18908 reply_id: 73283[/import]

I did this, but I have a problem (anybody test it please):

local timers = {}  
local i = 0  
local gamepause = false  
  
local function pause (e)  
 if ( gamepause == false and e.phase == "began" ) then  
 timer.pause ( timers[i] )  
 timer.pause ( timer1 )  
 gamepause = true  
 end  
end  
  
Runtime:addEventListener ( "touch", pause )  
  
local function unpause (e)  
 if ( gamepause == true and e.phase == "ended" ) then  
 timer.resume ( timers[i] )  
 timer.resume ( timer1 )  
 gamepause = false  
 end  
end  
  
Runtime:addEventListener ( "touch", unpause )  
  
local function timer2 ()  
 if ( gamepause == false ) then  
 timers[i] = nil  
 i = i - 1  
 print ( "Complete", #timers )  
 end  
end  
  
local function newTimer ()  
 if ( gamepause == false ) then  
 i = i + 1  
 timers[i] = timer.performWithDelay ( 2000 , timer2, 1 )  
 print ( #timers )  
 end  
end  
  
timer1 = timer.performWithDelay ( 1000, newTimer, 0 )  

When I pause the code, I don’t know why the #timers get increased by 1. [import]uid: 81091 topic_id: 18908 reply_id: 73308[/import]

You can’t just say timers[i] as that is only going to get the last timer.

Use a loop

for i = 1, #timers, -1 do  
 timer.pause(timers[i])  
end  

and so on [import]uid: 84637 topic_id: 18908 reply_id: 73361[/import]

I’m still having the same problem, I can pause the game but I don’t know why the #timers get increased by 1 when I unpause the game. [import]uid: 81091 topic_id: 18908 reply_id: 73383[/import]

The error was fixed, but now I need something to identify every timer to remove the expired timers from the table.

Here’s the current code:

[code]
local timers = {}
local gamepause = false
local timer1
local i = 0

local function pause (e)
if ( gamepause == false and e.phase == “began” ) then
timer.pause ( timer1 )
for i = 1, #timers do
timer.pause (timers[i])
end
gamepause = true
end
end

Runtime:addEventListener ( “touch”, pause )

local function unpause (e)
if ( gamepause == true and e.phase == “ended” ) then
gamepause = false
timer.resume ( timer1 )
for i = 1, #timers do
timer.resume (timers[i])
end
end
end

Runtime:addEventListener ( “touch”, unpause )

local function timer2 ()
if ( gamepause == false ) then
print ( “Complete:”, i, timers[i] )
end
end

local function newTimer ()
if ( gamepause == false ) then
i = i + 1
timers[i] = timer.performWithDelay ( 2000 , timer2, 1 )
print ( “Timer:”, i, timers[i] )
end
end

timer1 = timer.performWithDelay ( 1000, newTimer, 0 )
[/code] [import]uid: 81091 topic_id: 18908 reply_id: 73397[/import]

Think about it.

Your resuming timer1 when un-pausing your game and thus continuing to add a newTimer every 1000ms.

Can you give more info on “expired timers” ? [import]uid: 84637 topic_id: 18908 reply_id: 73760[/import]

Danny can you test the code on the simulator? If you test it, you’ll see that the timers are paused without problems, but you can see too that the terminal displays a warning advice about expired timers, and I need to cancel the expired timers and removing from the table. [import]uid: 81091 topic_id: 18908 reply_id: 74077[/import]

Please, somebody help me, I’ve tried to fix this for a week… [import]uid: 81091 topic_id: 18908 reply_id: 74078[/import]

Hello people, after a vacation trip, I’m in the same timer problem, I’m trying to fix it with event.source, with event.source I can identify the timer that calls the next function but I’m trying to find the way to erase that timer from the timers table to stop the warning message from the terminal (expired timer pause).

Please, somebody test this code:

[code]
local timers = {}
local gamepause = false
local timer1
local i = 0

local function pause (e)
if ( gamepause == false and e.phase == “began” ) then
timer.pause ( timer1 )
for i = 1, #timers do
timer.pause (timers[i])
end
gamepause = true
end
end

Runtime:addEventListener ( “touch”, pause )

local function unpause (e)
if ( gamepause == true and e.phase == “ended” ) then
gamepause = false
timer.resume ( timer1 )
for i = 1, #timers do
timer.resume (timers[i])
end
end
end

Runtime:addEventListener ( “touch”, unpause )

local function timer2 (e)
if ( gamepause == false ) then
print ( “Complete:”, e.source )
end
end

local function newTimer ()
if ( gamepause == false ) then
i = i + 1
timers[i] = timer.performWithDelay ( 2000 , timer2, 1 )
print ( “Timer:”, i, timers[i] )
end
end

timer1 = timer.performWithDelay ( 1000, newTimer, 0 )
[/code] [import]uid: 81091 topic_id: 18908 reply_id: 75807[/import]

FINALLY!

[code]
local timers = {}
local gamepause = false
local timer1
local i = 0

local function pause (e)
if ( gamepause == false and e.phase == “began” ) then
timer.pause ( timer1 )
for i = 1, #timers do
timer.pause (timers[i])
end
gamepause = true
end
end

Runtime:addEventListener ( “touch”, pause )

local function unpause (e)
if ( gamepause == true and e.phase == “ended” ) then
gamepause = false
timer.resume ( timer1 )
for i = 1, #timers do
timer.resume (timers[i])
end
end
end

Runtime:addEventListener ( “touch”, unpause )

local function timer2 (e)
if ( gamepause == false ) then
for k,v in pairs ( timers ) do
if ( v == e.source ) then
table.remove ( timers, k )
print ( "Complete: "…k, e.source, #timers )
end
end
end
end

local function newTimer ()
if ( gamepause == false ) then
i = #timers + 1
timers[i] = timer.performWithDelay ( 2000 , timer2, 1 )
print ( "Timer: "…i, timers[i], #timers )
end
end

timer1 = timer.performWithDelay ( 1000, newTimer, 0 )
[/code] [import]uid: 81091 topic_id: 18908 reply_id: 75821[/import]