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

REDNET HELP!

Started by Agent_Zeus, 17 August 2012 - 12:57 AM
Agent_Zeus #1
Posted 17 August 2012 - 02:57 AM
im trying to send 2 things through rednet like
rednet.send(32, user, message)

and receiving like
sender, user, message = rednet.receive()
print(user.." > "..message)


is this how i would do it?
were the message should be it keep printing "10"
im pretty new to lua and only know simple things

if anyone sends complicated code i would apreciate if you explain some of it so i can remember it for next time

thanks in advance
Luanub #2
Posted 17 August 2012 - 03:06 AM
Rednet sends the following information when it sends a message that you can capture in vars with rednet.receive(). In this order, sends computer ID, the message, and the distance. So you var for message is actually going to receive the distance the message was sent.

You will need to include both the use and message in the message and then separate them when the message is received. I like to use CSV's so I separate the user from the message with a comma then split the string at the comma.
Agent_Zeus #3
Posted 17 August 2012 - 03:11 AM
can you give me some example code ?
Luanub #4
Posted 17 August 2012 - 03:21 AM
Here's what I use to split with comma's. There are other ways to do it, this is just the way I have always done it.


function pattern(text,pattern,start)
return string.sub(text,string.find(text,pattern,start)) end

function split(string)
local sep = {}
local done = false
local count,prevVars,tmp = 0,0,0
string = string..",|"
while not done do
  count = count + 1
  tmp = pattern(string,"[^,]+",count+prevVars)
  if tmp == "|" then done = true return sep end
  prevVars = prevVars + tmp:len()
  table.insert(sep,count,tmp)
end
return sep
end

local id, msg, distance = rednet.receive()
local tTable = split(msg) -- the output of the split will be in a table
local user = tTable[1] -- if i remember right starts at 1 might be 0 though
local message = tTable[2]
Agent_Zeus #5
Posted 17 August 2012 - 03:34 AM
so will this make my print(user.." > "..message) work or will i have to put something else
Luanub #6
Posted 17 August 2012 - 03:38 AM
Yes it should work if you do that. I have not tried as I'm at work. Give it a shot and if you have issues let us know.
Agent_Zeus #7
Posted 17 August 2012 - 03:40 AM
ok thank you
Agent_Zeus #8
Posted 17 August 2012 - 03:48 AM
error
bios:206: [string "test"] :13: '=' expected
Luanub #9
Posted 17 August 2012 - 03:51 AM
Can you post the exact code you used?
Agent_Zeus #10
Posted 17 August 2012 - 04:04 AM
nope im on multiplayer but ive doubled checked and its exact same code as the one you gave me
Luanub #11
Posted 17 August 2012 - 04:12 AM
I'll have to play with it when I get home here in a few. Syntax is all correct and I've used that exact split and pattern functions in other code without that error so it should work. I'm thinking you probably missed a " or something somewhere.

You can try this but I don't think it will make much difference.


local tTable = {}
local user = ""
local message = ""

function pattern(text,pattern,start)
return string.sub(text,string.find(text,pattern,start)) end

function split(string)
local sep = {}
local done = false
local count,prevVars,tmp = 0,0,0
string = string..",|"
while not done do
  count = count + 1
  tmp = pattern(string,"[^,]+",count+prevVars)
  if tmp == "|" then done = true return sep end
  prevVars = prevVars + tmp:len()
  table.insert(sep,count,tmp)
end
return sep
end

local id, msg, distance = rednet.receive()
tTable = split(msg)
user = tTable[1]
message = tTable[2]
print(user.." > "..message)

I've tested both of the codes and they both work. I get no errors. You have to have a typo in the code somewhere.
Edited on 17 August 2012 - 03:16 AM
Agent_Zeus #12
Posted 17 August 2012 - 07:47 AM
the second one is ok but when it receives it only receives the user not the message
Luanub #13
Posted 17 August 2012 - 08:27 AM
How are you sending the message? It should look something like

rednet.send(32, "Luanub,Did this work?")

I was able to send that and have it print "Luanub > Did this work?"

If your using vars then it should look something like


local user = "Luanub" -- or obtain from a read or how ever you choose
print("Enter Message: ")
local message = read()
rednet.send(32, user..","..message)
Agent_Zeus #14
Posted 17 August 2012 - 01:47 PM
it works! thanks so much