This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
cmdpwnd's profile picture

working with os.startTimer

Started by cmdpwnd, 05 June 2015 - 02:36 PM
cmdpwnd #1
Posted 05 June 2015 - 04:36 PM
Alright so im trying to run a loop for X seconds where X is a func arg. Right now I have the variable init but I can't figure out where to put my os.pullEvent("timer") at because in the loop im returning but outside of the loop it would only run until the loop returned(I need it to be able to return multiple times). If I have the pullEvent just after return I get a 'end was expected to close if' error, I Just don't know where to put it at.

Code:

Original Question code snippet: http://pastebin.com/tYAPTthQ

Actual Function slightly diff from above: http://pastebin.com/8Rd0XCvJ
Edited on 05 June 2015 - 06:45 PM
MKlegoman357 #2
Posted 05 June 2015 - 05:38 PM
You almost got it. The only thing left to do is leave only one os.pullEvent without an event filter, so you could catch every event you need, and check what event it was.
cmdpwnd #3
Posted 05 June 2015 - 07:41 PM
You almost got it. The only thing left to do is leave only one os.pullEvent without an event filter, so you could catch every event you need, and check what event it was.

so like

mytimer = os.startTimer(waitTIme)
timer = {os.pullEvent()}

if timer == "timer" and timer[2] == mytimer then
blah
end

I just tried that out but its doing the same thing, Its like it just hangs waiting for the timer to finish even though the event isn't fired. If I run the same func without the timer it works fine but its just not continuing once it hits the pullEvent().

Heres the real code for reference.

http://pastebin.com/8Rd0XCvJ
Edited on 05 June 2015 - 05:53 PM
biggest yikes #4
Posted 05 June 2015 - 07:56 PM
-snip-
if timer == "timer" and timer[2] == mytimer then
-snip-
I think you'd have to do

if timer[1] == "timer"
not

if timer == "timer"
Edited on 05 June 2015 - 05:57 PM
cmdpwnd #5
Posted 05 June 2015 - 08:44 PM
-snip-
if timer == "timer" and timer[2] == mytimer then
-snip-
I think you'd have to do

if timer[1] == "timer"
not

if timer == "timer"

Plz reference: http://pastebin.com/8Rd0XCvJ
KingofGamesYami #6
Posted 05 June 2015 - 10:06 PM
You have 3 instances of os.pullEvent there;

line 3
line 7
line 14

Try to reduce this to a single os.pullEvent without any parameters. When utilizing multiple events, you cannot use a filter. Instead, do something like this:


while true do
  local event = {os.pullEvent()}
  if event[ 1 ] == "timer" then
    --#stuff
  elseif event[ 1 ] == "modem_message" then
    --#other stuff
  end
end