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

Turtle While loop help

Started by pitchblac23, 01 April 2013 - 04:30 PM
pitchblac23 #1
Posted 01 April 2013 - 06:30 PM
hello all ill make this question short it can get pretty long tho so bare with me here :P/>

im working on making a basic house building (10x10) program with while loops and if and elseifs so now my question is : on my program i have my functions and ifs and loops so far setup in this manner


function Fuel()
if turtle.getFuelLevel <= 1 then
  turtle.select(1)
  turtle.refuel(1)
 end
end
//other functions
i = 1
while i <= 10 do
if turtle.getItemCount(2) >= 20 then
  i = 1
  while i <= 10 do
	Fuel()
	Line()
	i = i + 1
  elseif turtle.getItemCount(2) <= 20
		  print("Need 20 in slot 2")
  end
end
end

and it seems after it finishes it stops and wont loop it over again please help me im a lua noob as you probably can tell mine is much bigger then this but i dont have access to the file to copy and paste if needed to i can finish the rest of it this is just the gist of it
InDieTasten #2
Posted 01 April 2013 - 08:00 PM
let me format that for you:
Spoiler

function Fuel()
	if turtle.getFuelLevel <= 1 then
		turtle.select(1)
		turtle.refuel(1)
	end
end
//other functions
i = 1
while i <= 10 do
	if turtle.getItemCount(2) >= 20 then
		i = 1
		while i <= 10 do
			Fuel()
			Line()
			i = i + 1
		elseif turtle.getItemCount(2) <= 20
			print("Need 20 in slot 2")
		end
	end
end
alright. then theres a weird ending. so I'll fix that, too:


Spoiler


function Fuel()
	if turtle.getFuelLevel <= 1 then
		turtle.select(1)
		turtle.refuel(1)
	end
end
//other functions
i = 1
while i <= 10 do
	if turtle.getItemCount(2) >= 20 then
		i = 1
		while i <= 10 do
			Fuel()
			Line()
			i = i + 1
		end
	elseif turtle.getItemCount(2) <= 20
		print("Need 20 in slot 2")
	end
end
alright. now the problem you described. it appears because you are using a loop in a loop. no problem as far as they don't use the same index. in your case i. so so I'll fix that as well:

Spoiler


function Fuel()
	if turtle.getFuelLevel <= 1 then
		turtle.select(1)
		turtle.refuel(1)
	end
end
//other functions
i = 1
while i <= 10 do
	if turtle.getItemCount(2) >= 20 then
		k = 1
		while k <= 10 do
			Fuel()
			Line()
			k = k + 1
		end
		i = i + 1
	elseif turtle.getItemCount(2) <= 20
		print("Need 20 in slot 2")
	end
end
so. thats all i can do for now because i don't have enough information to test your program of functionality, so it's you turn. would be easier to help you if you posted the complete program. because i don't remember the function line(). correct me, but this function needs to be somewhere in your code, doesn't it?
pitchblac23 #3
Posted 02 April 2013 - 10:08 AM
ya my line function is just this


fucntion Line()
	turtle.select(2)  
	turtle.digDown()
	turtle.placeDown()
end

and i will try out every thing you have showed me there and see it its working

PS. Thank you so much i didnt even think about needing a different index XD shows how bad i am but im learning