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

[lua][error] rednet:350: string expected

Started by dsc45, 01 April 2012 - 02:37 PM
dsc45 #1
Posted 01 April 2012 - 04:37 PM
Here is my RC light program.


term.clear()
term.setCursorPos(1,1)
print(Now running Turtle-Light on computer #" .. os.computerID())
rednet.open("right")
rednet.broadcast("TLS")
redstone.setOutput("front", true)
redstone.setOutput("back", true)
redstone.setOutput("left", true)
redstone.setOutput("right", true)
print("Now on.")
state,x,y,z = 1,nil,nil,nil
while true do
id, message, dis = rednet.receive()
if message ~= nil then
  --print("Message received from: " .. id .. "nMessage reads: " .. message .. "nDistance is: " .. dis)
  if message == os.computerID() .. "1" then
   print("Turning On.")
   redstone.setOutput("front", true)
   redstone.setOutput("back", true)
   redstone.setOutput("left", true)
   redstone.setOutput("right", true)
   state = 0
  end
  if message == os.computerID() .. "s" then
   rednet.send(id, state)
  end
  if message  == os.computerID() .. "l" then
   x, y, z = gps.locate
   rednet.send(id, x,y,z)
  end
end

Lets say the target computer id is 105.
If I do:

rednet.send(105, "1050")

That will turn the lights off.
But if I send it:

rednet.send(105, "105s")
the computer running the program will crash with rednet:350: string expected
same if i send it:

rednet.send(105, "105l")
Anybody know whats going on?
Advert #2
Posted 01 April 2012 - 05:05 PM
You need to convert the message to a string before you can send it; you also can't send multiple arguments (you need to put them all into one string):

rednet.send(id, tostring(state)) -- tostring converts stuff to a string; note that tables etc will show up as "Table: ABCDEF", and not the actual contents.

The XYZ loation is a bit more tricky:

rednet.send(id, tostring(string.format("%d;%d;%d", x, y, z))) -- See how string.format works programming in lua; basically you use %.. to specify where to inject stuff, and it'll replace it. %d = digit, so it'll replace it with a number
You'll need to separate it on the receiving computer, though, to get the xyz value.
dsc45 #3
Posted 01 April 2012 - 07:01 PM
Thank you for your help.
saschaepe #4
Posted 07 January 2013 - 07:40 AM
The XYZ loation is a bit more tricky:

rednet.send(id, tostring(string.format("%d;%d;%d", x, y, z))) -- See how string.format works programming in lua; basically you use %.. to specify where to inject stuff, and it'll replace it. %d = digit, so it'll replace it with a number
You'll need to separate it on the receiving computer, though, to get the xyz value.

And how do I do that? :huh:/>
ChunLing #5
Posted 07 January 2013 - 09:41 PM
Yeah…depends on just what you want to do with it. If you need to use them as separate values, then the easiest solution is to use the textutils.un/serialize() functions. Or you can use string functions to parse the string into parts and tonumber those.