I've created a wrapper around LuaBridge in my framework, so that I can easily swap out LuaBridge for an alternative library. I have certain classes that get registered with LuaBridge as part of my framework initialization. I can then access or instantiate those classes easily in Lua. The problem comes when I want to call a custom method on a class/table, that has already been registered in the framework init, that I've defined in Lua.
function MyObject:OnUpdate()
self:Init()
end
If I try and call OnUpdate with passing an instance of MyObject, so that I can utilize self, I get the following error:
"attempt to index a userdata value (local 'self')"
If I register MyObject manually instead of allowing my framework to initialize it during startup, it works perfectly. It's like Lua can resolve the metatable of MyObject even though I can clearly see it defined. I also can't seem to find much on that exact error from Lua.
I figured out my problem. The classes that I mentioned above that get registered are in another library (DLL). I'm generating keys for each class and storing those keys in the Lua registry. The key generation uses a static templated method that returns the address of a static char for each type. When the calling code comes back from the DLL and into my EXE, I'm registering a new class that inherits from one of the pre-registered classes from the DLL. When I lookup the key for the base class, it's different because the execution was running in the context of the DLL originally and now it's running in the context of my EXE. I assume this is a result of using templates in C++ and where the code is executing. After using C++ for more than 15 years, I learned something new. Hopefully this will help someone else.