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

[Programming] Having trouble with reading inputs.

Started by krealle, 05 October 2012 - 04:26 PM
krealle #1
Posted 05 October 2012 - 06:26 PM
Hello, so i am making a program with rednet (first time) and i am having some trouble getting it to read inputs without freezing my loop.

This is the part that sends a message this (should) be working.

rednet.open("left")
while true do
term.clear()
term.setCursorPos(1,1)
write("Input: ")
write("")
input = read()

if input == "a" then
  rednet.send(1,"1")
elseif input == "b" then
  rednet.send(1,"2")
elseif input == "c" then
  rednet.send(1,"3")
end
end

This is the part that receives and the place where i am getting trouble

rednet.open("left")
local function clock()
print("1")
os.sleep(.2)
print("2")
os.sleep(.2)
local evt, id, msg = os.pullEvent("rednet_message")
if msg == "" then
  clock()
elseif msg == "2" then
end
end
while true do
term.clear()
term.setCursorPos(1,1)
print("Waiting")
local evt, id, msg = os.pullEvent("rednet_message")
if msg == "1" then
  clock()
elseif msg == "2" then
  print("LOL NOPE") -- I enjoy making silly things :(/>/>
  os.sleep(.5)
end
end

This is the part giving me a hard time because it enjoys pausing the code until i send a new message to it, and i just can't get it to wait shortly (.2 seconds), and then go on if it gets nothing or go out of the loop and await my go.

local evt, id, msg = os.pullEvent("rednet_message")
if msg == "" then
  clock()
elseif msg == "2" then
end
Doyle3694 #2
Posted 05 October 2012 - 06:30 PM
don't use os.pullEvent("rednet_message")
use rednet.receive(timeout)
krealle #3
Posted 05 October 2012 - 06:40 PM
I tried that but it doesn't read inputs.
Doyle3694 #4
Posted 05 October 2012 - 06:42 PM
oh… go check out os.timeOut then, I think it's called that atleast. not really sure how to use it, but I'm sure you'll find it on the wiki.
krealle #5
Posted 05 October 2012 - 06:44 PM
Okay, i will check it out, Thanks. :(/>/>
cant_delete_account #6
Posted 05 October 2012 - 06:51 PM
don't use os.pullEvent("rednet_message")
use rednet.receive(timeout)
rednet.receive is just basically a wrapper for os.pullEvent("rednet_message"), which in some cases os.pullEvent can work better. They both do the same thing.
Doyle3694 #7
Posted 05 October 2012 - 06:52 PM
yeah I know. but rednet.receive has built in timeout, anf that's what he asked for.
krealle #8
Posted 05 October 2012 - 06:58 PM
Actually what I want is whenever it reaches the end of clock() I want it to read for a rednet signal for .2 seconds and if it gets nothing continue to run clock() but if it gets eg. 2 it will stop the function.
Doyle3694 #9
Posted 05 October 2012 - 07:00 PM
then what do you mean with not reading inputs? thought you ment you wanted it to either get a keypressed or a rednet signal.
krealle #10
Posted 05 October 2012 - 07:06 PM
The problem is if i use something like input = read() it will pause the code until it gets an input which would stop the function and needing me to give it an input instead of having it loop by itself.
Fatal_Exception #11
Posted 06 October 2012 - 12:14 AM
This is a bad idea unless you're really careful:

local function clock()
-- stuff
  clock()
end
Instead, you should put the code your want to repeat inside a loop and break out of it when you want to return.