Jump to content




Running an program while waiting for a rednet message?


  • You cannot reply to this topic
9 replies to this topic

#1 Tjakka5

  • Members
  • 256 posts

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.

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 15 December 2012 - 10:55 PM

View PostTjakka5, on 15 December 2012 - 10:50 PM, said:

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


#3 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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


#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 December 2012 - 12:31 AM

View PostremiX, on 16 December 2012 - 12:12 AM, said:

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!

#5 Tjakka5

  • Members
  • 256 posts

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!

#6 Doyle3694

  • Members
  • 815 posts

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.

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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

#8 Doyle3694

  • Members
  • 815 posts

Posted 16 December 2012 - 03:34 AM

that is the second option I wrote about.

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 December 2012 - 03:37 AM

View PostDoyle3694, on 16 December 2012 - 03:34 AM, said:

that is the second option I wrote about.

oh i thought you meant a custom read() lol

#10 Doyle3694

  • Members
  • 815 posts

Posted 16 December 2012 - 05:11 AM

yeah and that is exactly the meaning of what you wrote about... Anyways, back to topic!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users