Jump to content




os.startTimer issue


2 replies to this topic

#1 Linrox

  • Members
  • 7 posts

Posted 29 September 2020 - 10:52 AM

I have been trying to get 2 timers working (as a test, to understand the function) in a 'repeat until' loop and also tried a 'while true' loop.
I can only get the event trigger to work. So i can't tell which timer triggered the event.
I also referenced the example under the Timer(event) page on the CC wiki. if i copy and past there code (shown below) it works great with 2 timers.
But if i try it with my code it fails.
As far as i can see the complete parameter somehow never matches the timer name (timerA or timerB).

I also cannot find much info on how it works or and detailed wiki pages.

Timer(event) page

local timerA = os.startTimer(1)
local timerB = os.startTimer(2)
while true do
  local event, completed = os.pullEvent()
  if event == "timer" then
	if completed == timerA then
	  print("TimerA has finished, restarting it now")
	  timerA = os.startTimer(1)
	elseif completed == timerB then
	  print("TimerB has finished, restarting it now")
	  timerB = os.startTimer(2)
	else
	  print("A timer finished, but it wasn't one of the ones I care about")
	end
  end
end


My Test Code
function TimeA()
local timerA=os.startTimer(3)
end
function TimeB()
local timerB=os.startTimer(5)
end
TimeA()
TimeB()
while true do
local event,complete=os.pullEvent()
if event == "timer" then
   print("Timer Triggered")
   if complete == timerA then
   print("time A triggered")
   TimeA()
   end
   if complete == timerB then
   print("time B triggered")
   TimeB()
   end
end
if event=="char" and complete=="x" then break end
end


#2 Luca_S

  • Members
  • 407 posts
  • LocationGermany

Posted 29 September 2020 - 10:54 AM

The timerA and timerB variables are local to the functions TimeA and TimeB respectively, so in the event loop timerA and timerB are nil and thus can never equal the complete variable. remove the local in line 2 and 5 and add

local timerA
local timerB

at the top of your program

#3 Linrox

  • Members
  • 7 posts

Posted 29 September 2020 - 11:01 AM

Thanks Luca. I though i had already tried that, guess not. It works now. thanks again.


Side note for those who look after CC wiki, it needs more documentation.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users