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

Running an program while waiting for a rednet message?

Started by Tjakka5, 15 December 2012 - 09:50 PM
Tjakka5 #1
Posted 15 December 2012 - 10:50 PM
Hello all,

Im kinda being a n00b with rednet, so I was just wondering.

Is there a way to run a program that would just run something, while checking for a rednet message?
Kinda like this:


while true do
  if rednet == "Hello" then
    print("Hello!")
  end
  if rednet == "Bye" then
   print("Bye")
  end
  if not rednet then
    print("No message received")
  end
end

Thanks in advantage.
theoriginalbit #2
Posted 15 December 2012 - 10:55 PM
Hello all,

Im kinda being a n00b with rednet, so I was just wondering.

Is there a way to run a program that would just run something, while checking for a rednet message?
Kinda like this:


while true do
  if rednet == "Hello" then
	print("Hello!")
  end
  if rednet == "Bye" then
   print("Bye")
  end
  if not rednet then
	print("No message received")
  end
end

Thanks in advantage.

try something like this

while true do
  event, senderId, message = os.pullEvent()
  if event == "rednet_message" then
    print(message)
  end
end
remiX #3
Posted 16 December 2012 - 12:12 AM
Yeah use parallel api:


function yourFunction()
    -- blah blah code
end

function waitForMessage()
    while true do
	    e, senderID, message = os.pullEvent("rednet_message")
	    print(message)
	    return  -- use this if you want the program to stop when it gets a rednet message
    end
end

-- Main
parallel.waitForAny( yourFunction, waitForMessage ) -- waitForAny means it will stop when any of the functions returns, use waitForAll if you want it to only stop when all functions return
theoriginalbit #4
Posted 16 December 2012 - 12:31 AM
Yeah use parallel api:

can do that too.

but threading isn't the solution to everything. using os events can be just as much, if not more powerful!
Tjakka5 #5
Posted 16 December 2012 - 03:06 AM
Hmmm… That is not exactly what I want, because I explained it wrong.

I kinda want a IRC system, where you can type, and send messages, but the computer will, in the meantime check if theres a new message, and print it.

EDIT: I just read remiX's program critically, aaand… Im still not sure if that if that does what I want, I'm gonna test!
Doyle3694 #6
Posted 16 December 2012 - 03:09 AM
You have 2 options here:

The not so flexible, but so much easiser parallel API

Or recoding a custom read that will accept a rednet message in between.
theoriginalbit #7
Posted 16 December 2012 - 03:11 AM
you could also have a variable that holds what the user has typed, and on key press event add the char pressed. then if the event is rednet add it to the messages table
Doyle3694 #8
Posted 16 December 2012 - 03:34 AM
that is the second option I wrote about.
theoriginalbit #9
Posted 16 December 2012 - 03:37 AM
that is the second option I wrote about.

oh i thought you meant a custom read() lol
Doyle3694 #10
Posted 16 December 2012 - 05:11 AM
yeah and that is exactly the meaning of what you wrote about… Anyways, back to topic!