9 posts
Posted 18 October 2012 - 09:33 AM
So, I've typed up the following code (automatically digs a stripmine and places torches)
function Forward(x)
local a=0
repeat
if turtle.forward() then
a=a+1
else
print("Obstruction")
wait(0.5)
end
until a==x
end
function Backward(x)
local a=0
repeat
if turtle.backward() then
a=a+1
else
print("Obstruction")
wait(0.5)
end
until a==x
end
function Left(x)
for _=1,x do
turtle.turnLeft()
end
end
function Mine(x)
local a=0
repeat
turtle.dig()
if turtle.forward() then
turtle.digDown()
a=a+1
else
print("Obstruction")
wait(0.5)
end
until a==x
end
local tArgs={ ... }
if #tArgs~=1 then
write("Place torches in top left slot.nUsage: stripmine <branches>n")
return
end
if tonumber(tArgs[1])<1 then
write("Must have one or more branches!n")
return
end
turtle.select(1)
for _=1,tArgs[1] do
Mine(2)
turtle.placeDown()
Left(1)
Mine(6)
Left(2)
Forward(6)
Mine(6)
Backward(6)
Left(1)
end
print("Finished!")
And for some reason, my turtle keeps throwing "turtle:10: Expected number" at me. There's probably something with the turtle API, and the line of code in the API is :
local id = native[s_command]( ... )
So, what's wrong?
1243 posts
Location
Indiana, United States
Posted 18 October 2012 - 09:35 AM
Are you passing x as a number?
9 posts
Posted 18 October 2012 - 09:37 AM
Spotted missing capital letters in digDown and turnLeft. I'll see if that fixes it.
9 posts
Posted 18 October 2012 - 09:46 AM
Are you passing x as a number?
It should be. Not sure how it isn't working.
62 posts
Location
Australia
Posted 18 October 2012 - 01:22 PM
Use this instead.
until a == tonumber(x)
818 posts
Posted 18 October 2012 - 01:23 PM
why tonumber a number?
62 posts
Location
Australia
Posted 18 October 2012 - 01:32 PM
why tonumber a number?
Topic title : Expected number.
Error @ line 10 : until a == x
It must be passing x as a string and not a number.
I had this issue a few days ago.
Edit: It IS passing x as a string. I just made a test file with the following.
args = {...}
if args[1] == true then
print("True")
elseif args[1] == false then
print("False")
elseif args[1] == 1 then
print("One")
elseif tonumber(args[1]) == 2 then
print("Two")
else
print("Invalid")
end
Go on, run it. Everything you type will return invalid unless you type 2.
Edited on 18 October 2012 - 11:39 AM
2088 posts
Location
South Africa
Posted 18 October 2012 - 01:41 PM
Yeah tonumber() should work
9 posts
Posted 20 October 2012 - 06:13 AM
why tonumber a number?
Topic title : Expected number.
Error @ line 10 : until a == x
It must be passing x as a string and not a number.
I had this issue a few days ago.
Edit: It IS passing x as a string. I just made a test file with the following.
args = {...}
if args[1] == true then
print("True")
elseif args[1] == false then
print("False")
elseif args[1] == 1 then
print("One")
elseif tonumber(args[1]) == 2 then
print("Two")
else
print("Invalid")
end
Go on, run it. Everything you type will return invalid unless you type 2.
I've managed to fix it WITHOUT having to use tonumber().
Turns out,
I was using wait() instead of sleep().
function Forward(x)
local a=0
repeat
if turtle.forward() then
a=a+1
else
print("Obstruction")
sleep(0.5)
end
until a==x
end
function Backward(x)
local a=0
repeat
if turtle.back() then
a=a+1
else
print("Obstruction")
sleep(0.5)
end
until a==x
end
function Left(x)
for _=1,x do
turtle.turnLeft()
end
end
function Mine(x)
local a=0
repeat
turtle.dig()
if turtle.forward() then
turtle.digDown()
a=a+1
else
print("Obstruction")
sleep(0.5)
end
until a==x
end
local tArgs={ ... }
if #tArgs~=1 then
write("Place torches in top left slot.nUsage: stripmine <branches>n")
return
end
if tonumber(tArgs[1])<1 then
write("Must have one or more branches!n")
return
end
turtle.select(1)
for _=1,tArgs[1] do
Mine(3)
turtle.placeDown()
Left(1)
Mine(6)
Left(2)
Forward(6)
Mine(6)
Backward(6)
Left(1)
end
print("Finished!")
9 posts
Posted 20 October 2012 - 06:15 AM
By the way, the program itself was called "stripmine", not "turtle".
2005 posts
Posted 20 October 2012 - 08:51 AM
Okay, that needs to be in the "get help faster" section: Tell us the error line in your program rather than telling us to hunt down an error in the existing API. Yes, it is possible that there are errors in the existing API, but if so that's something to report in the Bugs forum.