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

[Solved] Question About Events Queue.

Started by Unknowntissue, 29 March 2016 - 06:33 PM
Unknowntissue #1
Posted 29 March 2016 - 08:33 PM
I'm working with Immibis's Peripherals RFID readers.(http://www.computerc...ss-peripherals/)

In my understanding, you start the scanning and while the scanning is happening you will get events that pop up until the scanning is done. (please let me know if I'm right about this)

My question is what will happen to the events that come in while the code is running? To make this clear i will try to make a time line.

Event Timeline:

-os.pullEvent()—————–(code for EventOne running)—-

———————-EventOne——EventTwo—————

So EventTwo happens when the code is busy but I don't want it to just go away. I want it to be handled. Is there a way to take care of incomeing request as they come in?

EDIT: changed title so others with same issue and search it easily.
Edited on 29 March 2016 - 09:14 PM
Anavrins #2
Posted 29 March 2016 - 09:21 PM
Don't put a filter on os.pullEvent(filter)
The first argument returned by pullEvent is the event's name, which you can deal with, with an if statement.


while true do
  local name, p1, p2, p3 = os.pullEvent()
  if name == "key" then
    print("You pressed key "..p1)
    #-- Do stuff with key presses
  elseif name == "rfid_detected" then
    print(p3.." scanner detected "..p1.." from "..p2.." blocks away")
    #-- Do stuff about RFID being detected
  elseif name == "rfid_scan_done" then
    print(p1.." scanner done scanning")
    #-- Do stuff about RFID being done scanning
  end
end
Edited on 29 March 2016 - 07:21 PM
Unknowntissue #3
Posted 29 March 2016 - 10:06 PM
I understand that much so far Anavrins.

My question is how do i make sure i don't lose the events that happen while the block running under "rfid detected". The case I' worried about is getting a second rfid while I'm handling the first one. Also the block under "rfid detected" will have to exchange information with other computers so it will be code that runs a bit slowly.(Slow in the computer world).


event,p1,p2,p3 = os.pullEvent()
if event == "key" then
   # -- do stuff
elseif event == "rfid_detected" then
  # -- talk to other computers and wait for response
  #-- possible to get other "rfid_detected" events while this runs
end
Edited on 29 March 2016 - 08:07 PM
Lyqyd #4
Posted 29 March 2016 - 10:12 PM
Use just one event handling loop, and track the state of various communications with other computers.
Anavrins #5
Posted 29 March 2016 - 10:23 PM
I understand that much so far Anavrins.

My question is how do i make sure i don't lose the events that happen while the block running under "rfid detected". The case I' worried about is getting a second rfid while I'm handling the first one. Also the block under "rfid detected" will have to exchange information with other computers so it will be code that runs a bit slowly.(Slow in the computer world).
The same exact thing, though you would use elseif name == "modem_message" or elseif name == "rednet_message"
Edited on 29 March 2016 - 08:23 PM
Unknowntissue #6
Posted 29 March 2016 - 10:31 PM
Lyqyd:
I'm not sure how that would work or if it will fit in my design plan.

Right now what I have is one computer at the door and other in the building. The one in the building holds the passwords. Then when a player i with an RFID card in their inventory walks into the range of the reader it will send the info on the card to the computer in the building. Then the computer in the building will give the computer at the door permission to open if the information is correct. At the moment, I'm trying to make sure it doesn't miss any RFID cards even if a player is holding many. The problem I will need to solve later is how will the computer in the building handle request if they all come at the same time.

Any input or help you guys can offer is very welcome.
Unknowntissue #7
Posted 29 March 2016 - 10:42 PM
I understand that much so far Anavrins.

My question is how do i make sure i don't lose the events that happen while the block running under "rfid detected". The case I' worried about is getting a second rfid while I'm handling the first one. Also the block under "rfid detected" will have to exchange information with other computers so it will be code that runs a bit slowly.(Slow in the computer world).
The same exact thing, though you would use elseif name == "modem_message" or elseif name == "rednet_message"

I'm not sure if my explanation is clear or if I am not seeing your point.

I'm fine with handling different types of events. I'm worried about events that come in while the code is not sniffing for one at that moment when it arrives.
Bomb Bloke #8
Posted 29 March 2016 - 10:59 PM
ComputerCraft has an event queue for each system. When an event occurs, it goes on the end of that queue. When the computer yields (calls os.pullEvent()), the first event gets removed from the front of the queue, and returned to the script attempting to pull it.

If you specify a filter when using os.pullEvent(), events are automatically discarded from the front of the queue until such time as a matching one is found (at which point it's returned). So as long as you're not using a filter, you can be reasonably sure you'll pull all events, eventually. They don't expire if you make them sit in the queue too long, if that's what you're thinking - they just sit there until pulled. (The queue does have a limit of 256 events, but for your purposes that should be plenty.)

Some functions must pull events in order to… function. For example, sleep() works by starting a timer, and then pulling timer events until it gets the one it wants. Calling functions such as sleep() may therefore cause you to "lose" wanted events that were sitting in the queue. It sounds like your particular script should have no need to call these sorts of functions, however.
Edited on 29 March 2016 - 09:01 PM
Unknowntissue #9
Posted 29 March 2016 - 11:11 PM
Ah alright!! That is exactly the answer I needed bloke!

Thanks again for your help everyone!
Lyqyd #10
Posted 30 March 2016 - 02:02 AM
As well, things like rednet.receive will also clear the event queue until they find what they are looking for, which is why I suggested what I did above: When needing to communicate with other computers, you must keep track of the state of the communications that are active, and not attempt to receive messages anywhere other than your main event handling loop.
Unknowntissue #11
Posted 30 March 2016 - 03:09 PM
Lyqyd:
Yea I haven't run into that problem yet but I can definitely see your point. I'm just having a hard time visualizing how I will use other computers to keep track of the communications.

My current structure:
client request to server—> server confirms that it is ready and listening to that client with return message ——>client sends information ——> client returns result

You don't have to write any code but what do you think is the best way to add in other computers to keep track of the communications,
Lyqyd #12
Posted 30 March 2016 - 03:42 PM
You don't use a separate computer for that. The program would have a variable (or table of variables, if it needs to communicate with multiple computers) that tracks what kind of message you are expecting to see next from a given computer. When a rednet message arrives, you look at the relevant variable for the sender of the message, and react to the message appropriately, depending upon what stage of the communication sequence you are at.
Unknowntissue #13
Posted 30 March 2016 - 04:19 PM
Oh alright I see what you mean now. Thanks I will get to work on that.