Within my Lua-application I have some own functions defined that are registered with lua_register("lua_fct_name","my_fct_name")
so that they are known to the Lua script.
Now I have some custom/user data that need to be accessible within my_fct_name()
. It is just a pointer to a memory area I manage for my own so I use lua_pushlightuserdata (L,data)
to add it to Lua-context.
Now it seems I don't have the correct position to add these data. When done right after L was created I can't access the data in my_fct_name()
, here lua_touserdata(L,1)
does return NULL
, so it is not available on the stack. When done right before lua_pcall()
executes the script, I get an error message about unexpected data.
So where/when do I have to set my user data so that they are available within my_fct_name()
?
Since you're refusing to provide your code, which is not helping at all, let me provide an example.
Setup of Lua state (C side):
Lua code V1:
Lua code V2:
In the V1 you would get your pointer like this, since you provide it as an argument:
In the V2, you would have to get it from the globals table (which would work for V1 as well)
Have a look at the Lua Reference Manual and Programming in Lua. Mind you that the book that is available online is based on Lua 5.0, so it's not completely up to date, but should be sufficient for learning basics of interacting between C and Lua.