Jump to content




Turtle Forward X times


  • You cannot reply to this topic
3 replies to this topic

#1 Macapple

  • Members
  • 28 posts

Posted 12 July 2012 - 01: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? :)/>

#2 xuma202

  • Members
  • 288 posts
  • LocationBonn Germany

Posted 12 July 2012 - 01: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


#3 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 12 July 2012 - 01: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


#4 Macapple

  • Members
  • 28 posts

Posted 12 July 2012 - 02:44 PM

View PostMysticT, on 12 July 2012 - 01:52 PM, said:

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 :)/>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users