23 posts
Posted 15 November 2014 - 02:20 PM
In preparation for writing my next program, I'm trying to find a way of reading from the event queue that immediately reassumes the program if nothing is found. I know this question has been asked repeatedly in the past, but I have yet to find one where a satisfactory answer was given.
I know this can be approximated with timers, but the minimal level they reduce the delay to is still rather significant for a once-an-execution occurence. The fact that rednet.receive() has a custom timeout tells me there is no fundamental technical reason this cannot be done, but I have so far failed to figure out how.
8543 posts
Posted 15 November 2014 - 08:30 PM
The reason this isn't commonly done is that there isn't usually a reason to do it. What does the program you're writing actually do, that it would need such functionality?
7083 posts
Location
Tasmania (AU)
Posted 15 November 2014 - 11:02 PM
If your script relies on events, then why does the script need to do anything when the queue is empty? If it doesn't rely on them, then why is it looking at the event queue at all?
It sounds like you're asking how to bypass ComputerCraft's enforced yielding system without ever pausing your script. In which case you should bear in mind that while any one system is running code,
no other system in the world - be it a computer or turtle - can do anything until that first system yields.
ComputerCraft's timers always take at least one server tick (a twentieth of a second) to fire, even if you specify 0 seconds. rednet.receive() uses a timer event to measure time, as does sleep(), so they inherit that limitation.
Anyway,
os.queueEvent() would be one way to pull it off.
375 posts
Location
USA
Posted 15 November 2014 - 11:19 PM
The simplest way is to queue a dummy event and then pull events and save them until you pull your dummy event. Since it's a queue (FIFO), your dummy event is guaranteed to be the last event on the queue. If you want to do it purely for inspection without disturbing the queue, you could then queue all the events back which you pulled.
23 posts
Posted 15 November 2014 - 11:44 PM
The simplest way is to queue a dummy event and then pull events and save them until you pull your dummy event. Since it's a queue (FIFO), your dummy event is guaranteed to be the last event on the queue. If you want to do it purely for inspection without disturbing the queue, you could then queue all the events back which you pulled.
Yes, that is the method I was leaning towards, I came here mostly to see if there was a way to do it that was less of a bypass.
If your script relies on events, then why does the script need to do anything when the queue is empty? If it doesn't rely on them, then why is it looking at the event queue at all?
Uh… that's a good point. I've been thinking that event-triggered behavior was incompatible with the heavy multi-tasking my program would be doing, but now that I think about it, I begin to suspect I was wrong. Thanks for the perspective, I guess. I suppose I'll get to actually writing it now, I'll be back if I do run into any issues.