Jump to content
  • Advertisement
Sign in to follow this  
marcusz

Lua: lua_register(lua_State* L, const char *s, lua_CFunction f, void* data)

This topic is 4440 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

Hi! Existing Lua API: lua_register(lua_State *L, const char *s, lua_CFunction f,) I want: lua_register(lua_State *L, const char *s, lua_CFunction f, void* data) Is there a way to associate a C-pointer, (or any type of data) when calling lua_register() and get to this data from within my C-callback when Lua calls it? Normally in C API:s, like the Windows API, when you register a C-callback you can also supply a void* that your callback gets as argument when it's invoked. This let's you register the same C-callback many times but with different void*:s. This is what I miss with lua_register(). I need this for wrapping the lua_State in a C++ class and having clients register callback interfaces with the wrapper. I'm not thrilled to have to pass the data pointer into the Lua scripts and require the Lua script to pass them as argument to my C API... Any ideas? /Marcus

Share this post


Link to post
Share on other sites
Advertisement
Ehm. Is this what lua_pushcclosure() is for?

I've sorted of use the IAHIGA on the concept of "closures" (Ignore And Hope It Goes Away)...


/Marcus

Share this post


Link to post
Share on other sites
That is indeed what lua_pushcclosure is for. Use lua_pushlightuserdata to stick your void pointer on the stack and then use it as an upvalue when registering the function. Later, you can get that upvalue from within the function and use it.

Share this post


Link to post
Share on other sites
Thank you again for your help! It helps a lot to get this confirmed.

I 've been think about this problem on and off for a long time, and now I really had to solve it.


/Marcus

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!