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

[Lua][Error] "A nil value" Mining Programm

Started by IceCrasher, 18 February 2013 - 03:06 AM
IceCrasher #1
Posted 18 February 2013 - 04:06 AM
Title:[Lua][Error] "A nil value" Mining Programm
Hello, i wrote a programm you can find under:
http://pastebin.com/qcr9z45f

or here
Spoiler

shell.run("clear")

function programm()
--[[das program wird im bereich der
angegebenen koordinaten minen,
wobei zu beachten ist, dass "s" immer
grosser als "z" sein muss!!!]]--

local msx = 168
local mzx = 158
local msy = 67
local mzy = 58
local msz = 47
local mzz = 37

turtle.select(16)
turtle.digUp()

rednet.open("right")
local x, y, z = gps.locate(3)
print("gps made")

zx = msx
zy = y+1
zz = z
print(""..zx..","..zy..","..zz)



function mine2d(msx, mzx, msy, mzy, msz, mzz)
minex(msx, mzx)
local chx, chy, chz = gps.locate(3)
if chz ~= mzz or chx ~= mzx then
turtle.turnLeft()
while not turtle.forward() do
  turtle.dig()
end
turtle.turnLeft()
mine2d(msx, mzx,msy, mzy, msz, mzz)
else
  turtle.mineDown()
  turtle.down()
  local tx, ty, tz = gps.locate(3)
  if ty+1 == mzy then
	goto(msx, msy, msz)
	print("I'm done :)/>/>/>/>/>")
  else
	turtle.up()
	goto(msx, ty + 1, msz)
	turtle.digDown()
	turtle.down()
	mine2d(mxx, mzx, msy, mzy, msz, mzz)
  end

end
end

function minex(msx, mzx)
for i = 1, msx - (1 + mzx) do
  turtle.select(15)
  if turtle.getItemCount(15) > 0 then
	while turtle.detectUp() do
	  turtle.digUp()
	end
  turtle.select(16)
  turtle.placeUp()
  for i = 1, 15 do
	turtle.select(i)
	turtle.dropUp()
  end
  turtle.select(16)
  turtle.digUp()
  turtle.select(1)
  while not turtle.forward() do
	turtle.dig()
  end
end
end






	  
function goto(zx, zy, zz)
zdirect = 1
rednet.open("right")
local fx, fy, fz = gps.locate(3)

for i = 1, 4 do
if turtle.forward() then
sx, sy, sz = gps.locate(3)

direct = nil
if fz < sz then
  direct = 0
elseif fz > sz then
  direct = 2
elseif fx > sx then
  direct = 1
elseif fx < sx then
  direct = 3
end
break
else
turtle.turnRight()
end
end

function turn(ndirect)
  while ndirect ~= direct do
	turtle.turnRight()
	if direct ~= 3 then
	  direct = direct + 1
	else
	  direct = 0
	end
  end
end





local fail = 0

function z0()
  if zz > sz then
	turn(0)
	if turtle.forward() then
	  sz = sz + 1
	  fail = 0
	  z0()
	else
	  x1()
	end
  else
	x1()
  end
end

function x1()
  if zx < sx then
	turn(1)
	if turtle.forward() then
	  sx = sx - 1
	  fail = 0
	  x1()
	else
	  z2()
	end
  else
	z2()
  end
end

function z2()
  if zz < sz then
	turn(2)
	if turtle.forward() then
	  sz = sz - 1
	  fail = 0
	  z2()
	else
	  x3()
	end
  else
	x3()
  end
end

function x3()
  if zx > sx then
	turn(3)
	if turtle.forward() then
	  sx = sx + 1
	  fail = 0
	  x3()
	else
	  yu()
	end
  else
	yu()
  end
end

function yu()
  if fail ~= 1 then
  if zy > sy then
	if turtle.up() then
	  sy = sy + 1
	  fail = 0
	  yu()
	else
	  yd()
	end
  else
	yd()
  end
  elseif zy ~= sy then
  else
	turtle.up()
	sy = sy + 1
	z0()
  end  
end

function yd()
  if zy < sy then
	if turtle.down() then
	  sy = sy - 1
	  fail = 0
	  yd()
	else
--	 z0()
	  cheg()
	end
  else
--   z0()
	cheg()
  end
end

function cheg()
  if fail == 1 then
  if zx == sx then
	if zy == sy then
	  if zz == sz then
		print("Ich bin angekommen und schalte ab!")
	  else
		print("fail")
	  end
	else
	  print("fail")  
	end
  else
	print("fail")
  end
  else
	if fail == 0 then
	  if zx == sx then
		if zy == sy then
		  if zz == sz then
			print("Ich bin angekommen und schalte ab!")
		  else
			fail = 1
			z0()
		  end
		else
		  fail = 1
		  z0()
		end
	  else
		fail = 1
		z0()
	  end
	end
  end
end

z0()
turn(zdirect)
end
end

goto(zx, zy, zz)

turtle.digDown()
turtle.down()

mine2d(msx, mzx, msy, mzy, msz, mzz)
end

programm()

I get the following messages:
gps made
168, 68, 47
goto:267: attempt to call nil

so, the gps thing is working and i get the numbers as you can see, but why does this attempt to call nil, i really got no clue :(/>/>
and just for better understanding, in slot 16 should be an enderchest by running this programm.

So i woul'd be very happy if someone could help me, because i don't know what to do and really need this programm. I want a turtle to mine for me whitout having problems because of restarting the world. When i would use the excavate the excavate programm would stop by restarting, so i had to make one by miself…

Thank you
IceCrasher
remiX #2
Posted 18 February 2013 - 06:13 AM
Which line is 267… also use spoilers for long codes please. Or use pastebin

I notice you're calling mine2d within mine2d which isn't good, find a way to remove the recursion.
IceCrasher #3
Posted 18 February 2013 - 08:23 AM
If you look at it, there is a pastebin link in line 4 or so.
And it shouldnt be a problem, the thing with "mine2d", i used it in other programms and it worked perfectli.
So i really just need to know why it replys zo my theres a nil value, because in the top 50 lines or so i defined them. Please help my with this problem, the other things are there and work.
ChunLing #4
Posted 19 February 2013 - 01:26 AM
You've gone and nested all your functions inside of programm(), so none of them get defined until programm is called. This is not the correct way to define API functions.
IceCrasher #5
Posted 19 February 2013 - 05:06 AM
oh, so thats the problem haha, i just did that because i wrote some code which uses functions above functions, so i thought i can solve the problem this way haha. So thank you :)/>