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

Getting a number from the rednet

Started by firenight5, 28 August 2012 - 10:06 PM
firenight5 #1
Posted 29 August 2012 - 12:06 AM
Hello, I am trying to make a turtle to go a specified amount of blocks forward, and the amount being specified by a computer via a rednet.send() function. In my efforts I have striped the code down to a simple form.
computer:


rednet.open("right")
function mine()
print("I want my turtle to go this many blocks..")
rednet.send( 13 , io.read() )
mine()
end
mine()


turtle:

rednet.open("right")
id,message = rednet.receive()
print(message)
if message<10 then
print("good")
end

As you can see the computer gets an input via the player to the turtle, for testing purposes I have been using 5. The turtle then gets that input and compares it to the number 10, so if the input is less than 10, the turtle will print "good". However when tested I get the following error: "mine:4: attempt to compare __lt on nil and number" I assume that this is because the computer sees the input as text and not a number. So how can I get this to work? Thank you for help in advance, I hope this hasn't already been answered, I did do my best to find a answer before asking.

~firenight
TheHappyBukkit #2
Posted 29 August 2012 - 12:09 AM
Hello, I am trying to make a turtle to go a specified amount of blocks forward, and the amount being specified by a computer via a rednet.send() function. In my efforts I have striped the code down to a simple form.
computer:


rednet.open("right")
function mine()
print("I want my turtle to go this many blocks..")
rednet.send( 13 , io.read() )
mine()
end
mine()


turtle:

rednet.open("right")
id,message = rednet.receive()
print(message)
if message<10 then
print("good")
end

As you can see the computer gets an input via the player to the turtle, for testing purposes I have been using 5. The turtle then gets that input and compares it to the number 10, so if the input is less than 10, the turtle will print "good". However when tested I get the following error: "mine:4: attempt to compare __lt on nil and number" I assume that this is because the computer sees the input as text and not a number. So how can I get this to work? Thank you for help in advance, I hope this hasn't already been answered, I did do my best to find a answer before asking.

~firenight

Possibly try changing if message<10 then to if message < 10 then
Grim Reaper #3
Posted 29 August 2012 - 12:19 AM
io.read() returns a string, as does pretty much every input function. Despite the string returned, rednet still accepts numbers as messages, but the message is sent as a string regardless of its original type.

You're problem resides within your turtle function:
On line 4 of your turtle program the code is such:

if message<10 then
You cannot compare a string to a number. Try casting the message variable to a number.

Here's an example:

if tonumber( message ) < 10 then

I just added that simple fix to your program.
Here it is:

rednet.open("right")
id,message = rednet.receive()


print(message)

if tonumber( message ) < 10 then
  print("good")
end
firenight5 #4
Posted 29 August 2012 - 02:39 AM
Thank you for your help!