9 posts
Posted 12 November 2012 - 08:13 PM
I have been trying to create a program where I type move (forward variable) (left/right variable). I want a negative number to be left and positive to be right. I don't know if Lua can interpret -2 as negative 2. Also, I get this error report:
move:16: attempt to compare number with string expected, got number
function goForward()
if not turtle.forward() then
repeat turtle.dig()
until
turtle.forward()
end
end
local mOve = {…}
print("Moving "..mOve[1].." spaces forward and "..mOve[2].." spaces sideways.")
for i = 1, mOve[1] do
goForward()
end
if mOve[2] < 0 then
turtle.turnLeft()
else
turtle.turnRight()
end
for i = 1, mOve[2] do
goForward()
end
1688 posts
Location
'MURICA
Posted 12 November 2012 - 08:18 PM
The strings need to be converted to numbers before using them. Put this under "local mOve = {…}"
mOve[1] = tonumber(mOve[1])
mOve[2] = tonumber(mOve[2])
9 posts
Posted 12 November 2012 - 08:51 PM
Thank you. Will it be able to interpret negative numbers?
Nevermind, I told it to multiply the value of mOve[2] by -1 then move. worked fine. Thanks again.
2088 posts
Location
South Africa
Posted 13 November 2012 - 01:21 AM
You can use math.abs(mOve[2]) to get the
absolute value of the number, so you don't have to times it by -1. But it doesn't make a different :P/>/>
And if you wan't - you should add so that the arguments for the programs have to be numbers and there actually have to be more than 1 argument.
Spoiler
local mOve = {...}
if #mOve ~= 2 then
print( "Usage: go <x times forward> <y times sideways>" )
return
elseif type(mOve[1]) == "string" or type(mOve[2]) == "string" then
print( "Please use numbers.nUsage: go <x times forward> <y times sideways>" )
return
end
-- [[ Declaring variables ]] --
nFoward = tonumber(mOve[1])
nSideways = tonumber(mOve[2])
term.clear() term.setCursorPos(1,1)
function goForward()
while not turtle.forward() do
turtle.dig()
end
turtle.forward()
end
print( "Moving "..nForward.." spaces forward and "..nSideways.." spaces sideways.n" )
for i = 1, nForward do
goForward()
end
print( "I just went "..nForward.." spaces forward!n" )
if nSideways < 0 then
turtle.turnLeft()
print( "Turning left!" )
else
turtle.turnRight()
print( "Turning right!" )
end
for i = 1, nSideways do
goForward()
end
print( "I just went "..nSideways.." spaces forward!" )