Thread Tools Display Modes
11-10-08, 09:10 PM   #1
SpamStringer
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 4
Function names...

I'm having trouble with the following:

Functions = {"one", "two", "test.three"}; -- a table with function names

function test.dosomething()
func = _G[Functions[somevalue]];
pcall(func, somevalue);
end

function one()
end

function two()
end

function test.three()
end

For functions "one" and "two", things are fine, but "test.three" assigns a nil value to func. What am I doing wrong?

Last edited by SpamStringer : 11-10-08 at 10:25 PM.
  Reply With Quote
11-11-08, 12:14 PM   #2
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
It's _G["test"].three not _G["test.three"].

if you replace function test.three() end with _G["test.three"] = function() end then it should work perfectly, although I don't think thats what you were looking for.

Here's a simple solution replace
Code:
 func = _G[Functions[somevalue]]
with

Code:
 func = loadstring("return "..Functions[somevalue])())
or you can use string.gmatch the advantage being theres no wrapper function created:

Code:
Functions = {[1] = "foo.bar"}
foo = {}
function foo.bar()
end
local tbl = _G
for var in string.gmatch(Functions[1], "([^%.%[%]\"\']+)") do
	tbl = tbl[var]
end

print(tbl == foo.bar)
Would output true

Hope this helps.

Last edited by Slakah : 11-13-08 at 09:28 AM.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Function names...


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off