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

Torch placing program error

Started by drummer_si, 26 July 2012 - 07:05 AM
drummer_si #1
Posted 26 July 2012 - 09:05 AM
Hey Guys. First post, first program.

I quarry underground and it gets dark, so I wrote a turtle program to go around and place torches for me..

I'm a program for a living, but LUA is new to me.. Code below:


-- Torcher goes CLOCKWISE round a pit, so place with left side facing a wall
currentSlot = 1 -- Which slot are we currently using?
sides = {} -- Length of cavern sides
sideLength = 0 -- Initial length of sides
topSide = 0 -- Counter to know how far down we are
torchCount = 0
-- Get in the pit
turtle.forward()
turtle.down()
for t = 1,4 do --Loop through all 4 sides to determine hole size
while (turtle.detect() == false) do
  turtle.forward()
  sideLength = sideLength + 1
end
sides[t] = sideLength
sideLength = 0
turtle.turnRight()
end
-- Here we're back at the starting point and we know the hole side lengths, start placing torches
-- Go down 4 blocks to start
turtle.down()
turtle.down()
turtle.down()
turtle.down()
topSide = 6
while turtle.detectDown() == false do --Continue loop until Bedrock is below
for t=1,4 do --Loops through each cavern side
  for tt=1, sides[t] do -- Loops through each section of cavern wall
   turtle.forward()
   -- if( math.fmod(tt,7) = 0 ) then -- Place a torch
   torchCount = torchCount + 1
   if torchCount == 7 then
	torchCount = 0
	if turtle.getItemCount(currentSlot) == 0 then
	 currentSlot = currentSlot + 1
	 if currentSlot == 10 then currentSlot = 1
	 turtle.select(currentSlot)
	 turtle.placeUp()
	end
   end
  end
  turtle.turnRight()
end
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
turtle.down()
topSide = topSide + 7
end
-- Lets raise the little fella back to the surface
for t = 1,topSide do
turtle.up()
end


However I receive the following error:


bios:206: [string "Torcher2"]:70: 'end' expected (to close 'while' at line 34)

Line 34 appears to be:

if turtle.getItemCount(currentSlot) == 0 then

But I can't see anything obvious that stopping it from working


Any ideas guys?
BigSHinyToys #2
Posted 26 July 2012 - 09:19 AM
you missed an end see how this formatting helps find errors
Spoiler

currentSlot = 1 -- Which slot are we currently using?
sides = {} -- Length of cavern sides
sideLength = 0 -- Initial length of sides
topSide = 0 -- Counter to know how far down we are
torchCount = 0
-- Get in the pit
turtle.forward()
turtle.down()
for t = 1,4 do --Loop through all 4 sides to determine hole size
    while (turtle.detect() == false) do
	    turtle.forward()
	    sideLength = sideLength + 1
    end
    sides[t] = sideLength
    sideLength = 0
    turtle.turnRight()
end
-- Here we're back at the starting point and we know the hole side lengths, start placing torches
-- Go down 4 blocks to start
turtle.down()
turtle.down()
turtle.down()
turtle.down()
topSide = 6
while turtle.detectDown() == false do --Continue loop until Bedrock is below
    for t=1,4 do --Loops through each cavern side
	    for tt=1, sides[t] do -- Loops through each section of cavern wall
		    turtle.forward()
		    -- if( math.fmod(tt,7) = 0 ) then -- Place a torch
		    torchCount = torchCount + 1
		    if torchCount == 7 then
			    torchCount = 0
			    if turtle.getItemCount(currentSlot) == 0 then
				    currentSlot = currentSlot + 1
				    if currentSlot == 10 then currentSlot = 1
					    turtle.select(currentSlot)
					    turtle.placeUp()
				    end
			    end
		    end
		    turtle.turnRight()
	    end
	    turtle.down()
	    turtle.down()
	    turtle.down()
	    turtle.down()
	    turtle.down()
	    turtle.down()
	    turtle.down()
	    topSide = topSide + 7
    end
end -- missed an end here
-- Lets raise the little fella back to the surface
for t = 1,topSide do
turtle.up()
end
drummer_si #3
Posted 26 July 2012 - 09:29 AM
EDIT:

My bad, it seems in LUA even single line IF statements require an END:

I rewrote the line as

if currentSlot == 10 then currentSlot = 1 end
BigSHinyToys #4
Posted 26 July 2012 - 09:30 AM
while I'm no pro I use Notepad++ for all my coding