timer delay value auto-update?

Hello, i have a question about timers. this is my function
 

local loopy = {

 i=1,

 k=1,

 timer = function(self,event)

print(“i k =”,self.i,self.k)

self.k=self.k+1

media.playEventSound( beep )

if (self.k>4) then

 self.i,self.k = self.i+1,1

 if (self.i>bars) then

self.i = 1

bpmStart=bpmStart+incremento

print("BPM = "…bpmStart)

dTS=(60000/bpmStart)

print("Delay Time = "…dTS)

self.dTS = self.dTS

lordTimer = timer.performWithDelay(dTS,loopy,-1)

end

my question is: when at the end the function assign a new value for the dTS variable, is there a way to make the timer update the dTS variable value it has while the function is running?

thanks in advance

  1. Please always format your code with the tools provided by the forums post editor (see image below for instructions).  You’ll get more answers this way.

formatyourcode.jpg

  1. You want the function, which is being run by the timer to adjust the next perform with delay?  If “Yes,” then try this to understand the concept:

    – I had trouble reading your your code, so I made up an example. – It should still spell out how to achieve your goal. local function onTimer( self, event ) print("Hello from ", self.myName, " @ ", system.getTimer() ) self.mydelay = math.random(5000,8000) – Random 5 … 8 second delay timer.performWithDelay( self.mydelay, self ) end local tmpa = display.newCircle( 100,100,10) local tmpb = display.newCircle( 100,200,10) tmpa.myName = “Bob” tmpb.myName = “Bill” tmpa.timer = onTimer tmpb.timer = onTimer tmpa.mydelay = 4000 tmpb.mydelay = 6000 timer.performWithDelay( tmpa.mydelay, tmpa ) timer.performWithDelay( tmpb.mydelay, tmpb )

Note: As you can see by the example, it is not necessary to keep the variable in the object if it will be calculated in the function anyways.  The real lessons here are:

a. Avoid infinite timers except in simple cases.

b. Assign a function to the ‘timer’ field on an object and pass the object instead of a closure to the performWithDelay() function.

local loopy = { i=1, --initalize the counters for the two for loops i need k=1, timer = function(self,event) print("i k =",self.i,self.k) self.k=self.k+1 --this is the inner loop: the counter is used to play the right amount of sounds media.playEventSound( beep ) --as the function starts, a beep is played and so is every time k is increased if (self.k\>4) then --as k reach the 4, the i counter is increased by 1 and the k counter restarts from 1 self.i,self.k = self.i+1,1 if (self.i\>bars) then -- this is the external circle: used to play the right amount of sounds quartets self.i = 1 bpmStart=bpmStart+incremento --when both of the loops are done, the variable bpmStart gets a new value --bpmStart and incremento are both defined by user inputs dTS=(60000/bpmStart) --as bmpStart update its value so does the dTS variable --this last part is supposed to: --1) calculate a new value for the dTS variable --2) restar the count of the primary for loop as i reach the same value of the variable bars --everything works right but the timer does not get the new dTS value --so the loop just restart with the previous delay time --This is my problem: i don't know how to pass the new dTS value to my timer end end } self.lordTimer = timer.performWithDelay(dTS,loopy,-1)

Hello, first of all thanks for your help.
i repost my code with some comments around hoping to clarify better my doubts and the code itself.

also made a test of your example and the compiler signals me this error:
main.lua:26: attempt to index global ‘timer’ (a function value) stack traceback

but i think i understood your solution
 

NOTE - I hope you’re reading this in the forums and not from the e-mail you may be getting.  I always post, edit, tweak, etc.  So if you read the initial e-mail, you’ll miss corrections.

My bad, I named the function timer, I should have called it something else.  I’ve corrected the code above and it will run fine now.  Brain compile failure I guess.

Be aware, I often write answer code and do not test it.  So, it is possible I’ll make a syntax (or in the prior case a naming) error. These should be easy to ferret out and fix when you try the code.

