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

[LUA][Error] Attempt to call nil

Started by Delerium, 01 May 2013 - 04:05 AM
Delerium #1
Posted 01 May 2013 - 06:05 AM
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

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 #2
Posted 01 May 2013 - 11:43 AM
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.
Noobcoder #3
Posted 09 July 2013 - 06:21 AM
GravityScore.. I tried that and it didn't help.. Can you plz help me?
Engineer #4
Posted 09 July 2013 - 07:04 AM
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..

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! :P/>
CosmoConsole #5
Posted 09 July 2013 - 07:11 AM
function returntosurface()
returnToSurface()
It is case-sensitive. I saw multiple calls to returnToSurface, although it's declared as 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 on 09 July 2013 - 05:12 AM
apemanzilla #6
Posted 09 July 2013 - 12:33 PM
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 #7
Posted 17 July 2013 - 03:09 PM
I have a attemp to call nil error me too

my pastebin code is :

http://pastebin.com/fS1gztKU
GravityScore #8
Posted 17 July 2013 - 03:24 PM
I have a attemp to call nil error me too

my pastebin code is :

http://pastebin.com/fS1gztKU

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 :P/>).
karadeli #9
Posted 17 July 2013 - 03:29 PM
It is not computer it is turtle and ı am a Lua N00b please explain it
Bubba #10
Posted 17 July 2013 - 03:39 PM
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.
karadeli #11
Posted 17 July 2013 - 03:49 PM
So What should I do
karadeli #12
Posted 17 July 2013 - 04:08 PM
Yes and Yup What should I do
karadeli #13
Posted 17 July 2013 - 04:55 PM
Error is in the 45th line
ZagKalidor #14
Posted 17 July 2013 - 05:16 PM
I have a attemp to call nil error me too

my pastebin code is :

http://pastebin.com/fS1gztKU

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 #15
Posted 19 August 2013 - 11:45 PM
i'm getting Attempt to call nil to here's my code
http://pastebin.com/badmBheB
MR_nesquick #16
Posted 20 August 2013 - 04:39 AM
you got function ED() on the inside the if statement on line 9