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

[Question][Lua]Rednet sending values

Started by exploder, 20 September 2012 - 03:11 PM
exploder #1
Posted 20 September 2012 - 05:11 PM
Hello pros.

I can't seem to figure out how to send values over rednet. I can make my Mining turtle remotely dig, but I want it to report his progress on my main screen inside my house.
But the progress bar on screen stays 0.
And if there is a way, could you tell me how to make text appear on screen and on terminal on same time.



Here is the code for my Turtle.

y = 1
rednet.open("right")
x = os.computerID()
print("Your Turtle ID is "..x.."")
rednet.receive(nil)
turtle.forward()
for i=1,50 do
turtle.digDown()
turtle.down()
y = y+1
rednet.send(1,"..y..")
end
for o=1,50 do
turtle.up()
y = y+1
rednet.send(1,"..y..")
end
turtle.back()


Here is the code for monitor that is collecting the info.

function start()
l = l-1
mon.clear()
rednet.open("back")
rednet.send(3)
while y < 10 do
mon.setCursorPos(1,1)
print("+--------------------------------------------+")
print(":					  :	  Mining		 :")
print(":					  :  Progress:"..y.."		 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print("+--------------------------------------------+")
mon.setCursorPos(1,1)
sleep(1)
y = rednet.receive(1)
mon.clear()
end

while y > 10 do
mon.clear()
print("+--------------------------------------------+")
print(":					  :	  Mining		 :")
print(":					  :  Progress:"..y.."		:")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print(":					  :					 :")
print("+--------------------------------------------+")
mon.setCursorPos(1,1)
mon.clear()
sleep(1)
y = rednet.receive(1)
mon.clear()
end
end
Lyqyd #2
Posted 20 September 2012 - 06:06 PM
Try using rednet.send(1, tostring(y)) in the turtle program. You're literally sending the string ..y.. each time, not the value of y. Also, use this in your receiving program:


local id, message = rednet.receive()
y = tonumber(message)

rednet.receive() returns the ID of the computer that sent the message as well as the message, so you need to accept both values and then act upon them.
MetalMiner #3
Posted 20 September 2012 - 07:52 PM
You can also send Tables with the textutils AIP:

Computer 1:

local t = {"Any string", 5, x = 7}
local str = textutils.serialize(t)	   -- Output: "{[1]="Any string",[2]=5,["x"]=7,}"
rednet.send(id, str)

Computer 2:

local id, str = rednet.receive()
local t = textutils.unserialize(str)
exploder #4
Posted 20 September 2012 - 07:54 PM
Try using rednet.send(1, tostring(y)) in the turtle program. You're literally sending the string ..y.. each time, not the value of y. Also, use this in your receiving program:


local id, message = rednet.receive()
y = tonumber(message)

rednet.receive() returns the ID of the computer that sent the message as well as the message, so you need to accept both values and then act upon them.

This works excellent, thank you.
exploder #5
Posted 20 September 2012 - 09:29 PM
You can also send Tables with the textutils AIP:

Computer 1:

local t = {"Any string", 5, x = 7}
local str = textutils.serialize(t)	   -- Output: "{[1]="Any string",[2]=5,["x"]=7,}"
rednet.send(id, str)

Computer 2:

local id, str = rednet.receive()
local t = textutils.unserialize(str)

Thank you aswell, I will try that in Multiplayer.

Now there is one question left, is there any possible way to run program text on the screen and on the terminal aswell?
MysticT #6
Posted 20 September 2012 - 11:22 PM
Now there is one question left, is there any possible way to run program text on the screen and on the terminal aswell?
There is now: sync api.
It's not hard to do it, but with an api is always easier :)/>/>