Thanks for posting the code in a block, but it is still a bit of a hot mess to read.   Having said that, I’ve taken the liberty of re-posting it with these changes:

  • Added spaces (code w/o spaces is super hard to read and maintain)

  • Added indentation

  • Removed comments (I know.  You were elaborating, but they were interfering with reading and once removed and formatted, the structure became clear without comment).

    local loopy = { i = 1, k = 1, timer = function( self, event ) print( “i k =”, self.i, self.k ) self.k = self.k+1 media.playEventSound( beep ) if( self.k > 4 ) then self.i, self.k = self.i+1,1 if( self.i > bars ) then self.i = 1 bpmStart = bpmStart + incremento dTS = ( 60000 / bpmStart ) end end end } self.lordTimer = timer.performWithDelay(dTS,loopy,-1)

Next, let me propose you NOT create  a new function every time you make a ‘loopy’ table

local function onTimer( self, event ) print("i k =", self.i, self.k) self.k = self.k + 1 media.playEventSound( beep ) if( self.k \> 4 ) then self.i, self.k = self.i + 1, 1 if ( self.i \> bars ) then self.i = 1 bpmStart = bpmStart + incremento dTS = ( 60000 / bpmStart ) end end end local loopy = { i=1, k=1, timer = onTimer }

Next,  I’ve looked at the code and there are issues:

  • dTS, bpmStart, incremento… are all globals.  I’ll change that in a sec.
  • I don’t understand the last line and why you say: self.lordTimer… Is all of this code inside anther function with a ‘self’ context?  If not you’re not understanding the meaning of self. 

Finally, let me modify your code to what I think you intend below.  I’ve also added my own extras:

local function onTimer( self, event ) print("i k =", self.i, self.k) self.k = self.k + 1 media.playEventSound( beep ) if( self.k \> 4 ) then self.i = self.i + 1 self.k = 1 if ( self.i \> bars ) then self.i = 1 self.bpmStart = self.bpmStart + self.incremento self.dTS = ( 60000 / self.bpmStart ) end end timer.performWithDelay( self.dTS, self ) end local function doLoopy( bpmStart, incremento, dTS ) local loopy = { i = 1, k = 1, bpmStart = bmpStart, incremento = incremento, dTS = dTS, timer = onTimer } timer.performWithDelay( dTS, loopy ) return loopy end

doLoopy( 10, 1, 1000 ) doLoopy( 20, 1, 2000 )

Note: I made several re-edits to the above ‘final’ bit of code, but I’m done now.

thanks a lot @roaminggamer!!! you gave me a lot of advices and you are helping me so much =D 

i’m about to fix my code with your last indications, i’ll let you know what happend.

sorry for that self.lordTimer. got caught by the good ol’copy paste error and did not notice it… my fault =P

 

  1. Please always format your code with the tools provided by the forums post editor (see image below for instructions).  You’ll get more answers this way.

formatyourcode.jpg

  1. You want the function, which is being run by the timer to adjust the next perform with delay?  If “Yes,” then try this to understand the concept:

    – I had trouble reading your your code, so I made up an example. – It should still spell out how to achieve your goal. local function onTimer( self, event ) print("Hello from ", self.myName, " @ ", system.getTimer() ) self.mydelay = math.random(5000,8000) – Random 5 … 8 second delay timer.performWithDelay( self.mydelay, self ) end local tmpa = display.newCircle( 100,100,10) local tmpb = display.newCircle( 100,200,10) tmpa.myName = “Bob” tmpb.myName = “Bill” tmpa.timer = onTimer tmpb.timer = onTimer tmpa.mydelay = 4000 tmpb.mydelay = 6000 timer.performWithDelay( tmpa.mydelay, tmpa ) timer.performWithDelay( tmpb.mydelay, tmpb )

Note: As you can see by the example, it is not necessary to keep the variable in the object if it will be calculated in the function anyways.  The real lessons here are:

a. Avoid infinite timers except in simple cases.

b. Assign a function to the ‘timer’ field on an object and pass the object instead of a closure to the performWithDelay() function.

