Glad you found a solution for your problem!
I would change some stuff (well I am a guy which rewrites working code to be more elegant…)
You have this “Var0” “Var1” and then this potentially long “if then elseif then…” …
As “id” is something you can use to index. You could use that to create the vars as an table with id be the index…
BUT … Lua allows you to have every object extended on the fly…
So your “var” could be a pointer inside the “button” as can be the “perform with delay” timer…
Here a bit (not validated) code illustrating what I mean:
-- the "timer" table listener event handler which does
-- all the counting
function stepHelper(self,event)
if self.running then
-- increment the count inside the "value"
self.value[1]=self.value[1]+self.stepping
timer.performWithDelay(100,self)
-- maybe do something like update screen with value
end
-- if not running we do not retrigger the event anymore
end
local value1={0} -- its a table so we can reference it from a plus and a minus button
local value2={0}
-- helper function for less code on setup per button
function setupButton(but,value,steps)
but.value=value -- points to the table with the count
but.stepping=steps -- is the stepping
but.running=false -- indicator if still counting
but.timer=stepHelper -- setting the timer table listener
end
local but\_plus1=ui.newButton()
setupButton(but\_plus1,value1,1)
local but\_minus1=ui.newButton()
setupButton(but\_minus1,value1,-1)
local but\_plus2=ui.newButton()
setupButton(but\_plus2,value2,1)
local but\_minus2=ui.newButton()
setupButton(but\_minus2,value2,-1)
You would then set “but_plus.running” to true and call “but_plus.timer(but_plus, nil)” function yourself on “press” and set the but_plus.running to false on release.
This whole “id” thing for the ui.button is IMHO … well … no good coding. It is kind of a workaround.
The ui.Button uses an object oriented approach itself but asks for function pointers and this “id” to handle the events.
Instead of this it should create it’s own real (custom) events and make the button a real object you can add your own table listeners too. It also could offer a lot more functionality this way.
I wonder why that was done this way (and used as example code)… as the original implementation of the events typically use “event.target” to indicate which object was “touched”… which is much better and more universal.
BTW: If you use multiple buttons with your counting functionality… You could easily extend the “ui.lua” code with your functionality. That “button” stuff inside is not rocket science… [import]uid: 6928 topic_id: 1394 reply_id: 3894[/import]