lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, 4 Jan 2010 19:55:08 +0000
Colm Sloan <colmsloan@gmail.com> wrote:

> I'm trying to call a function, let's say it's called:
> MyClass:MyFunction01
> 
> I'm trying to pass the name of the function as text where it can be
> later called. Here's what I've tried:
> local functionName = 'MyClass.MyFunction01'
> _G[ functionName ]()
> This method was taken from here (
> http://stackoverflow.com/questions/1791234/lua-call-function-from-a-string-with-function-name
>  ).

What this does is looks up a key 'MyClass.MyFunction01' in the globals
table.  This would work if you had something like this:

_G = {
  -- other stuff
  ['MyClass.MyFunction01'] = function() end,
}

But MyClass is a table (or something table-like, I assume), so what you
actually have is something like this:

_G = {
  -- other stuff
  MyClass = {
    MyFunction01 = function() end
  }
}

Using that, think about your solution and try again.

> 
>  I get the error:
> "attempt to call field 'CLua_NPC_Nurse.PrintTest' (a nil
> value)<http://stackoverflow.com/questions/1791234/lua-call-function-from-a-string-with-function-name>
> "
> 
> I looked in the _G table and found that it does have the entry for my
> class and I'm sure my class has that function. Could anyone tell me
> what I'm doing wrong and how I can fix it?
> 
> Thanks :)

-Rob

Attachment: signature.asc
Description: PGP signature