Jump to content




Attempt to call nil.... executing a function in the same table


  • You cannot reply to this topic
4 replies to this topic

#1 Goof

  • Members
  • 751 posts

Posted 17 February 2014 - 08:00 AM

Hello.

I have no idea if this've been asked before, but i am having trouble executing a function from a function inside the same table...


what is happening:
Spoiler



my table code
Note that im having different functions all around my full code etc.. ( like a button function )


Spoiler

Do i really have to seperate these functions into stand-alone functions instead of having them in one table?


Thanks in Advance


Edit.... 555'th post xD

Edited by Mikk809h, 17 February 2014 - 08:15 AM.


#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 17 February 2014 - 08:36 AM

local menu
menu = { -- ...
So Lua will know what menu is when you declare the function that indexes it.

Edited by LBPHacker, 17 February 2014 - 08:37 AM.


#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 February 2014 - 08:37 AM

the problem is that when the table is defined those variables do not exist within its scope, so it will search for them in the global scope, one way (other than LBPHacker's method) to circumvent this is by doing the following.

local menu = {}
menu.draw = function()
  --# do note that draw cannot call onClick, you have to use some forward declarations to do that
end
menu.onClick = function()
  --# onClick can invoke draw
end

Edited by theoriginalbit, 17 February 2014 - 08:37 AM.


#4 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 17 February 2014 - 08:45 AM

View Posttheoriginalbit, on 17 February 2014 - 08:37 AM, said:

draw cannot call onClick, you have to use some forward declarations to do that
That's not entirely true. If draw calls onClick by calling the function held by menu.onClick, that's fine. That only requires menu to be declared earlier. Although, when you have two local functions, you really have to forward declare one of them:
local a
local b = function(invoke) if invoke then return a(false) end end
a = function(invoke) if invoke then return b(false) end end


#5 Goof

  • Members
  • 751 posts

Posted 17 February 2014 - 09:33 AM

Ohh Well that explained a lot why it didn't work... xD


Thanks for your help





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users