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

(Question) Rednet receive forever or until keypress

Started by dirtywastegash, 08 May 2012 - 09:14 AM
dirtywastegash #1
Posted 08 May 2012 - 11:14 AM
Trying to get a computer to listen for a rednet signal indefinitely until a key is pressed or input is entered.

the code:

rednet.open(side)
while true do
id, dest = rednet.receive()
if input == "x" then
  break -- will stop the while loop
end
end

Doesn't allow me to break the program…

Also, will using


rs.setBundledOutput(side, color, true)
os.startTimer(1)

cut off the output?
Luanub #2
Posted 08 May 2012 - 11:30 AM
The best way to do this is with events, I would check out the tutorial on os.pullEvent for more details but here is a small example of what you will want to do.


while true do
local event, param1, param2 = os.pullEvent()
if event == "rednet_message" then
--do stuff for the rednet message"
elseif event == "key" then -- will detect any key being pressed and return the key number for example the esc key is 1
if "param1" ~= 1 then -- if anything except esc was pressed
-- do key stuff
end
elseif event == "char" then -- will return which key was pressed
if param1 == "x" then
--do different key stuff
end
end
end
dirtywastegash #3
Posted 08 May 2012 - 11:45 AM
Thank you for the quick reply….

This should help me out no end.