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

Turtle not adding or subtracting from distance

Started by jakemg, 05 April 2013 - 09:04 AM
jakemg #1
Posted 05 April 2013 - 11:04 AM
Okay so im using distance = 0 at the top of my script
then after each function i either have distance = distance + 2 (because my dig function goes 2 layers forward)
then my home function is distance = distance - 1 but when i run the code it runs the dig function turns around then it just goes forward once????



local args = {...}
local times = tonumber(args[1]) -- this is how many times we want
for i=1, times do
distance = 0

function empty()
	    if turtle.getItemCount(16) > 15 then
			    turtle.digUp()
			    turtle.placeUp(1)
			    for i=1,16 do
			    turtle.dropUp(i)
			    turtle.select(1)
			    end -- ending the for
			    turtle.digUp()
	    end -- ending the if
end
function dig()
	    turtle.select(1)
	    turtle.dig()
	    turtle.forward()
	    turtle.digUp()
	    turtle.up()
	    turtle.turnRight()
	    turtle.dig()
	    turtle.forward()
	    turtle.digDown()
	    turtle.down()
	    turtle.turnLeft()
	    turtle.dig()
	    turtle.forward()
	    turtle.digUp()
	    turtle.up()
	    turtle.turnLeft()
	    turtle.dig()
	    turtle.forward()
	    turtle.turnRight()
	    turtle.digDown()
	    turtle.down()
   distance = distance + 2
end
function turn()
turtle.turnLeft()
turtle.turnLeft()
end
function home()
if distance >= 0 then
turtle.forward()
distance = distance - 1
else print ("im home")
end
end

dig()
empty()
end
turn()
home()

Left #2
Posted 05 April 2013 - 11:31 AM
Change distance = tonumber( 1 - distance )
jakemg #3
Posted 05 April 2013 - 11:43 AM
Where do I put that lol do i replace my distance = 0?
SuicidalSTDz #4
Posted 05 April 2013 - 12:15 PM
If i'm correct, you are defining the distance varaible over and over again in a loop. The for loop at the top. Just define the distance variable after you define the times variable.

EDIT:
Also, define the distance variable as local

local distance = yourNumber
jakemg #5
Posted 05 April 2013 - 12:22 PM
I am using this code and I am getting error trying to perform arithmetic on line 44

local args = {...}
local times = tonumber(args[1]) -- this is how many times we want
local distance = yourNumber
for i=1, times do

function empty()
for i=1, times do
  if turtle.getItemCount(16) > 15 then
   turtle.digUp()
   turtle.placeUp(1)
   for i=1,16 do
    turtle.dropUp(i)
    turtle.select(1)
   end -- ending the for
   turtle.digUp()
  end -- ending the if
end -- ending your for
end

function dig()
	    turtle.select(1)
	    turtle.dig()
	    turtle.forward()
	    turtle.digUp()
	    turtle.up()
	    turtle.turnRight()
	    turtle.dig()
	    turtle.forward()
	    turtle.digDown()
	    turtle.down()
	    turtle.turnLeft()
	    turtle.dig()
	    turtle.forward()
	    turtle.digUp()
	    turtle.up()
	    turtle.turnLeft()
	    turtle.dig()
	    turtle.forward()
	    turtle.turnRight()
	    turtle.digDown()
	    turtle.down()
   distance = tonumber( 1 + distance )
end
function turn()
turtle.turnLeft()
turtle.turnLeft()
end
function home()
if distance >= 0 then
turtle.forward()
distance = tonumber( 1 - distance )
else print ("im home")
end
end

dig()
empty()
end
turn()
home()

Telokis #6
Posted 05 April 2013 - 12:27 PM

local distance = yourNumber

You have to replace "yourNumber" by whatever number you want ! (Logically, it should be 0)
jakemg #7
Posted 05 April 2013 - 12:30 PM
still nothing i tried that i dont get error this time though my turtle just turns around then goes forward once….
Telokis #8
Posted 05 April 2013 - 12:34 PM
Could you write
print(distance)
Just where you expect your turtle to move ? It could be useful to debug your code, I think.
SuicidalSTDz #9
Posted 05 April 2013 - 01:02 PM
Could you write
print(distance)
Just where you expect your turtle to move ? It could be useful to debug your code, I think.
Great tip! I always print what my programs are doing when I debug.

