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

Functions and a distinct lack of errors

Started by River_Chief, 28 September 2012 - 06:06 PM
River_Chief #1
Posted 28 September 2012 - 08:06 PM
It's been years since my FORTRAN and COBAL days, but DW's CC tutorials videos have re-lit the flame.

The goal = get comfortable with LUA and develope an ultra simple strip mining program.

This code would start at bedrock, mine up, mine in and come back out. Writing the code out long hand worked great. Incorrectly incorporating DW's functions causes this code to simply not run. No errors, I just come back to the command prompt. Clearly, I've jacked up the way functions interact with the main part of the code. Once I get this relationship down, I'll finish it and move on to the rest of the strip mining code.

Where did I go wrong?

function vertical()
for i = 1, height do
while not turtle.up() do
turtle dig.up()
end
end

function deep()
for i = 1, depth do
while not turtle.forward() do
turtle.dig()
end
end

function aboutFace()
turtle.digDown()
turtle.down()
turtle.turnRight()
turtle.turnRight()
end
end

–Main Program

term.write("How many blocks up?")
height=read()

term.write("How many blocks deep?)
depth=read()

for i = 1, height do
vertical()
end
end

for i = 1, depth do
deep()
end
end

aboutFace()

end




Thanks for any pointers in advance!
MysticT #2
Posted 28 September 2012 - 08:27 PM
You have some missing and some extra ends in the code. Also, you don't have to run the functions in a loop if they already do it.
Here's a fixed version:

local function vertical(height)
  for i = 1, height do
    while not turtle.up() do
	  turtle.digUp()
    end
  end
end

local function deep(depth)
  for i = 1, depth do
    while not turtle.forward() do
	  turtle.dig()
    end
  end
end

local function aboutFace()
  turtle.digDown()
  turtle.down()
  turtle.turnRight()
  turtle.turnRight()
end

print("How many blocks up?")
local height = tonumber(read()) -- read() returns a string, so this converts it to a number to use in the loop

print("How many blocks deep?)
local depth = tonumber(read())

vertical(height)
deep(depth)
aboutFace()
Formating the code helps to catch the missing/extra ends.
River_Chief #3
Posted 28 September 2012 - 11:31 PM
Great stuff, I'm going to go give it a try right now! I really appreciate the help!
River_Chief #4
Posted 29 September 2012 - 12:35 AM
T,

I think I'm headed in the right direction. I looked up the difference between local and global functions and I see how you added the variable in the function itself. I see that tonumber tries to convert its argument to a number. After running it with the corrections I'm getting a <eof> error on 15.

The good news is I think I'm getting the larger concept. The bad news is, the relationships the "Ends" play within a function isn't quite clear (How many and why that many). Or is more accurate to say that the problem lies within the loop and these simple functions are written correctly?

Possible to clarify the situation?
MysticT #5
Posted 29 September 2012 - 01:36 AM
You need to put an end for each block you make. Blocks can be: functions, loops, conditionals or do blocks.
Examples of different blocks:
Spoiler

if <condition> then
  -- code to run if the condition is true
end

if <condition> then
  -- code to run if the condition is true
else
  -- code to run if the condition is false
end

if <condition> then
  -- code to run if the condition is true
elseif <condition2> then
  -- code to run if the second condition is true
else
  -- code to run if both conditions are false
end

while <condition> do
  -- code to run in the loop
end

for <variable> = <start>, <end>[, <increment>] do
  -- code to run in the loop
end

function <name>(<arguments>)
  -- function code
end
Notice that the if blocks only need one end even when using else or elseif.
Nested blocks (blocks inside blocks) need to have their corresponding ends also.
Example from the code above:

local function vertical(height)
  for i = 1, height do
    while not turtle.up() do
	  turtle.digUp()
    end -- end while loop
  end -- end for loop
end -- end function
You could also have something like this:

while true do
  if condition then
    print("Something")
  else
    print("Something else")
  end -- end if
  for i = 1, 10 do
    print("i = ", i)
  end -- end for loop
end -- end while

Hope you understand a little better the ends now.