Thread Tools Display Modes
11-23-05, 04:28 PM   #1
JIM the Inventor
A Cyclonian
 
JIM the Inventor's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 42
How to find a variable name for a function?

Given an actual function (basically a 'pointer'), is there any way to search the global variable list for a name of that function?

I thought I had a solution in the LUA "debug" library, like so:

functionName = debug.getinfo( anyFunction ).name

... however, World of Warcraft doesn't acknowledge the existence of the debug library.

---

If I can answer this question, I can solve the following problem:

I want to create an AddOn that tells you what slash commands are registered, and what function names they run. (I believe this would be useful because many AddOn authors give their /command functions distinct names - names that can be related back to the AddOns themselves.)
  Reply With Quote
11-28-05, 03:36 PM   #2
Rowne
A Fallenroot Satyr
 
Rowne's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2005
Posts: 26
I'm not sure I understand fully but since functions exist as objects, couldn't you just check for their existance? Example ...

Code:
FuncScanner = {
   funcTable = {
      {func = "FuncOne"},
      {func = "FuncTwo"},
      {func = "FuncThree"}
   }
}

function FuncScanner:DoScan()
   for k, v in pairs(self.funcTable) do
      if getglobal(v.func) then
         v.exists = TRUE
      end
   end
end
If however you mean a way to parse the API for function names, ehh ... that might be possible with metatable searching. I'll bug kergoth about this, he was speaking of such a thing and I'm sure he has it done. Basically it would search for running functions and store information about them without needing input from the user. Other than that, there's always the wowwiki.com, which is a wholly nice resource.

Though I've probably missed your point entirely, I'm not always on the right wavelength, I hope that helps though.
__________________
There can only be so many ways of doing things; rope rubs against rope and it causes fire, fire cleanses all. Sadly, the ropes can't be around to appreciate it.

Short: Tolerance FTW?
  Reply With Quote
11-29-05, 12:58 AM   #3
Silversage
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 132
Jim,

I believe that Blizzard has intentionally and carefully removed access to the global namespace as an iterable entity. As a result, you can only play with global things (via getglobal and setglobal) if you already know their name.

Furthermore, there's no guarantee that any function registered against a slash command has been assigned to any global variable (i.e. has a name).

For example, you can paste the following snippet into Luapad and execute.

Code:
function RegisterCommand(id, cmdName, fcn)
  setglobal('SLASH_'..id..'1', '/' .. cmdName);
  SlashCmdList[id] = fcn;
end

RegisterCommand('XYZZY', 'xyzzy', function(cmd) ChatFrame1:AddMessage(cmd) end);
Now, '/xyzzy' is a defined command, but the function itself has never been assigned to a named variable.

hth, Q
  Reply With Quote
12-07-05, 02:07 AM   #4
JIM the Inventor
A Cyclonian
 
JIM the Inventor's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 42
Originally Posted by Rowne
Other than that, there's always the wowwiki.com, which is a wholly nice resource.
I worship wowwiki. In fact, I even perform ritual sacrifices. (create addOn concept, crumple notes into a ball, set on fire, repeat as necessary)

Many thanks you two! I did find the SlashCmdList to be very informative, if not exactly what I wanted ... and the [for .. in] loop syntax is a possible MVP in this environment.

I think you'll be well pleased with the results when "JIM's Slash Commander" comes out.

Last edited by JIM the Inventor : 12-09-05 at 09:52 PM. Reason: It's out! Whoohoo!!
  Reply With Quote
12-07-05, 10:07 PM   #5
Esamynn
Featured Artist
Premium Member
Featured
Join Date: Jan 2005
Posts: 395
Originally Posted by Qzot
I believe that Blizzard has intentionally and carefully removed access to the global namespace as an iterable entity. As a result, you can only play with global things (via getglobal and setglobal) if you already know their name.
Try getfenv(0)
That should return the global environment table. I haven't tried it yet myself, but Iriel said it works.
  Reply With Quote
12-08-05, 02:41 AM   #6
Gello
A Molten Giant
AddOn Author - Click to view addons
Join Date: Jan 2005
Posts: 521
Wow is that the global namespace? Hmm that's really really interesting.

/script function CountGlobals(m) local t,j,k,i=getfenv(0),0,0 for i in t do j=j+1 if string.find(i,"^"..m) then k=k+1 end end DEFAULT_CHAT_FRAME:AddMessage(j.." entries in getfenv(0). Beginning with \""..m.."\":"..k) end

Then:

/script CountGlobals("Name or start of name of mod here")

34776 entries in getfenv(0). Beginning with "Gatherer":741
34776 entries in getfenv(0). Beginning with "Recap":1353
34776 entries in getfenv(0). Beginning with "CT_":4849
34775 entries in getfenv(0). Beginning with "ItemRack":1217
34777 entries in getfenv(0). Beginning with "BankStatement":1007
34777 entries in getfenv(0). Beginning with "Titan":789
34777 entries in getfenv(0). Beginning with "TITAN":436
34777 entries in getfenv(0). Beginning with "Atlas":80

Printing out the list, the XML stuff creates a ton of globals. But then my mods are pretty heavily involved in the XML.

This is fun stuff. I fear it becoming another "metric" to mods tho lol. I don't wanna make my mods slash-driven I enjoy playing with the xml hehe
  Reply With Quote
12-09-05, 09:58 PM   #7
JIM the Inventor
A Cyclonian
 
JIM the Inventor's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 42
*drool* ... I'm going to see that "wow" and raise you a "goodness gravy"!

With a list like this I could possibly ... try to take over the world! ... of Warcraft!

That sure is a lot of data, though.
Hmmm. I bet Slash Commander could parse it some.
I'll get back to you boys and girls in a few months. Again, thank you VERY MUCH.
  Reply With Quote
12-09-05, 10:17 PM   #8
JIM the Inventor
A Cyclonian
 
JIM the Inventor's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 42
Originally Posted by Gello
This is fun stuff. I fear it becoming another "metric" to mods tho lol. I don't wanna make my mods slash-driven I enjoy playing with the xml hehe
Oh, ... Gello, please don't change. Your Recap is beautiful in its appearance.

As far as I'm concerned, the very best-designed mods don't rely on slash-commands. The use of slash-commands is just another viable option.

Hopefully I've been able to fill a niche by designing a listing for them: AddOn developers can't always be as thorough as CT in their documentation, nor as natural as the MapNotes team.

Blizzard has shown some unusual company smarts by letting you folk in the door. I hope to work with some of you eventually!
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » How to find a variable name for a function?


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