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

How to separate rednet messages?

Started by Agent Silence, 13 May 2014 - 03:28 PM
Agent Silence #1
Posted 13 May 2014 - 05:28 PM
Say I wanted to send a command to a computer through rednet, like say "Start turtle1 30" and have a computer read "turtle1" and "30" individually, how would I do that?
Lignum #2
Posted 13 May 2014 - 05:33 PM
Use a pattern.
We'll use the pattern "%w+". '%w' stands for all alphanumeric characters while the '+' tells Lua that we want one or more of these characters.
So basically, we want to match all words.

We can use the string.gmatch function to get all occurrences of our pattern.


for word in message:gmatch("%w+") do
	 print(word)
end

EDIT:
This thread was posted today and is pretty much the same thing. The pattern used there is %S+ which matches all non-space characters instead of just letters and numbers. So if that fits your needs more, you should go with that.
Edited on 13 May 2014 - 03:37 PM
gezepi #3
Posted 13 May 2014 - 09:17 PM
Another way to do this is using tables. Say you wanted to send the command "start" and 1 parameter (30) to the turtle whose computer id is 5:

local msg = {}
msg.toID = 5
msg.command = "start"
msg.parameter = 30

rednet.send(msg.toID, msg)

Sending information over rednet with tables makes using it at the receiving end really easy:

local senderID, message = rednet.receive()
shell.run(message.command, tostring(message.parameter))
Edited on 13 May 2014 - 07:18 PM
viniciuslrangel #4
Posted 14 May 2014 - 12:34 AM
You can use textutils

local PC_ID = 29
local txt = {
  "turtle1",
  30
}
rednet.send(PC_ID,textutils.serialize(txt) )
and the reciver

senderID, msg, distance = rednet.recive()
msgInTable = textutils.unserialize(msg)
// this /\ return a table called in first PC