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

Help my code dont work :(

Started by cheekycharlie101, 17 September 2012 - 04:11 PM
cheekycharlie101 #1
Posted 17 September 2012 - 06:11 PM
ok so i made this simple turtle moving program

function move(distance)
for i = 1, distance do
turtle.forward()
end
end
please help me. i dont no why it wont work. what im trying to do is make it so when you type <programname> 5 it will move the turtle 5 blocks forward
but it doesent work D: please can someone tell me what im doing wrong?
Cranium #2
Posted 17 September 2012 - 06:21 PM
You forgot to turn the string you would get from read() to a number. Do this:

function move(distance)
for i = 1, tonumber(distance) do --tonumber() changes from string to a number
turtle.forward()
end
end
GopherAtl #3
Posted 17 September 2012 - 06:22 PM
you've written a function, which can only be called from lua, not from the command line. Running a file containing just this will only define the function "move(dist)", not actually call it. You can see that it's defined by running it, then running lua, and in lua typing "move(5)" which will cause your turtle to move forward 5 as expected.

Only code in a file that is not inside a function will be executed when you run the program from the command line, and a little extra work is involved in taking command-line parameters as well.


--this grabs all command-line parameters to an array
local params={...}

--pull out the first parameter and make it into a number (command line params are all strings normally)
local distance=tonumber(  params[1] )

--now your code from inside the function
for i = 1, distance do
  turtle.forward()
end

hope this helps!
cheekycharlie101 #4
Posted 17 September 2012 - 09:28 PM
omg thanks guys soo much. i coudlent figure this out thanks again :)/>/> -Cheeky