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

[Question] [Lua] Can you make a loop wait for a signal for just a second?

Started by krealle, 09 October 2012 - 04:58 PM
krealle #1
Posted 09 October 2012 - 06:58 PM
So i have this program that works with rednet and i have it so that whenever i send a signal from pc A to pc B eg. "a" it will read for the signal on pc B and if it is "a" it will start a loop. Now my problem is that in this loop i want it to run the loop and then check for a signal eg. "b". If it doesn't get a signal it will keep running the loop but if it gets "b" it will stop the loop and go back to waiting for "a".

Here are the programs so you know what i am talking about.

The sending program:

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

The receiving program:

rednet.open("left")

local function clock()
 print("1")
 os.sleep(.2)
 print("2")
 os.sleep(.2)
 rednet.receive(.2)
 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")
  os.sleep(.5)
 end
end

The loop i am talking about is the clock() function.
Doyle3694 #2
Posted 09 October 2012 - 07:06 PM
Please, indentate your code before posting it here. it's almost unreadable.
krealle #3
Posted 09 October 2012 - 07:11 PM
Please, indentate your code before posting it here. it's almost unreadable.
I am really sorry, i got it fixed now. Didn't even notice it.
Doyle3694 #4
Posted 09 October 2012 - 07:28 PM
btw, refering to the same function in a function can cause a stack overflow. Nice indentation too, but don't understand what's not working.
krealle #5
Posted 09 October 2012 - 07:36 PM
btw, refering to the same function in a function can cause a stack overflow. Nice indentation too, but don't understand what's not working.
In the clock function i want it to check for a rednet signal eg. "b" for just a second or so. And if it doesn't get a signal it will run the function again causing it to be a loop that can be stopped by giving it a signal with "b", that is where my problem is, I have no clue how to make it check for a signal for just a second (if you make it read for something it will pause the code) and make it do something if it gets nothing.
Lyqyd #6
Posted 09 October 2012 - 07:39 PM
Start a timer, then use os.pullEvent(). If you get the timer event first, continue the loop. If you get the rednet_message first, do the other thing.
krealle #7
Posted 09 October 2012 - 07:43 PM
If you are referring to a RedPower timer that can't happen, i wrote this program to avoid using timers.
Lyqyd #8
Posted 09 October 2012 - 07:54 PM
If you are referring to a RedPower timer that can't happen, i wrote this program to avoid using timers.

No, I'm referring to os.startTimer().
krealle #9
Posted 09 October 2012 - 07:56 PM
If you are referring to a RedPower timer that can't happen, i wrote this program to avoid using timers.

No, I'm referring to os.startTimer().
And how does that work? I am fairly new to lua.
Lyqyd #10
Posted 09 October 2012 - 07:58 PM
If you are referring to a RedPower timer that can't happen, i wrote this program to avoid using timers.

No, I'm referring to os.startTimer().
And how does that work? I am fairly new to lua.

Check out the wiki. It starts a timer that throws a "timer" event when the specified time in seconds has elapsed.
krealle #11
Posted 09 October 2012 - 08:01 PM
If you are referring to a RedPower timer that can't happen, i wrote this program to avoid using timers.

No, I'm referring to os.startTimer().
And how does that work? I am fairly new to lua.

Check out the wiki. It starts a timer that throws a "timer" event when the specified time in seconds has elapsed.
Thanks! I will check it out and hopefully this is what i have been looking for. Have my vote. :P/>/>
BrolofTheViking #12
Posted 09 October 2012 - 09:23 PM
well, you could do that, or you could just do
computer, message = rednet.receive(1).
That will make the program wait for 1 second to receive the rednet message, and if more than one second passes, the values should just equal NIL (I'm not actually sure what it will set them to, as I have never tested that specifically)