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

'attempted to call nil' at existing function

Started by oWave, 10 July 2013 - 05:41 AM
oWave #1
Posted 10 July 2013 - 07:41 AM
Hello,

I'm getting the error '65:attempted to call nil' even if the function in that line exists.
I already renamed the function and tried to move it, but I'm still getting the error.

Line 65 is the line with 'GetMid()'

Here's the part of the code that errors:
Spoiler





local function Update(Status)
if Called == false then
GetMid()
Update1()
Called = true
end
term.setCursorPos(MidX, MidY2) term.clearLine() term.setCursorPos(MidX,MidY2) – Clear the 'Status' line and set the Cursor on the line again
print("Status: " .. Status)
end

local function Update1() – Only called once
term.setCursorPos(MidX,MidY1)
if bm == false then
print "Bonemeal: Disabled"
else
print "Bonemeal: Enabled"
end
end

local function GetMid()
MaxX, MaxY = term.getSize()
MidX = MaxX / 2
MidY = MaxY / 2
MidY1 = MidY - 3
MidY2 = MidY + 3
end



(For some reason it doesn't paste the indents. Look at the pastebin code. The part begins at line 63 and goes to line 88)
And if you need the full code: Pastebin
Engineer #2
Posted 10 July 2013 - 07:56 AM

local function Update1() -- Only called once
  term.setCursorPos(MidX,MidY1)
  if bm == false then
    print "Bonemeal: Disabled"
  else
    print "Bonemeal: Enabled"
  end
end

--- Can be shortened:
local function GetMid()
  local MaxX, MaxY = term.getSize()
  MidX = MaxX / 2
  MidY = MaxY / 2
  MidY1 = MidY - 3
  MidY2 = MidY + 3
end
--- to: (Just a suggestion though)
local function GetMid()
  local w, h = term.getSize()
  return w / 2, h / 2 + 3
end

local function Update(Status)
  local MidX, midY2
  if Called == false then
    MidX, midY2 = GetMid()
    Update1()
    Called = true
  end
  term.setCursorPos(MidX, MidY2) term.clearLine() term.setCursorPos(MidX,MidY2) -- Clear the 'Status' line and set the Cursor on the line again
  print("Status: " .. Status)
end

Define your variables first, a function is a variable, then you can use them.
nolongerexistant #3
Posted 10 July 2013 - 07:58 AM
It's becouse you made all your functions local, if you remove the local from GetMid() it works.

On a side note: It's good habit to name functions without a capital, like getMid()
oWave #4
Posted 10 July 2013 - 08:05 AM
Thank you guys, works now
Engineer #5
Posted 10 July 2013 - 08:43 AM
It's becouse you made all your functions local, if you remove the local from GetMid() it works.

On a side note: It's good habit to name functions without a capital, like getMid()
Local is a better practise than not local. Because then you have it only in your program, and the _G table holds less information. Its a bad practise if you make functions not local if it is not an API!
Lyqyd #6
Posted 10 July 2013 - 10:50 AM
Don't post "fixes" that involve the removal of the "local" keyword unless it is the only possible solution. Like when one desires to expose a function in an API they're writing. Usually, if your suggestion is to remove "local", you should wait for someone smarter than you to post a real answer.
GopherAtl #7
Posted 10 July 2013 - 10:52 AM
engineer is correct, making everything non-local may fix this but it is not the best way to fix it. Better, and not really much harder, is just making sure you declare all functions and variables before you call or access them.

:edit: ninja'd