lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Thanks for the suggestion.  For some reason I did not get the two replies to my earlier email, but I see them in the archives http://lua-list.2524044.n2.nabble.com/Lua-5-2-creating-globals-with-userdata-td7451639.html.  Kevin and Thomas both replied with the same thought that I was trying to set the global before the library was loaded (and the metatable created).  They were right.  I had added the pointer to my setup function to preloaded_libs instead of loaded_libs, so the library was not set up until the require 'display' line in my script executed.

Thanks again for the help.

   
On Apr 10, 2012, at 5:27 PM, Coda Highland wrote:

Can anyone provide an example of how to create global userdata objects in C code that are accessible from Lua 5.2 scripts?

I've not used 5.2, but in 5.1 it's a simple matter of creating the
userdata object as normal, leave it on the top of the stack, and then
use lua_setfield(L, LUA_GLOBALSINDEX, "variableName");

A quick look at the documentation indicates that in 5.2, instead of
using LUA_GLOBALSINDEX you'll want to grab the global environment out
of the registry (lua_getfield(L, LUA_REGISTRYINDEX,
LUA_RIDX_GLOBALS);) and then set your userdata as a field in the table
that returns.

/s/ Adam