local loopy = { i=1, --initalize the counters for the two for loops i need k=1, timer = function(self,event) print("i k =",self.i,self.k) self.k=self.k+1 --this is the inner loop: the counter is used to play the right amount of sounds media.playEventSound( beep ) --as the function starts, a beep is played and so is every time k is increased if (self.k\>4) then --as k reach the 4, the i counter is increased by 1 and the k counter restarts from 1 self.i,self.k = self.i+1,1 if (self.i\>bars) then -- this is the external circle: used to play the right amount of sounds quartets self.i = 1 bpmStart=bpmStart+incremento --when both of the loops are done, the variable bpmStart gets a new value --bpmStart and incremento are both defined by user inputs dTS=(60000/bpmStart) --as bmpStart update its value so does the dTS variable --this last part is supposed to: --1) calculate a new value for the dTS variable --2) restar the count of the primary for loop as i reach the same value of the variable bars --everything works right but the timer does not get the new dTS value --so the loop just restart with the previous delay time --This is my problem: i don't know how to pass the new dTS value to my timer end end } self.lordTimer = timer.performWithDelay(dTS,loopy,-1)

Hello, first of all thanks for your help.
i repost my code with some comments around hoping to clarify better my doubts and the code itself.

also made a test of your example and the compiler signals me this error:
main.lua:26: attempt to index global ‘timer’ (a function value) stack traceback

but i think i understood your solution
 

NOTE - I hope you’re reading this in the forums and not from the e-mail you may be getting.  I always post, edit, tweak, etc.  So if you read the initial e-mail, you’ll miss corrections.

My bad, I named the function timer, I should have called it something else.  I’ve corrected the code above and it will run fine now.  Brain compile failure I guess.

Be aware, I often write answer code and do not test it.  So, it is possible I’ll make a syntax (or in the prior case a naming) error. These should be easy to ferret out and fix when you try the code.

Thanks for posting the code in a block, but it is still a bit of a hot mess to read.   Having said that, I’ve taken the liberty of re-posting it with these changes:

  • Added spaces (code w/o spaces is super hard to read and maintain)

  • Added indentation

  • Removed comments (I know.  You were elaborating, but they were interfering with reading and once removed and formatted, the structure became clear without comment).

    local loopy = { i = 1, k = 1, timer = function( self, event ) print( “i k =”, self.i, self.k ) self.k = self.k+1 media.playEventSound( beep ) if( self.k > 4 ) then self.i, self.k = self.i+1,1 if( self.i > bars ) then self.i = 1 bpmStart = bpmStart + incremento dTS = ( 60000 / bpmStart ) end end end } self.lordTimer = timer.performWithDelay(dTS,loopy,-1)

Next, let me propose you NOT create  a new function every time you make a ‘loopy’ table

local function onTimer( self, event ) print("i k =", self.i, self.k) self.k = self.k + 1 media.playEventSound( beep ) if( self.k \> 4 ) then self.i, self.k = self.i + 1, 1 if ( self.i \> bars ) then self.i = 1 bpmStart = bpmStart + incremento dTS = ( 60000 / bpmStart ) end end end local loopy = { i=1, k=1, timer = onTimer }

Next,  I’ve looked at the code and there are issues:

  • dTS, bpmStart, incremento… are all globals.  I’ll change that in a sec.
  • I don’t understand the last line and why you say: self.lordTimer… Is all of this code inside anther function with a ‘self’ context?  If not you’re not understanding the meaning of self. 

Finally, let me modify your code to what I think you intend below.  I’ve also added my own extras:

local function onTimer( self, event ) print("i k =", self.i, self.k) self.k = self.k + 1 media.playEventSound( beep ) if( self.k \> 4 ) then self.i = self.i + 1 self.k = 1 if ( self.i \> bars ) then self.i = 1 self.bpmStart = self.bpmStart + self.incremento self.dTS = ( 60000 / self.bpmStart ) end end timer.performWithDelay( self.dTS, self ) end local function doLoopy( bpmStart, incremento, dTS ) local loopy = { i = 1, k = 1, bpmStart = bmpStart, incremento = incremento, dTS = dTS, timer = onTimer } timer.performWithDelay( dTS, loopy ) return loopy end

doLoopy( 10, 1, 1000 ) doLoopy( 20, 1, 2000 )

Note: I made several re-edits to the above ‘final’ bit of code, but I’m done now.

thanks a lot @roaminggamer!!! you gave me a lot of advices and you are helping me so much =D 

i’m about to fix my code with your last indications, i’ll let you know what happend.

sorry for that self.lordTimer. got caught by the good ol’copy paste error and did not notice it… my fault =P