Jump to content




Pull event without yelding?


  • You cannot reply to this topic
18 replies to this topic

#1 Nix

  • New Members
  • 5 posts

Posted 26 October 2012 - 01:17 PM

Is it possible to pull an event without yelding the PC (like an os.pullEvent that returns nil if no event is in the queue) or even to read the event queue maually to implement the former?

#2 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 26 October 2012 - 01:23 PM

no its not, it yields till it gets an event, thats the whole point in having a coroutine anyway, what would you need this for?

#3 Ditto8353

  • New Members
  • 138 posts

Posted 26 October 2012 - 01:23 PM

os.pullEvent is based off os.pullEventRaw
os.pullEventRaw consists of a single line of code, unfortunately for you that line is return coroutine.yield

#4 Ditto8353

  • New Members
  • 138 posts

Posted 26 October 2012 - 01:26 PM

However! You can try the following!
local evtTimeout = os.startTimer(0.05) //Single-Tick Timer
local evt = {os.pullEvent()}
if evt[2] == evtTimeout then
   //Event Timeout
end


#5 Nix

  • New Members
  • 5 posts

Posted 26 October 2012 - 01:43 PM

That's a clever way to solve the problem, Ditto. Thanks! ^^

#6 Ditto8353

  • New Members
  • 138 posts

Posted 26 October 2012 - 01:47 PM

View PostNix, on 26 October 2012 - 01:43 PM, said:

That's a clever way to solve the problem, Ditto. Thanks! ^^
D'Awww! :3 Well thank you, but it does still yield, and you may have to change the timing (I've never actually done this).

#7 Nix

  • New Members
  • 5 posts

Posted 26 October 2012 - 01:57 PM

I would have to make the loop sleep, I hope that works too

#8 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 26 October 2012 - 02:11 PM

View PostNix, on 26 October 2012 - 01:57 PM, said:

I would have to make the loop sleep, I hope that works too

Sleep yields too. :D/> (it's basically os.setTimer(t) and os.pullEvent('timer') if I'm not mistaken)

#9 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 26 October 2012 - 02:15 PM

And sleep eats events. Why do you need this anyway? Chances are you're over complicating things.

#10 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 26 October 2012 - 02:18 PM

After reading the OP again. Can't you just use the parallel API to run a coroutine that pulls events and adds them to an internal queue. Then from another coroutine you can just check that internal queue for (all) new events without yielding (and waiting). You'd still want to yield now and then in that coroutine though. :D/>

#11 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 26 October 2012 - 02:29 PM

simply do Orwell's idea with only one coroutine

local tQueue={}
local queuer=coroutine.wrap(function() while true do tQueue[#tQueue+1]={os.pullEvent()} end end)
local function pull()
  return table.remove(tQueue,1)
end

unfortunately you will need to make your code a coroutine too though

#12 Nix

  • New Members
  • 5 posts

Posted 26 October 2012 - 02:31 PM

View PostOrwell, on 26 October 2012 - 02:11 PM, said:

View PostNix, on 26 October 2012 - 01:57 PM, said:

I would have to make the loop sleep, I hope that works too

Sleep yields too. :D/> (it's basically os.setTimer(t) and os.pullEvent('timer') if I'm not mistaken)
I know, I mean: if I didn't yeld it for a while, the script would stop: "too long without yelding"

View PostCloudy, on 26 October 2012 - 02:15 PM, said:

And sleep eats events. Why do you need this anyway? Chances are you're over complicating things.
What do you mean, "Sleep eats events"?

View PostOrwell, on 26 October 2012 - 02:18 PM, said:

After reading the OP again. Can't you just use the parallel API to run a coroutine that pulls events and adds them to an internal queue. Then from another coroutine you can just check that internal queue for (all) new events without yielding (and waiting). You'd still want to yield now and then in that coroutine though. :)/>
Ehrm... I think "my" (Thanks again, Ditto) way is simpler, so I prefer it ^^'

#13 Ditto8353

  • New Members
  • 138 posts

Posted 26 October 2012 - 02:35 PM

View PostOrwell, on 26 October 2012 - 02:11 PM, said:

Sleep yields too. :D/> (it's basically os.setTimer(t) and os.pullEvent('timer') if I'm not mistaken)
Huh... That's good to know (I just checked and you are correct).
It starts a timer and then pulls events until it gets that timer.
Basically it's intercepting events from being passed to the calling program until the timer ends.

#14 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 26 October 2012 - 02:45 PM

Sleep eats events. If you start a five-second sleep, any and all events that the program would have otherwise received during those five seconds will be discarded by the sleep function waiting for its timer event to fire.

#15 Nix

  • New Members
  • 5 posts

Posted 26 October 2012 - 03:13 PM

Ok, thank all of you

#16 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 26 October 2012 - 03:30 PM

View PostLyqyd, on 26 October 2012 - 02:45 PM, said:

Sleep eats events. If you start a five-second sleep, any and all events that the program would have otherwise received during those five seconds will be discarded by the sleep function waiting for its timer event to fire.

if you have a simultaneous coroutine listening it will still be recorded though

#17 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 26 October 2012 - 04:49 PM

View PostKaoS, on 26 October 2012 - 03:30 PM, said:

View PostLyqyd, on 26 October 2012 - 02:45 PM, said:

Sleep eats events. If you start a five-second sleep, any and all events that the program would have otherwise received during those five seconds will be discarded by the sleep function waiting for its timer event to fire.

if you have a simultaneous coroutine listening it will still be recorded though

View PostLyqyd, on 26 October 2012 - 02:45 PM, said:

Sleep eats events. If you start a five-second sleep, any and all events that the program would have otherwise received during those five seconds will be discarded by the sleep function waiting for its timer event to fire.

As you can see, I am well aware of that fact. An easy way to demonstrate this is to use bundled cable rednet to send a large message to a computer, then have the receiving computer sleep for a short time when the message starts being received. The rednet.run() function, running in parallel with the shell, will still see all of the events come in, despite the other program sleeping.

#18 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 26 October 2012 - 05:12 PM

erm, ok... I must have misunderstood. I apologize

#19 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 26 October 2012 - 06:52 PM

View PostKaoS, on 26 October 2012 - 05:12 PM, said:

erm, ok... I must have misunderstood. I apologize

No, you were correct, I was simply stating that it had already been said. I then provided an easy example to see it in action.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users