# Get the name of the lua function inside the function.

This topic is 2930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites
Quote:
 Original post by m_switchFunctions 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 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 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 on other sites
Yeh, this is exactly what upvalues to C functions are for.

##### Share on other sites
Quote:
 Original post by SneftelYeh, this is exactly what upvalues to C functions are for.

Yes they work perfectly, thanks guys!