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

Help please. Simple tunnel script not working properly

Started by le nub, 23 January 2013 - 06:49 AM
le nub #1
Posted 23 January 2013 - 07:49 AM
http://pastebin.com/2h6CKjuk

I've been trying to learn lua for a couple weeks now in my off time and without much progress. I was hoping one of you more helpful fellas could give me a tip or two on how to fix / improve this script.

right now ive been getting the error "end" expected to close 'function' at line :3:

This is confusing the crap out of me… Thanks in advance.
MudkipTheEpic #2
Posted 23 January 2013 - 07:56 AM
Spoiler

--[[   Go   ]]--
function checkBlocks()
if turtle.detectUp() then
  turtle.digUP()
end
if turtle.dectectDown() then
  return true
end
elseif not turtle.detectDown() then
  turtle.select(1)
  sleep(0.375)
  turtle.placeDown()
end
end
function moveForward()
checkBlocks()
sleep(0.375)
  if not turtle.forward() then
   if turtle.detect() then
   turtle.dig()
   turtle.forward()
   else
   turtle.attack()
   turtle.forward()
end
end
function tunnel()

	  input = read()
	  print("tunneling "..input.." blocks deep...")
	  
for moved = 1, tonumber(input) do
  moveForward()
	end
moved = 0
print("Finished!!")
sleep(3)
end
print("How many spaces? ")
tunnel()
-- i know i suck at this...

There is a couple spelling errors in that, so try this:

Spoiler

--[[   Go   ]]--
function checkBlocks()
if turtle.detectUp() then
  turtle.digUp()
end
if turtle.detectDown() then
  return true
elseif not turtle.detectDown() then
  turtle.select(1)
  sleep(0.375)
  turtle.placeDown()
end
end
function moveForward()
checkBlocks()
sleep(0.375)
  if not turtle.forward() then
   if turtle.detect() then
   turtle.dig()
   turtle.forward()
   else
   turtle.attack()
   turtle.forward()
end
end
end
function tunnel()

	  input = read()
	  if tonumber(input) == nil then
	  error("Must be a number")
	  end
	  print("tunneling "..input.." blocks deep...")
	  
for moved = 1, tonumber(input) do
  moveForward()
	end
checkBlocks()
moved = 0
print("Finished!!")
sleep(3)
end
print("How many spaces? ")
tunnel()
-- i know i suck at this...

Checking right now…
Willibilly19 #3
Posted 23 January 2013 - 08:05 AM
I got it working. Took out the "end" on line 9, you have elseif there, so if you don't end it…just let it move on. Fixed a spelling error on line 4 and 7. also removed the turtle.forward() from line 20. ( the "if not turtle.forward() do" line will actually move the turtle forward, so in your original code, he was moving twice for each space.


--[[   Go   ]]--
function checkBlocks()
if turtle.detectUp() then
turtle.digUp()
end

if turtle.detectDown() then
return true


elseif not turtle.detectDown() then
turtle.select(1)
sleep(0.375)
turtle.placeDown()
end
end

function moveForward()
checkBlocks()
sleep(0.375)
if not turtle.forward() then
turtle.dig()
turtle.forward()
end
end


function tunnel()

	  input = read()
	  print("tunneling "..input.." blocks deep...")

for moved = 1, tonumber(input) do
moveForward()
	end
moved = 0
print("Finished!!")
sleep(3)

end

print("How many spaces? ")
tunnel()
-- i know i suck at this...

le nub #4
Posted 23 January 2013 - 08:10 AM
Spoiler

--[[   Go   ]]--
function checkBlocks()
if turtle.detectUp() then
  turtle.digUP()
end
if turtle.dectectDown() then
  return true
end
elseif not turtle.detectDown() then
  turtle.select(1)
  sleep(0.375)
  turtle.placeDown()
