os.pullEvent(): What is it and how is it useful?
#21
Posted 18 July 2012 - 06:14 AM
#22
Posted 29 August 2012 - 11:51 PM
If I filter an event with a string parameter to either pullEvent or pullEventRaw, will any event at the top of the stack/head of the queue ( However the events are implemented ) be destroyed if it does not match. What I mean is, if I use os.pullEvent("rednet_message"), press a key, then receive a message. Will that key press still be in the queue waiting for either a pullEvent("char"), pullEvent("key") or pullEvent(). OR will it be disposed and gone?
The reason I ask is if I'm waiting for a redstone signal, will rednet messages still be queued up. I know I could use a while loop without a filter. But it'd be good to know how filters handle non-matching events. If anyone can answer this it would be greatly appreciated.
Thanks, and sorry for the grave dig.
- NC
#23
Posted 30 August 2012 - 12:59 AM
NeverCast, on 29 August 2012 - 11:51 PM, said:
If I filter an event with a string parameter to either pullEvent or pullEventRaw, will any event at the top of the stack/head of the queue ( However the events are implemented ) be destroyed if it does not match. What I mean is, if I use os.pullEvent("rednet_message"), press a key, then receive a message. Will that key press still be in the queue waiting for either a pullEvent("char"), pullEvent("key") or pullEvent(). OR will it be disposed and gone?
The reason I ask is if I'm waiting for a redstone signal, will rednet messages still be queued up. I know I could use a while loop without a filter. But it'd be good to know how filters handle non-matching events. If anyone can answer this it would be greatly appreciated.
Thanks, and sorry for the grave dig.
- NC
#24
Posted 20 September 2012 - 05:54 PM
EDIT: ok, so I've found a way to get around it, but what I'm looking for at this point is a way to check two events and ignore any other event. any suggestions?
EDIT2: well, I managed to come up with a less than desired, but fully functional (as far as I can tell) solution to my problem. for those of you who are interested in being able to do the same. here you go.
os.pullEvent = os.pullEventRaw
function termiFunc()
-- your alternative terminate commands
print("bye")
sleep(3)
os.shutdown()
end
function checkEvent()
local e = os.pullEvent()
if (e == "terminate") then
termiFunc()
elseif not (e == "timer") then
checkEvent()
end
end
for a = 0, 20 do
os.startTimer(.5)
checkEvent()
print("next")
end
#25
Posted 20 September 2012 - 10:22 PM
timothyhtime, on 20 September 2012 - 05:54 PM, said:
EDIT: ok, so I've found a way to get around it, but what I'm looking for at this point is a way to check two events and ignore any other event. any suggestions?
EDIT2: well, I managed to come up with a less than desired, but fully functional (as far as I can tell) solution to my problem. for those of you who are interested in being able to do the same. here you go.
os.pullEvent = os.pullEventRaw
function termiFunc()
-- your alternative terminate commands
print("bye")
sleep(3)
os.shutdown()
end
function checkEvent()
local e = os.pullEvent()
if (e == "terminate") then
termiFunc()
elseif not (e == "timer") then
checkEvent()
end
end
for a = 0, 20 do
os.startTimer(.5)
checkEvent()
print("next")
end
A few things though
1: os.pullEvent = os.pullEventRaw protects from termination, no real use in this script.
2: 'While true do' is a better kind of loop.
Helpful code
while true do e = os.pullEvent() if e == "terminate" then termiFunc() end endThat will loop repeatedly, no need for the checkEvent().
#26
Posted 20 September 2012 - 10:26 PM
#27
Posted 28 December 2012 - 09:45 AM
local input = read() if input == "1" then
Altleast I learned it....
#28
Posted 28 December 2012 - 01:34 PM
Onionnion, on 30 April 2012 - 02:15 AM, said:
- "char" when text is typed on the keyboard. Argument is the letter typed.
- "key" when a key is pressed on the keyboard. Argument is the numerical keycode.
- "timer" when a timeout started by os.startTimer() completes. Argument is the value returned by startTimer().
- "alarm" when a time passed to os.setAlarm() is reached. Argument is the value returned by setAlarm().
- "redstone" when the state of any of the redstone inputs change.
- "disk" or "disk_eject" when a disk is entered or removed from an adjacent disk drive. Argument is the side.
- "rednet_message" when a message is received from the rednet API.
You missed 8...
http://www.computerc...ent#Event_Types
#29
Posted 28 December 2012 - 02:03 PM
TheOriginalBIT, on 28 December 2012 - 01:34 PM, said:
Onionnion, on 30 April 2012 - 02:15 AM, said:
- "char" when text is typed on the keyboard. Argument is the letter typed.
- "key" when a key is pressed on the keyboard. Argument is the numerical keycode.
- "timer" when a timeout started by os.startTimer() completes. Argument is the value returned by startTimer().
- "alarm" when a time passed to os.setAlarm() is reached. Argument is the value returned by setAlarm().
- "redstone" when the state of any of the redstone inputs change.
- "disk" or "disk_eject" when a disk is entered or removed from an adjacent disk drive. Argument is the side.
- "rednet_message" when a message is received from the rednet API.
You missed 8...
http://www.computerc...ent#Event_Types
terminate is invisible to most programs as it's handled inside os.pullEvent.
Same for http_success and http_failure which are handled inside http.get.
peripheral and peripheral_detach should probably be included.
mouse_click, mouse_scroll and mouse_drag were added after the post was written.
#30
Posted 03 July 2013 - 12:34 PM
How did you get that title?
#31
Posted 04 July 2013 - 01:10 PM
#32
Posted 17 July 2013 - 11:14 PM
#33
Posted 18 July 2013 - 01:25 AM
RichyRocker423, on 17 July 2013 - 11:14 PM, said:
while true do
-- * puts everything returned by .pullEvent into a table
local eventData = {os.pullEvent()}
-- * eventData[1] will contain the name of the event
if eventData[1] == "monitor_touch" then
-- * do something with the other indexes of eventData
-- * unocomment the block below if you have no idea what to do with eventData
--[[ local side = eventData[2]
local touchX = eventData[3]
local touchY = eventData[4]
-- * now you have side, touchX and touchY
]]
-- * ...
elseif eventData[1] == "redstone" then
-- * ...
end
endSee? It'll check the first parameter returned by .pullEvent, and that contains the name of the event. This:-- * ...means that you have to insert your code there.
#34
Posted 30 July 2013 - 12:03 PM
#35
Posted 11 August 2013 - 02:08 PM
Should I look for answers in the "parallel" API, using alternating yields to manage the coroutines? Or is there a better/cleaner way to go about this?
#36
Posted 11 August 2013 - 03:38 PM
Eunomiac, on 11 August 2013 - 02:08 PM, said:
Eunomiac, on 11 August 2013 - 02:08 PM, said:
#37
Posted 24 November 2013 - 05:18 PM
#38
Posted 07 December 2013 - 07:39 AM
From what I understand they are a true false condition that can trigger other events.
What I dont understand is how to add one in the mission editor. Nothing I can find says "add flag" or anything like that.
Im sure it is a simple procedure but I cant seem to figure it out.
Thanks
#39
Posted 10 March 2015 - 01:43 AM
I set a timer to activate every 2 seconds. Let's say that gets given an ID of 130.
I then start a communication function to communicate with a target computer. That function sets another timeout timer of 4 seconds. Let's say that gets the ID of 131.
The loop for the communication function receives the pullEvent trigger for timer 130, ignores that as it should, then another 2 seconds later gets the pullEvent trigger for timer 131. The function calls os.cancelTimer(131) after the correct ID was found on the timer event, also calls that if the function gets the response message before that timer is triggered.
The communication function works fine, does what is expected every time.
The problem occurs in the main program loop. It does a pullEvent to test for keyboard input and that 2 second timer (ID 130 in this example). It gets the key events but never gets the timer event!
Here is the code with unimportant parts taken out...
local modem = peripheral.wrap("back")
modem.open(555)
local count = 0
term.clear()
local computerInRange = true
local myTimer = os.startTimer(1)
-- Function transmit
--
-- Transmits a message and gets a response.
-- If no response in 4 seconds, returns false.
local function transmit(port, message)
modem.transmit(port, 555, message)
local timeout = os.startTimer(4)
while true do
local e, p1, p2, p3, p4, p5 = os.pullEvent()
if e == "timer" then
if p1 == timeout then
os.cancelTimer(timeout)
return false
end
end
if e == "modem_message" and p2 == 555 then
os.cancelTimer(timeout)
return p4
end
end
end
local function updateDisplay()
-- Updates the status display
end
-- First gets status data then continues:
updateDisplay()
while true do
local event, p1, p2, p3, p4, p5 = os.pullEvent()
if event == "timer" then
if p1 == myTimer then
-- Update display
updateDisplay()
-- Poll for other computer
reply = transmit(123, "Hello?")
if reply == false then
computerInRange = false
elseif reply == "Hi!" then
computerInRange = true
else
term.clear()
print("Unexpected response!")
print("Checking in.")
print("Remote computer: "..reply)
break
end
end
if event == "key" and p1 == 38 then
-- Do key L event stuff
end
if event == "key" and p1 == 24 then
-- Do key O event stuff
end
end
os.cancelTimer(myTimer)
It is run on a portable computer which is why it needs to check if the target computer is in range while also updating the display.
Edited by Galbi3000, 10 March 2015 - 01:54 AM.
#40
Posted 10 March 2015 - 01:58 AM
It's a missing 'end' statement that ComputerCraft scripting missed! No errors given for the missing end!
Edited by Galbi3000, 10 March 2015 - 02:00 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











