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

Turn a rednet message into a variable

Started by hitlit127, 01 August 2012 - 10:55 PM
hitlit127 #1
Posted 02 August 2012 - 12:55 AM
I've been trying to setup a communications network between my friend and I's Companies. Because the Modems only have a range of 64 meters, I am faced with a problem. I have made a method that theoretically will transfer the message from one computer to another down a line between the companies. However the only thing that I can't get to work(at the moment) is turing the original message into a variable, such as "x" and have it still be the same message at the other end.

Is this even possible? I've been trying for the majority of the day now, and have been met with no success.

Here is the code for the transfering.(there is a seperate version counting down rather than up for a message going the other direction)

event, id, text = os.pullEvent()
if event == "rednet_message" then
x = rednet_message
j=os.computerID()
i=j
i=i+1
rednet.send(i,x)
end

And here is the code so far for turning it into a variable. (Not complete or working)


x = input()
j=computercraftID()
i=j
while i<5 do
i=i+1
rednet.send(i, x)
end
event, id, text = os.pullEvent()
if event == "rednet_message" then
print(id ..">"..text)
shell.run("IM")

Any help is appreciated, Thanks in advance!
Lyqyd #2
Posted 02 August 2012 - 01:08 AM
The incoming message is stored in the third variable when using the os.pullEvent returns. So, instead of sending 'x' onward for your forwarding, you should be sending whatever os.pullEvent returns in 'text' when you get the rednet message.

I'm not sure what you mean by "turn into a variable", by the way, since whatever message is received is placed into a variable when the call you make to receive the message returns. So, the ID of the sending computer is placed in 'id', the message in 'text', and if you had a third variable, it would contain the euclidean distance to the sending computer.
Cranium #3
Posted 02 August 2012 - 03:03 PM
If you're trying to send a message to multiple computers at once, why nor try rednet.broadcast?
twormtwo #4
Posted 02 August 2012 - 05:36 PM
Try this code (to send):

messageToSend = "" --This is the message to be sent.
toSendID = "" --This is the computer id that is having the message sent to it.
rednet.open("right") --This opens the rednet interface on the right side.  CHANGE TO THE SIDE YOUR REDNET MODEM IS ON!!!
function send(message, id)
messageToSend = message
toSendID = id
if rednet.send(toSendID, messageToSend) then --This makes sure that the message was sent (doesn't mean it was received)
  term.setCursorPos(1,1)
  print("Message Sent Successfully!")
else
  term.setCursorPos(1,1)
  print("Message Sending Failed!")
end
end
send("HELLO THIS IS A MESSAGE!", 1234) --This would send the message HELLO THIS IS A MESSAGE to computer 1234.  Use read() to get the user input. EG:
--send(read(), read())

And this to receive (and resend):

receivedMessage = "" --This is the message received from computer ID.
computerID = 0 --This is the computer ID.
distance = 0 --This is the euclidean distance from the two modem blocks.
function receive()
  rednet.open("right") --This opens the rednet on the right side. CHANGE TO THE SIDE YOUR REDNET MODEM IS ON!!!
  computerID, receivedMessage, distance = rednet.receive() --There is an optional time to stop listening in the ()'s
  rednet.send(receivedMessage, (computerID + 1)) --Change the + to a - to send the other direction
  receive1() --This is a separate function with the exact same code, just so that java doesn't error with java.stackOverflow
end
function receive1()
  rednet.open("right") --This opens the rednet on the right side. CHANGE TO THE SIDE YOUR REDNET MODEM IS ON!!!
  computerID, receivedMessage, distance = rednet.receive() --There is an optional time to stop listening in the ()'s
  rednet.send(receivedMessage, (computerID + 1)) --Change the + to a - to send the other direction
  receive()
end
receive()
If you want the message received as a variable, the string receivedMessage in this code is what you want (if you want it to put it on the screen, add a line that says

print("IM: " .. receivedMessage)
Hope this helps you in your industrial journey! :ph34r:/>/>
EDIT:
You probably could have used a loop instead of two functions, it will take up less space if you want to use that instead:

[CODE]
receivedMessage = "" --This is the message received from computer ID.
computerID = 0 --This is the computer ID.
distance = 0 --This is the euclidean distance from the two modem blocks.
function receive()
  rednet.open("right") --This opens the rednet on the right side. CHANGE TO THE SIDE YOUR REDNET MODEM IS ON!!!
  while true do
    computerID, receivedMessage, distance = rednet.receive() --There is an optional time to stop listening in the ()'s
    rednet.send(receivedMessage, (computerID + 1)) --Change the + to a - to send the other direction
  end --This ends the while loop, to be restarted.  If you have a line that checks a key to exit the loop, type break to exit the loop.
end
receive()
Take note that both of these things will infinitely loop, so if you want it to just receive and then go on, use this code:

receivedMessage = "" --This is the message received from computer ID.
computerID = 0 --This is the computer ID.
distance = 0 --This is the euclidean distance from the two modem blocks.
function receive()
  rednet.open("right") --This opens the rednet on the right side. CHANGE TO THE SIDE YOUR REDNET MODEM IS ON!!!
  computerID, receivedMessage, distance = rednet.receive() --There is an optional time to stop listening in the ()'s
  rednet.send(receivedMessage, (computerID + 1)) --Change the + to a - to send the other direction
end
--This is just called because it is an example.  If you want it to only execute when you want, put this in the execute code with the other things.
receive()
--Continue here