end
end
function moveForward()
checkBlocks()
sleep(0.375)
  if not turtle.forward() then
   if turtle.detect() then
   turtle.dig()
   turtle.forward()
   else
   turtle.attack()
   turtle.forward()
end
end
function tunnel()

	  input = read()
	  print("tunneling "..input.." blocks deep...")
	  
for moved = 1, tonumber(input) do
  moveForward()
	end
moved = 0
print("Finished!!")
sleep(3)
end
print("How many spaces? ")
tunnel()
-- i know i suck at this...

There is a couple spelling errors in that, so try this:

Spoiler

--[[   Go   ]]--
function checkBlocks()
if turtle.detectUp() then
  turtle.digUp()
end
if turtle.detectDown() then
  return true
elseif not turtle.detectDown() then
  turtle.select(1)
  sleep(0.375)
  turtle.placeDown()
end
end
function moveForward()
checkBlocks()
turtle.forward()
sleep(0.375)
  if not turtle.forward() then
   turtle.dig()
   turtle.forward()
  end
end
function tunnel()

	  input = read()
	  print("tunneling "..input.." blocks deep...")
	  
for moved = 1, tonumber(input) do
  moveForward()
	end
moved = 0
print("Finished!!")
sleep(3)
end
print("How many spaces? ")
tunnel()
-- i know i suck at this...

Checking right now…

Thanks for that, it fixed the end error. However the script is supposed to fill in 1 block deep gaps below the turtle and skips every othe gap below.
Also, i read through the changes and i cant find exactly what you changed… :/ mind giving me a clue, i just dont see it…

NM, thx for the clarification :)/>
Edited on 23 January 2013 - 07:11 AM
ChunLing #5
Posted 23 January 2013 - 08:10 AM
Hmm…you want to read up on Lua control structures.
le nub #6
Posted 23 January 2013 - 08:17 AM
I got it working. Took out the "end" on line 9, you have elseif there, so if you don't end it…just let it move on. Fixed a spelling error on line 4 and 7. also removed the turtle.forward() from line 20. ( the "if not turtle.forward() do" line will actually move the turtle forward, so in your original code, he was moving twice for each space.


--[[   Go   ]]--
function checkBlocks()
if turtle.detectUp() then
turtle.digUp()
end

if turtle.detectDown() then
return true


elseif not turtle.detectDown() then
turtle.select(1)
sleep(0.375)
turtle.placeDown()
end
end

function moveForward()
checkBlocks()
sleep(0.375)
if not turtle.forward() then
turtle.dig()
turtle.forward()
end
end


function tunnel()

	  input = read()
	  print("tunneling "..input.." blocks deep...")

for moved = 1, tonumber(input) do
moveForward()
	end
moved = 0
print("Finished!!")
sleep(3)

end

print("How many spaces? ")
tunnel()
-- i know i suck at this...

"also removed the turtle.forward() from line 20. ( the "if not turtle.forward() do" line will actually move the turtle forward, so in your original code, he was moving twice for each space."

AH, i suspected i was calling it twice, that explains why he was skipping those fillers below too. I really appreciate your help dude, and everybody else.
Next time i ask for help ill try to find my own spelling errors… :P/> sorry for that
le nub #7
Posted 23 January 2013 - 08:18 AM
Hmm…you want to read up on Lua control structures.

thanks. im slow sometimes…. :)/>
Willibilly19 #8
Posted 23 January 2013 - 08:24 AM
AH, i suspected i was calling it twice, that explains why he was skipping those fillers below too. I really appreciate your help dude, and everybody else.
Next time i ask for help ill try to find my own spelling errors… :P/> sorry for that

No problem. I just threw the code onto my turtle and checked it for you. Glad I could be of help. btw, it's a decent script for just starting:P
le nub #9
Posted 23 January 2013 - 08:31 AM
AH, i suspected i was calling it twice, that explains why he was skipping those fillers below too. I really appreciate your help dude, and everybody else.
Next time i ask for help ill try to find my own spelling errors… :P/> sorry for that

