28 posts
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? :)/>/>
286 posts
Location
Bonn Germany
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
1604 posts
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
28 posts
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 :)/>/>