For me, os.pullEvent() has proven to being one of the most useful tools available for ComputerCraft, but it can be quite confusing for many at first and I won't deny it for me as well (took me a couple days before I figured out how to use it properly..probably longer).
So what is os.pullEvent()?
os.pullEvent() is a function that's used to detect events of any type, which can be pressing a button on the keyboard or an incoming rednet message. For this brief guide I will just explain the basics of how it works and how to use it because of how easy it is to plug it in for other uses.
Hint:
A quick demonstration
Open a computer in Minecraft and enter the Lua command prompt ('lua' is the command as we should all know by now). Enter this short program:
while true do print(os.pullEvent()) endAfter entering this feel free to press some buttons on your keyboard and you can see that it prints things like 'key30' or 'charj'.
What is it doing?
Basically what os.pullEvent() does is return values depending on certain inputs, or events, that happen, such as a keyboard button press or a redstone input. It returns these values based on this syntax:
event, param1, param2 = os.pullEvent() -- param = parameterWhen os.pullEvent() is called, it waits, or "yields" for an input (depending on what's inside its parameters which in this case is nothing so it accepts all events; will cover this later) and once an input is recieved it applies it in like shown above.
Here's how the function works:
- When called, it waits for an input (depending on parameter arguments)
- Once an input is detected it sets a string to the first variable, which in this case is 'event', as the name of the event that occurred.
- It then sets other parameters to other variables depending on the event type. These parameters and the number used is dependent on the event type.
Examples of usage:
Let's say you were developing a program that included key-pressing to choose options (press a to do this, press b to do that, etc.). How such a thing would be written is actually quite simple:
function clear(test) -- function to reset screen term.clear() term.setCursorPos(1,1) if test == nil then -- easy way to decide whether to display text or not print("Press 1 to say Hi, 2 to say Bye or 3 to exit.") end end clear() while true do event, param1 = os.pullEvent() if event == "char" and param1 == "1" then -- if the event was a character input and the character pressed was 1 print("Hi") -- print "Hi" sleep(2) -- sleep for 2 seconds clear() -- call the clear function elseif event == "char" and param1 == "2" then -- if the event was a character input and the character pressed was 2 print("Bye") -- print "Bye" sleep(2) -- sleep for 2 seconds clear() -- call the clear function elseif event == "char" and param1 == "3" then break -- breaks loop end end -- will repeat until loop is broken with 'break' (3 being pressed) clear(1) -- reset screen but without text
Another good example would be for detecting rednet messages:
side = "back" -- it's always good practice to set a variable to the side being used rednet.open(side) -- opens that side's rednet port function clear() -- screen-clearing function term.clear() term.setCursorPos(1,1) end clear() while true do event, param1, param2 = os.pullEvent("rednet_message") -- with rednet messages, the second variable is the ID of -- the computer from which it was sent (integer) and -- the third is the message itself (string) -- parameter argument "rednet_message" limits the event -- accepted to just "rednet_message" if param2 == "e" then -- if the message received is the string "e", break -- break the loop and exit end print(param1..": "..param2) -- if the loop isn't broke, it will print from which the ID came from and -- the message itself end clear() -- clear the screen before exitingHint:
Something to try:
Take the code of the first example and make it so it doesn't need 'event == "char"' in the boolean (true or false; if) operations.
All event types (from the events help file)
- "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.
FAQ
(Idea for something to put here? Please share!)
How do I use Redstone events?? The Redstone event doesn't return any parameters!
if event == "redstone" and rs.getInput(side) == true then print("blah") end
Understand yet? Feel free to ask any questions and I'll try my best to help. Any suggestions? Feel free to give! I'll also soon add more advanced ways to use this!