Skip to main content
gamedev.net
🔒 Locked

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

Started by marcusz Mar 31, 2006 at 5:39 PM 3 replies 1.8k views
Original Post
marcusz
marcusz
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
marcusz
marcusz
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
marcusz
marcusz
How do I bump my own rating? :-)
Sneftel
Sneftel
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.
marcusz
marcusz
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.