No problem. I just threw the code onto my turtle and checked it for you. Glad I could be of help. btw, it's a decent script for just starting:P

Decent… oh, thats a relief. Ive only ever written 2 other smaller and simpler scripts. So im quite new to this. Thanks, i think ill take that as a compliment. :)/> Again, thanks for the help. i hope ill get better so i can return the favor sometime.

Just out of curiosity.. How would you have written a program to do the same thing? Sorry if this is a little too much to ask for.. But im really curious to see how someone else might use different methods to achieve this more efficiently.
ChunLing #10
Posted 23 January 2013 - 08:44 AM
I would write it as "gox t20" (or however long I wanted the tunnel). Then again, I have an API that let's me do that.
Willibilly19 #11
Posted 23 January 2013 - 08:46 AM
I have not written a program to do that actually. I have a link to my programs in my signature. I'm still new to Lua as well(only a couple months)….so my work isn't exactly great:P
le nub #12
Posted 23 January 2013 - 08:57 AM
I have not written a program to do that actually. I have a link to my programs in my signature. I'm still new to Lua as well(only a couple months)….so my work isn't exactly great:P

I did look at your page. it has some very nice stuff. I like how your code is very simple to read and cleanly. Maybe you could explain to me the function of tArgs {…} in your quarry? I see that used a lot so i figure its helpful. I get that its setting up a table but after that im not sure what task its performing… Sorry if im killing you with Q's.. a link or pointer to something relevant would be great instead if your not up to explaining the nub stuff.. :)/>
Thanks so much
Willibilly19 #13
Posted 23 January 2013 - 09:04 AM
I think this is how I'd do something like that.


tArgs = {...}
length = tArgs[1]

if #tArgs ~= 1 then
  print("Usage: branch <length>")
end


local function forward()
  while not turtle.forward() do
    turtle.dig()
    turtle.digUp()
    turtle.attack()
  end
end


local function tunnel()
  forward()
  if not turtle.detectDown() then
    turtle.select(1)
    turtle.placeDown()
  end
end


for i = 1,length do 
  tunnel()
end
turtle.digUp()

for i = 1,length do
  turtle.back()
end



I'd probably add some more to it, but that would be the basic code.
Willibilly19 #14
Posted 23 January 2013 - 09:05 AM
I'd be happy to explain. got skype or something? just so we aren't littering the forums here.
le nub #15
Posted 23 January 2013 - 09:37 AM
I think this is how I'd do something like that.


tArgs = {...}
length = tArgs[1]

if #tArgs ~= 1 then
  print("Usage: branch <length>")
end


local function forward()
  while not turtle.forward() do
	turtle.dig()
	turtle.digUp()
	turtle.attack()
  end
end


local function tunnel()
  forward()
  if not turtle.detectDown() then
	turtle.select(1)
	turtle.placeDown()
  end
end


for i = 1,length do
  tunnel()
end
turtle.digUp()

for i = 1,length do
  turtle.back()
end



I'd probably add some more to it, but that would be the basic code.

Thanks for that. I think i get it except for the tArgs once again.. I also sent you my Skype via PM in case you ever want to chat or exchange ideas.. im still pretty nub so i dont know if ill have anything useful but i can try :)/>
ChunLing #16
Posted 23 January 2013 - 05:01 PM
tArgs is just an identifier, could be anything else (that isn't a reserved identifier). The special part is {…}, which is a way of referring to a table containing a variable size argument list passed into the program (the other method mentioned in the linked page, using the arg keyword, only works on functions defined inside the program, it doesn't work for the program itself).

Basically, when you run a program, the shell loads the file as a function with a variable argument list, so that you can pass in command line parameters (such as "go fd 3" using the go program). If you want to use those passed in parameters in your program, then you need to assign the argument table to an identifier so that you can use the normal table access methods to get the values in the table.