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

[Lua][Rednet][Programming]Reading message to send via Rednet?

Started by EmTeaKay, 20 June 2012 - 10:12 PM
EmTeaKay #1
Posted 21 June 2012 - 12:12 AM
How do I read a message and send it to another computer via rednet?
MysticT #2
Posted 21 June 2012 - 12:14 AM
To receive a message:

local id, msg = rednet.receive()
to send:

rednet.send(theID, "The Message")
The id must be a number, and the message a string.
EmTeaKay #3
Posted 21 June 2012 - 12:15 AM
Thanks, but I mean if I want to type the message out of lua. That, to my knowledge, can only be used in lua.
Zalerinian #4
Posted 21 June 2012 - 12:20 AM
Thanks, but I mean if I want to type the message out of lua. That, to my knowledge, can only be used in lua.

Please explain this better.
MysticT #5
Posted 21 June 2012 - 12:20 AM
You have to make a program to do it, there isn't a default program to send/receive messages.
Try with this one:

local tArgs = { ... }
local id = tonumber(tArgs[1])
local msg = tArgs[2]

if not id or not msg then
  print("Usage: send <id> <message>")
  return
end

for _,s in ipairs(rs.getSides()) do
  rednet.open(s)
end

rednet.send(id, msg)

And to receive:

for _,s in ipairs(rs.getSides()) do
  rednet.open(s)
end

local id, msg = rednet.receive()
print(id, ": ", msg)
EmTeaKay #6
Posted 21 June 2012 - 12:23 AM
Wow! Thanks for that, helped a lot. But this is too complicated for me. But thanks for the help.

while true do
rednet.open("right")
io.read = msg
rednet.broadcast(msg)
end
Will a variation of that work?
MysticT #7
Posted 21 June 2012 - 12:24 AM
Almost, should be:

rednet.open("right")
while true do
  local msg = read()
  rednet.broadcast(msg)
end
EmTeaKay #8
Posted 21 June 2012 - 12:30 AM
Perfect! This works amazingly.

Now, how do I send a command through a rednet message? I.e. a reboot message.
MysticT #9
Posted 21 June 2012 - 12:36 AM
If you want the receiver to execute a command, it has to be waiting for a message, and then execute the command. There's different ways to do it, you can use keywords (like "reboot" to make the computer reboot), or make the computer execute any message it receives as a program (with shell.run).
EmTeaKay #10
Posted 21 June 2012 - 12:37 AM
Would I do this?


if msg == reboot then
os.reboot()
end
tfoote #11
Posted 21 June 2012 - 12:52 AM
yes.
MysticT #12
Posted 21 June 2012 - 01:19 AM
Would I do this?


if msg == reboot then
os.reboot()
end
Yes, but remember to put quotes to the strings:

if msg == "reboot" then
  os.reboot()
end