generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Function Names

Previous Thread :: Next Thread 
squerpooo is not online. 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 is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
09 Aug 2013 12:54 PM
Elaborate? Not sure what you mean by that.
Report Abuse
squerpooo is not online. 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 is not online. 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 is not online. squerpooo
Joined: 18 Feb 2011
Total Posts: 2356
09 Aug 2013 12:59 PM
Tried it; got this:

function: 15407860
Report Abuse
stravant is not online. stravant
Forum Moderator
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 is not online. 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 is not online. 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 is not online. 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 is not online. 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
18cwatford is not online. 18cwatford
Joined: 08 Oct 2009
Total Posts: 5410
09 Aug 2013 01:09 PM
_G.Output.PrintLine(i)
Report Abuse
kirkyturky12 is not online. kirkyturky12
Joined: 30 Apr 2010
Total Posts: 1915
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 is not online. squerpooo
Joined: 18 Feb 2011
Total Posts: 2356
09 Aug 2013 01:12 PM
Thank you, @kirkyturky12.

PROBLEM SOLVED!
Report Abuse
18cwatford is not online. 18cwatford
Joined: 08 Oct 2009
Total Posts: 5410
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
kirkyturky12 is not online. kirkyturky12
Joined: 30 Apr 2010
Total Posts: 1915
09 Aug 2013 01:15 PM
@18cwatford

You can have mine. *Gives cake.*
Report Abuse
18cwatford is not online. 18cwatford
Joined: 08 Oct 2009
Total Posts: 5410
09 Aug 2013 01:16 PM
*gives half of cake back*

yaaaaaaaaaaaay
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image