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

Running two things at the same time.

Started by grand_mind1, 02 February 2013 - 06:09 PM
grand_mind1 #1
Posted 02 February 2013 - 07:09 PM
I have a door that I want to be able to be opened from the main computer and another computer with a rednet modem on it. I already know how to use rednet and all of the other things I will need but I need to know if there is a way to have my main computer waiting for touchscreen and rednet events at the same time. Being able to do this will allow me to open the door with the monitor next to it AND from another computer somewhere else. I wanted to try to do something like

event = os.pullEvent()
if event == "monitor_touch" then
  --door stuff
elseif event == "rednet_message" then
  --more door stuff
end
but for the touch screen functionality I need the parameters of the event and I don't know how to do this.
If someone could tell me a way to do this, that would be great!
Help is appreciated!
Thanks! :D/>
raineth #2
Posted 02 February 2013 - 07:20 PM
You're on the right track.

In Lua, functions can return more than one value, and os.pullEvent() does exactly this.

There are some examples (and a list of what the event parameters are) in the documentation for os.pullEvent().
grand_mind1 #3
Posted 02 February 2013 - 07:31 PM
Oh, ok. Although I still don't understand how I could get the parameters for monitor_touch. For example:

event, side, xPos, yPos = os.pullEvent("monitor_touch")
I need the x and y position of where I touched. How would I do this if I am not directly waiting for a monitor touch event?
ChunLing #4
Posted 02 February 2013 - 07:44 PM
Don't specify an event type, or os.pullEvent() will discard all other event types till it gets the type specified.
grand_mind1 #5
Posted 02 February 2013 - 08:08 PM
So if I put

event, senderId, msg, side, xPos, yPos = os.pullEvent()
and the event is a rednet message then it will ignore the other parameters?
lieudusty #6
Posted 02 February 2013 - 08:16 PM
Well you would do:

event, p1, p2, p3 = os.pullEvent()
grand_mind1 #7
Posted 02 February 2013 - 08:22 PM
I don't think I really understand the os.pullEvent(). If I need either the location of where I touched the monitor or the message that a computer sent to it, what sort of parameters would I put on os.pullEvent()? If I put them in a certain order will the computer know which one is which?
Shnupbups #8
Posted 02 February 2013 - 09:24 PM
As lieudsty said:

event, p1, p2, p3 = os.pullEvent()
If it was rednet_message p1 would be the senderId, p2 would be the msg and p3 would be the distance between the sender and the reciever in blocks, but you can ignore that.
If it was monitor_touch p1 would be the side, p2 would be the x and p3 would be the y.

Simple enough.