[LUA][Error] Attempt to call nil
Delerium 01 May 2013
Title: [LUA][Error] Attempt to call nil
I wrote a little mining turtle programm by myself but can't figure out whats wrong here. I tried various things. Changing the order of the functions, what i think is the problem, because the error message goes to other lines by changing the order of the functions... I even tried to convert it into java and debug it, but java can't find anything at all.
The code itself should be self explaining, but im ready to answear questions! Hope anyone can help me.
Here is the code
I wrote a little mining turtle programm by myself but can't figure out whats wrong here. I tried various things. Changing the order of the functions, what i think is the problem, because the error message goes to other lines by changing the order of the functions... I even tried to convert it into java and debug it, but java can't find anything at all.
The code itself should be self explaining, but im ready to answear questions! Hope anyone can help me.
Here is the code
xDir = 1 yDir = 0 w = 0 x = 0 y = 0 z = 0 n = 1 for i=1, 65 do turtle.digDown() turtle.down() z=z-1 end function dig() while turtle.detect() do turtle.dig() sleep(1) end end function changeDirR() if xDir == 1 then xDir = 0 yDir = -1 end if xDir == -1 then xDir = 0 yDir = 1 end if yDir == 1 then xDir = 1 yDir = 0 end if yDir == -1 then xDir = -1 yDir = 0 end end function changePos() if xDir == 1 then x = x+1 end if xDir == -1 then x = x-1 end if yDir == 1 then y = y+1 end if yDir == -1 then y = y-1 end end function turn() turtle.turnRight() turtle.turnRight() end function goup() for h1=1, -z do turtle.digup() turtle.up() end unload() charge() returnDiggin() end function godown() for h2=1, -z do turtle.digdown() turtle.down() end function gox() for g1=1, x do dig() turtle.forward() end end function goy() for g2=1, y do dig() turtle.forward() end end function gonegx() for g3=1, -x do dig() turtle.forward() end end function gonegy() for g4=1, -y do dig() turtle.forward() end end function goToCorner() for bn=1, w do turtle.forward() end turn() routine() end function routine() while turtle.getItemCount(16) < 1 do w = 0 for p=1, 2 do for u=1, n do while turtle.detect() do turtle.dig() sleep(1) end turtle.forward() changePos() w = w+1 if enoughFuel() == true then for s=1, 12 do turtle.digUp() turtle.up() end for t=1, 12, 1 do turtle.digDown() turtle.down() end else returnToSurface() end end turtle.turnRight() changeDirR() end n=n+1 end returnToSurface() end function returntosurface() if xdir==1 then turtle.turnRight() goy() if x<=0 then turtle.turnLeft() gonegx() else turtle.turnright() gox() turn() end end if xdir==-1 then turtle.turnRight() gonegy() if x<=0 then turtle.turnRight() gonegx() else turtle.turnLeft() gox() turn() end end if ydir==1 then turtle.turnRight() gonegx() if y<=0 then turtle.turnLeft() gonegy() turtle.turnright() else turtle.turnright() goy() turtle.turnleft() end end if ydir==-1 then turtle.turnRight() gox() if y<=0 then turtle.turnRight() gonegy() turtle.turnright() else turtle.turnleft() goy() turtle.turnleft() end end goup() end function enoughFuel() if xDir == 1 or xDir == -1 then if x >= 0 then if turtle.getFuelLevel() > x+y-z+30 then return 1 end end if x < 0 then if turtle.getFuelLevel() > -x+y-z+30 then return 1 end end end if yDir == 1 or yDir == -1 then if y >= 0 then if turtle.getFuelLevel() > x+y-z+30 then return 1 end end if y < 0 then if turtle.getFuelLevel() > x-y-z+30 then return 1 end end end return nil end function unload() for q=1, 16 do local itemCount = turtle.getItemCount(q) if itemCount > 0 then turtle.select(q) turtle.drop() end end turtle.select(1) end function returnDiggin() godown() if yDir ~= 0 then if x >= 0 then gox() if y > 0 then turtle.turnLeft() goy() end if y <= 0 then turtle.turnRight() gonegy() turn() end end if x < 0 then turn() gonegx() if y >= 0 then turtle.turnRight() goy() turn() end if y < 0 then turtle.turnLeft() gonegy() end end end if xDir ~= 0 then if y >= 0 then turtle.turnLeft() goy() if x >= 0 then turtle.turnRight() gox() turn() end if x < 0 then turtle.turnLeft() gonegx() end end if y < 0 then turtle.turnRigt() gonegy() if x > 0 then turtle.turnLeft() gox() end if x < 0 then turtle.turnRight() gonegx() end turn() end end end goToCorner() end function charge() print ("Out of Fuel!") end routine()
GravityScore 01 May 2013
Firstly, if you indent your code, it makes it a lot easier to read and easier to debug. I think you may have forgotten the end after the godown function.
As a general rule of function placement, you always place functions you are calling above where you're calling them from. So say you have a function called hello() on line 9, and you call it on line 1, this will generate an attempt to call nil error (the same one you're getting) because the function is defined below where you're calling it from. If you put the hello() function above line 1, the program will work.
As a general rule of function placement, you always place functions you are calling above where you're calling them from. So say you have a function called hello() on line 9, and you call it on line 1, this will generate an attempt to call nil error (the same one you're getting) because the function is defined below where you're calling it from. If you put the hello() function above line 1, the program will work.
Engineer 09 Jul 2013
I re-indented it for you, because Im assuming you didnt reindent it properly. Actually, Sublime did it for me
http://pastebin.com/u5YK2Zp4
As you can see, youhave one big function with all sort of functions, Im not surprised that it isnt working..
If you want to call a function, you have to define it first, so your routine function should be defined as the last one. And it is not even necessry to put a function for that, I see function as code-shorteners, so only when you need the function in different places, you should create a function. But that is just some preference.
Edit: You by the way have it quite a lot that you call a function that is not defined yet..
It will work because it will load it in the global table, but I think its bad practise.
Second edit: This is an example how you should do it with the example above:
This is the good practise!
http://pastebin.com/u5YK2Zp4
As you can see, youhave one big function with all sort of functions, Im not surprised that it isnt working..
If you want to call a function, you have to define it first, so your routine function should be defined as the last one. And it is not even necessry to put a function for that, I see function as code-shorteners, so only when you need the function in different places, you should create a function. But that is just some preference.
Edit: You by the way have it quite a lot that you call a function that is not defined yet..
function x() z() end function z() --stub end
It will work because it will load it in the global table, but I think its bad practise.
Second edit: This is an example how you should do it with the example above:
function z() -- stub end function x() z() end
This is the good practise!
CosmoConsole 09 Jul 2013
Quote
function returntosurface()
Quote
returnToSurface()
Also, it's a good practice (as others have said) to put the function declarations always before any calls to them.
EDIT: Fixed derping
Edited by CosmoConsole, 09 July 2013 - 06:12 AM.
apemanzilla 09 Jul 2013
Also, earlier up after the first few functions you tried to do turtle.digup(), when it should be turtle.digUp(). Same goes for turtle.digdown, should be turtle.digDown(). These can both generate attempt to call nil errors.
karadeli 17 Jul 2013
GravityScore 17 Jul 2013
karadeli, on 17 July 2013 - 02:09 PM, said:
What line?
My guess is that you're wrapping the scanner peripheral from the right side, but also open the rednet modem on the right side, meaning the scanner is wrapped to a modem object, meaning you get attempt to call nil when trying to do scanner.getAspectsDown(). Do you have 2 peripherals on the same side of the computer? (That's not possible btw ).
Bubba 17 Jul 2013
karadeli, on 17 July 2013 - 02:29 PM, said:
It is not computer it is turtle and ı am a Lua N00b please explain it
You can't wrap a peripheral on a side of the turtle that has a peripheral attached to it. So if the turtle has a modem on the right side, you'll have to turn the turtle to face the peripheral and wrap it from the front.
ZagKalidor 17 Jul 2013
karadeli, on 17 July 2013 - 02:09 PM, said:
half of your code is meant to be inserted into table util.
you open the table in line 6 and close it in line 38.
further on, you call table util.send, but there is no valid table util
you seem to open a function called util but made it to a table
seems like you just forgot to insert the code of function util
also, like said before, 2 peripherals wrapped on the same side is not good
you call scanner where it is supposed to be a modem
Lewisk3 19 Aug 2013
i'm getting Attempt to call nil to here's my code
http://pastebin.com/badmBheB
http://pastebin.com/badmBheB