squerpooo
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 2356 |
|
|
| 09 Aug 2013 12:53 PM |
| I've worked with functions allot, but I've kinda wondered how you get the function names to print them or something, but from an existing function variable. How? |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 09 Aug 2013 12:54 PM |
| Elaborate? Not sure what you mean by that. |
|
|
| Report Abuse |
|
|
squerpooo
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 2356 |
|
|
| 09 Aug 2013 12:56 PM |
| I am hoping to make a coding language some day when I grow up, but for now I am making a small testing language that only I know out of Lua, and I have a table with a few functions. I have another function to list all the function's names in that table, but I don't know how to get the names. |
|
|
| Report Abuse |
|
|
blocco
|
  |
| Joined: 14 Aug 2008 |
| Total Posts: 29474 |
|
|
| 09 Aug 2013 12:56 PM |
If you try to print a function as a variable, like a raw table or raw userdata you get its position in memory.
AFAIK, there is no real way to get the name of a function in the environment, unless you hax the environment with a special tostring function designed for this purpose |
|
|
| Report Abuse |
|
|
squerpooo
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 2356 |
|
|
| 09 Aug 2013 12:59 PM |
Tried it; got this:
function: 15407860
|
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 09 Aug 2013 01:04 PM |
The problem is that you are assuming that functions have names. The reality is that functions do not actually have names.
Every time you are running code in Lua, you are inside of some "environment", which itself is a table mapping all of the global variable names to their values (you can even get and modify that table by calling getfenv()).
When you do this: function myFunction(...) end
You are really doing this: myFunction = function(...) end
So, what you are trying to do is really something that doesn't make that much sense to do. There is a way to do something similar though, this should do the trick:
for variableName, value in pairs(getfenv(0)) do if type(value) == 'function' then -- a global variable which is a function, and now you have it's name. end end
That will find all of the global functions in the current environment. Of course, this won't actually find all of the functions, because some functions may not ever have a name at all:
(funciton() print("This function only exists for a brief moment so it can be called") end)() |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 09 Aug 2013 01:04 PM |
How does your table look? { ["funcname"] = function } OR { {"funcname", functon} } Or wut? |
|
|
| Report Abuse |
|
|
squerpooo
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 2356 |
|
|
| 09 Aug 2013 01:06 PM |
This is my table:
_G.Math = { ListFunctions = function() print() _G.Output.PrintLine("List of functions in this library below."); print() for i, v in pairs(_G.Math) do if type(v) == "function" then _G.Output.PrintLine(tostring(v)) end end print() end; } |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 09 Aug 2013 01:07 PM |
Then just do
for name, func in pairs(_G.Math) do print(name) end |
|
|
| Report Abuse |
|
|
squerpooo
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 2356 |
|
|
| 09 Aug 2013 01:08 PM |
| It returns the function's id. I want the name you assign as the variable. |
|
|
| Report Abuse |
|
|
| |
|
|
| 09 Aug 2013 01:09 PM |
_G.Math = { ListFunctions = function() print() _G.Output.PrintLine("List of functions in this library below."); print() for i, v in pairs(_G.Math) do -- Notice it returns i and v. if type(v) == "function" then _G.Output.PrintLine(i) -- The i is the function's index in the table. end end print() end; } |
|
|
| Report Abuse |
|
|
squerpooo
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 2356 |
|
|
| 09 Aug 2013 01:12 PM |
Thank you, @kirkyturky12.
PROBLEM SOLVED! |
|
|
| Report Abuse |
|
|
|
| 09 Aug 2013 01:13 PM |
"Thank you, @kirkyturky12."
I LITERALLY DID THE SAME THING, HE JUST PUT IT IN YOUR SCRIPT. I WANT CAKE, NAO. |
|
|
| Report Abuse |
|
|
|
| 09 Aug 2013 01:15 PM |
@18cwatford
You can have mine. *Gives cake.* |
|
|
| Report Abuse |
|
|
|
| 09 Aug 2013 01:16 PM |
*gives half of cake back*
yaaaaaaaaaaaay |
|
|
| Report Abuse |
|
|