CometWolf, on 24 April 2014 - 05:58 PM, said:
However chances are the issue is simply caused by another event being fired before your timer. This will cause updateInterval to be overwritten, and it has to wait another 5 seconds. Adding a print where it starts the timer aswell would probably reveal this to you.
It's this. You shouldn't be starting a new timer until the previous one has been acted on.
That is to say, this structure:
while true do
pingTimeout = os[ "startTimer" ]( updatefreq )
local e2 = { os[ "pullEvent" ]( "timer" ) }
if e2[ 1 ] == "timer" then
if e2[ 2 ] == pingTimeout then
.
.
.
end
end
end
Should be changed into this structure:
pingTimeout = os[ "startTimer" ]( updatefreq )
while true do
local e2 = { os[ "pullEvent" ]( "timer" ) }
if e2[ 1 ] == "timer" then
if e2[ 2 ] == pingTimeout then
.
.
pingTimeout = os[ "startTimer" ]( updatefreq )
end
end
end
By the way, I'm not sure why you're using this sort of thing:
os[ "startTimer" ]( updatefreq )
... but if you're thinking it's more efficient, "os.startTimer( updatefreq )" would in fact execute faster.
Edit:
The timer thing may call for more explanation.
Remember that you're running two sets of functions via the parallel API, and both are starting timers. Each co-routine gets its own copy of the event queue. Pulling an event out of one co-routine's copy of the queue does not pull it from the other co-routine's copy. Whenever an event is fired, it goes into BOTH copies of the queue, regardless as to which co-routine triggered it. Since you're setting timers all over the place you should hence be able to see why your code was messing up.
Even if you
weren't using the parallel API, you should always account for the possibility that something entirely external to your code may be firing off new timers.
Another point, I'm not sure what's with the "if e2[ 1 ] == "timer" then" check. You've already filtered the os.pullEvent() call to only return timer events.
Edited by Bomb Bloke, 25 April 2014 - 12:46 AM.