Main Post: If I were in the mood to get on a singleplayer world then I would test this for you/fix it correctly. However, I have a mild headache and cannot bare to code. So there are two things that can happen here. Another member may be able to solve your problem, or by using the tip posted above you could try to solve the problem by yourself.

I'm out! :ph34r:/>
jakemg #10
Posted 05 April 2013 - 01:15 PM
its printing distance fine every time it goes forward it add 1 but i just cant get it to turn and come home
jakemg #11
Posted 05 April 2013 - 01:21 PM
I think it might be because home is a function so it is just running it once and the home function is out of the loop?
Telokis #12
Posted 05 April 2013 - 01:23 PM
I think it might be because home is a function so it is just running it once and the home function is out of the loop?

You're right. home will only be called once.

You should also change the

distance = tonumber( 1 - distance )

Use

distance =  distance - 1

instead !
jakemg #13
Posted 05 April 2013 - 01:24 PM
SOLVED =D thanks guys for all your support if you
this was my solution =D


while distance > 0 do
home()
Telokis #14
Posted 05 April 2013 - 01:26 PM
SOLVED =D thanks guys for all your support if you this was my solution =D
 while distance > 0 do home() 

Happy you finally solved it !
SuicidalSTDz #15
Posted 05 April 2013 - 01:30 PM
As am I.. Congrats on your program though! ^_^/>
Engineer #16
Posted 05 April 2013 - 01:34 PM
I am using this code and I am getting error trying to perform arithmetic on line 44
- snip -

Try this:
Spoiler

local args = {...}
local times = tonumber(args[1]) -- this is how many times we want
local distance = yourNumber
--for i=1, times do -- This loop does nothing but redefine empty() 'times' amount of times

function empty()
	for i= 1, 16 do
		if turtle.getItemCount(16) > 15 then
			turtle.digUp()
			turtle.placeUp(1)
			for i=1,16 do
				turtle.dropUp(i)
				turtle.select(1)
   			end -- ending the for
			  turtle.digUp()
		  end -- ending the if
end -- ending your for

--end

function dig()
	turtle.select(1)
	turtle.dig()
	turtle.forward()
	turtle.digUp()
	turtle.up()
	turtle.turnRight()
	turtle.dig()
	turtle.forward()
	turtle.digDown()
	turtle.down()
	turtle.turnLeft()
	turtle.dig()
	turtle.forward()
	turtle.digUp()
	turtle.up()
	turtle.turnLeft()
	turtle.dig()
	turtle.forward()
	turtle.turnRight()
	turtle.digDown()
	turtle.down()
	--distance = tonumber( 1 + distance ) -- tonumber is really unneccessary
	distance = distance + 1
end
function turn()
	turtle.turnLeft()
	turtle.turnLeft()
end
--[[function home() -- Revised home function
	if distance >= 0 then
		turtle.forward()
		--distance = tonumber( 1 - distance ) -- again, tonumber is unneccessary
		distance = 1 - distance -- maybe distance - 1?
		else
			print ("im home")
		end
	end

	dig()
	empty()
end]]--

function home()
	for i = 1, times do -- notice the loop
		turtle.forward()
		distance = distance -1
		dig()
		empty()
	end
end

turn()
home()

I revised the code:
SpoilerI removed distance since it had really no purpose

local tArgs = {...}
local times = tArgs[1] and tonumber(tArgs[1]) or 1

turtle.select(1)

for i = 1, times do
	turtle.forward()

	turtle.dig()
	turtle.forward()
	turtle.digUp()
	turtle.up()
	turtle.turnRight()
	turtle.dig()
	turtle.forward()
	turtle.digDown()
	turtle.down()
	turtle.turnLeft()
	turtle.dig()
	turtle.forward()
	turtle.digUp()
	turtle.up()
	turtle.turnLeft()
	turtle.dig()
	turtle.forward()
	turtle.turnRight()
	turtle.digDown()
	turtle.down()

	if turtle.getItemCount(16) > 0 then
		turtle.select(1)
		turtle.placeUp()
		for i = 2, 16 do
			turtle.select(i)
			turtle.dropUp()
		end
		turtle.select(1)
		turtle.digUp()
	end
end
jakemg #17
Posted 05 April 2013 - 01:40 PM
umm if I remove distance how ill it come back?
Engineer #18
Posted 05 April 2013 - 01:43 PM
Turn around and go for times back?