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

automation program problem

Started by G_agent, 29 September 2012 - 07:22 AM
G_agent #1
Posted 29 September 2012 - 09:22 AM
Hi, I wrote a simple program to automate a reed farm. I am rather new to lua and I'm having problems with syntax, especially when trying to call a function through another function.

From the information on the reference manual, I believe that you can perform such operation with a line similar to the following:

f = turtle.forward()

Afterwards, to call function f

if something then
f()
end

But apparently I have it all wrong, I cannot get the program below to work (despite it being stupidly simple). Any help you guys could provide would be greatly appreciated.


local function dig()
turtle.dig()
end
local function digU()
turtle.digUp()
end
local function digD()
turtle.digDown()
end
local function det()
return turtle.detect()
end
local function detD()
return turtle.detectDown()
end
local function detU()
return turtle.detectUp()
end
local function fwd()
turtle.forward()
end
local function back()
turtle.back()
end
local function up()
turtle.up()
end
local function down()
turtle.down()
end
local function tR()
turtle.turnRight()
end
local function tL()
turtle.turnLeft()
end

local function rowReed()
for n=1,16 do
  if det() then
	dig()
  else
	fwd()
  end
end
end
local function levelReed()
rowReed()

fwd()
tR()
fwd()
fwd()
tR()

rowReed()

fwd()
tL()
fwd()
tL()

rowReed()

fwd()
tR()
fwd()
fwd()
tR()

rowReed()

fwd()
tR()
fwd()
fwd()
fwd()
fwd()
fwd()
fwd()
tR()

end
local function floorReed()
levelReed()
down()
levelReed()

end

function reed(nFloor)
down()
down()
down()

for i=1,nFloor do
  floorReed()
  down()
  down()
  down()
  down()
end

for j=1,nFloor do
  for k=1,5 do
   up()
  end
end

for m=1,16 do
  turtle.select(m)
  turtle.drop()
end
end

I gave up after many failed attempts at correcting it. My main concern is knowing how to call a function through another function, so i can "rename" the ones provided by default.
Luanub #2
Posted 29 September 2012 - 09:44 AM
The main issue I see with the script is the var nFloor is never declared or assigned a value.

Any how functions, you've got them setup correctly and I like the fact that you have them all local. Here is a couple of example functions and how to call them.

local function example() -- plain normal function
--insert code
end

example() -- to call it

local function example1(...) -- function with unlimited number of arguments. stored in the table tArgs
for x=1, #tArgs do --for loop that will print the contents of the table
print(tArgs[x])
end
end

example1("arg1", "arg2", "arg4", "etc..etc..etc..")

local function example2( num ) -- function with 1 argument
if not num then num = 1 end  --in case user did not send a number
for x=1, num do -- move the turtle forward the number of steps sent in the arg
  turtle.forward()
end
end

example2(5) -- tell it to move the turtle 5 steps forward

Always make sure the functions are above the code that is calling them. Lua reads from the top down and it it hasn't read it, it doesn't exist as far as it is concerned.
Pharap #3
Posted 30 September 2012 - 03:31 AM
It all pretty much looks in order. I can see a few flaws in how you are going about doing things
eg

local function rowReed()
for n=1,16 do
  if det() then
	    dig()
  else
	    fwd()
  end
end
end
The turtle isn't going to do the whole row since forward is an else condition and thus if the turtle has to dig, it wont move forward, thus it may end up moving 14 spaces instead of 16 (for example)