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 )