Sign in to follow this  
SteveDeFacto

Get the name of the lua function inside the function.

Recommended Posts

I have one function bound to lua many times but with different names, I need to know the name that lua is calling the function under in order to properly process the variables and return the appropriate value.
int test(lua_State* lua_state)
{
// Get the name of this function from the lua state.
return 1;
}

Share this post


Link to post
Share on other sites
Functions in Lua don't have "names."

If you put a function into the global table then the name is just a key into the _G table that indexes a function.

Regardless, this seems like a questionable design. One function doing different work depending on the name seems like an obvious candidate to be replaced with multiple functions.

If you give us an example of what you're doing we can help you make better design choices.

Share this post


Link to post
Share on other sites
Quote:
Original post by m_switch
Functions in Lua don't have "names."

If you put a function into the global table then the name is just a key into the _G table that indexes a function.

Regardless, this seems like a questionable design. One function doing different work depending on the name seems like an obvious candidate to be replaced with multiple functions.

If you give us an example of what you're doing we can help you make better design choices.


This is what I am doing with it. the last entry is where I thought of this idea.

Share this post


Link to post
Share on other sites
This is a really bad idea; but it is possible through lua.
You need to construct a lua_Debug instance; and then query the stack of the state. After that, you can grab the first function off the stack; and assume that is your function.

However, this does not always work - some functions do not have names, for instance.

Share this post


Link to post
Share on other sites
I have to agree with bobofjoe that would be really nasty and hackish. If for some reason I personally needed this information I would store an upvalue for the function with the value being it's registered name.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this