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

Sending Local Arg via Rednet message?

Started by aarondm, 13 February 2013 - 11:38 AM
aarondm #1
Posted 13 February 2013 - 12:38 PM
Hey guys.

I'm trying to get a new sorting system up and running that uses turtles to pull from barrels and place in 1 of 2 ender chests. One goes to my ender pouch, one goes back into the system.

I got all the code working except for one little piece. I cannot get the local arg to be sent via Rednet message. Any ideas?

Code on call Computer

local args = { ... }
amount = tonumber(args)
rednet.send(1, "amount")

Receiving Turtle

rednet.open("right")
while true do
    id, msg = rednet.receive()
    amount = tonumber(msg)
    turtle.suckUp()
    turtle.drop(amount)
    turtle.dropDown()
    end

I'm sure it's just a syntax error on my part but I cannot figure it out.
NDFJay #2
Posted 13 February 2013 - 12:42 PM
remove the quotes on "amount"

rednet.send(1,"amount")

should be

rednet.send(1,amount)


That should work

Also on the computer I would have

amount = tostring(args)

instead of

amount = tonumber(args)
aarondm #3
Posted 13 February 2013 - 12:45 PM
Woah, I can't read your post. so many color tags!
aarondm #4
Posted 13 February 2013 - 03:45 PM
When I run the program I get an error on the turtle.

turtle:18: Expected number

Any idea why this is happening?
Bubba #5
Posted 13 February 2013 - 03:56 PM
You're trying to convert a table to a number. You can't do that. You would need to iterate over the table and convert each value to a number (or actually, to a string due to the fact that you can't send numbers over rednet), even if your table has only one value in it.

If you only need to send one value, you could change the first line like so:

--Original
value = {...}
--New
value = ...
value = tostring(value)
rednet.send(1, value)
aarondm #6
Posted 13 February 2013 - 04:04 PM
That is how I have it set up now and it is giving me that error.

I am using

local args = { ... }
amount = tostring(args)
rednet.open("top")
rednet.send(1,amount)
Bubba #7
Posted 13 February 2013 - 04:13 PM
That is how I have it set up now and it is giving me that error.

I am using

local args = { ... }
amount = tostring(args)
rednet.open("top")
rednet.send(1,amount)

You didn't properly read my post then. What you are doing now is putting the arguments into a table, which are signified by these -> { }
What you want is to take those out, and just leave behind the … so you can capture a single argument that is not inside of a table. Only then will you be able to perform operations such as tostring(arg) on that value.
What you have:

local args = { ... }

What you want:

local arg = ...

If you need to send more than one argument, I would suggest using textutils.serialize(table) like so:

local tArgs = {...}
local stringArgs = textutils.serialize(tArgs)
rednet.send(1,stringArgs)

Essentially what textutils.serialize() does is convert the table into a string, which you can then send over rednet.

On the receiving end you would have this.

local id, message = rednet.receive()
local tArgs = textutils.unserialize(message)
for i=1,#tArgs do
 --Perform operations on values in tArgs
end

More info on textutils is avalailable here: http://www.computercraft.info/wiki/Textutils_(API)
aarondm #8
Posted 13 February 2013 - 04:18 PM
Oooooh. I did not realize the brackets were affecting how it read the value. Thank you.

I got it working now! Thank you very much!!

I also wasn't aware that you had to have the receive written like

id, msg = rednet.receive()

Part of my problem was I was using

msg = rednet.receive()

Any info on why this made a difference?
Bubba #9
Posted 13 February 2013 - 04:20 PM
Oooooh. I did not realize the brackets were affecting how it read the value. Thank you.

I got it working now! Thank you very much!!

I also wasn't aware that you had to have the receive written like

id, msg = rednet.receive()

Part of my problem was I was using

msg = rednet.receive()

Any info on why this made a difference?

Yup. rednet.receive() is actually a function that returns two arguments: the id of the sender, and the message itself. So what you were putting into the msg variable was actually the ID of the sender, not the message.
aarondm #10
Posted 13 February 2013 - 04:24 PM
I've seen people use things in their receive functions like


id, msg, distance - rednet.receive()

What decides how much information the sending computer gives?
Bubba #11
Posted 13 February 2013 - 04:31 PM
I've seen people use things in their receive functions like


id, msg, distance - rednet.receive()

What decides how much information the sending computer gives?

Actually, those values are given to you no matter what: it is whether or not you capture them that makes a difference. In general, I use rednet and events in this format:

local tData = {rednet.receive()}

That puts ALL of the information returned by the function into the tData table, and all I have to do to access the variables I want is:

print("The sender was: "..tData[1].." and the message was: "..tData[2])

Although the distance of the sender from the receiver is captured in the tData table, I have no use of it at this point so I do not retrieve it. If at some point in the future though I decide that I do want to use it, it is there.
aarondm #12
Posted 13 February 2013 - 04:41 PM
Oh, I see. Thank you very much!
Bubba #13
Posted 13 February 2013 - 04:43 PM
Oh, I see. Thank you very much!

No problem :)/>