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

Turtle Forward X times

Started by Macapple, 12 July 2012 - 12:27 PM
Macapple #1
Posted 12 July 2012 - 02:27 PM
Hi there,players
I've got a problem,i want my turtle to go forward a SPECIFIED (x) number of times…
I tried it like this

--Program name is: Forward ..x..
a = x
while a > 0 do
turtle.forward()
a = a-1
end

It says: Attempt to compare nil with number

How to fix this? :)/>/>
xuma202 #2
Posted 12 July 2012 - 02:41 PM
The argument passed into the program you'll have to use arg[1] you can then name the program however you want but you don't need a ..x… at the end

forward

a = arg[1]
while a > 0 do
turtle.forward()
a = a-1
end
MysticT #3
Posted 12 July 2012 - 02:52 PM
You need to get the arguments first:

local args = { ... }
That will give you an array (wich is actually a table with numeric indices) with all the arguments passed to the program. Now you can access them like:

local times = args[1]
but remember that the arguments are strings, so you have to convert it if you need a number:

local nTimes = tonumber(args[1])
And now you can use it in a loop (I'll use a for loop since it's better for this kind of things):

for i = 1, nTimes do
  turtle.forward()
end
Macapple #4
Posted 12 July 2012 - 03:44 PM
You need to get the arguments first:

local args = { ... }
That will give you an array (wich is actually a table with numeric indices) with all the arguments passed to the program. Now you can access them like:

local times = args[1]
but remember that the arguments are strings, so you have to convert it if you need a number:

local nTimes = tonumber(args[1])
And now you can use it in a loop (I'll use a for loop since it's better for this kind of things):

for i = 1, nTimes do
  turtle.forward()
end

Thank You so much,it worked fine :)/>/>