Jump to content
  • Advertisement
Sign in to follow this  
RetroBilly

Get the name of the lua function inside the function.

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!