简体   繁体   English

C ++ LuaBridge尝试索引用户数据值(本地“自身”)

[英]C++ LuaBridge attempt to index a userdata value (local 'self')

I've created a wrapper around LuaBridge in my framework, so that I can easily swap out LuaBridge for an alternative library. 我在框架中围绕LuaBridge创建了一个包装器,以便可以轻松地将LuaBridge换成其他库。 I have certain classes that get registered with LuaBridge as part of my framework initialization. 我的某些类在框架初始化过程中已向LuaBridge注册。 I can then access or instantiate those classes easily in Lua. 然后,我可以在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. 当我想在已经在Lua中定义的框架init中注册的类/表上调用自定义方法时,就会出现问题。

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: 如果尝试通过传递MyObject实例来调用OnUpdate,以便可以利用self,则会出现以下错误:

"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. 如果我手动注册MyObject而不是允许我的框架在启动期间对其进行初始化,则它可以正常工作。 It's like Lua can resolve the metatable of MyObject even though I can clearly see it defined. 就像Lua可以解析MyObject的元表一样,即使我可以清楚地看到它已定义。 I also can't seem to find much on that exact error from Lua. 关于Lua的确切错误,我似乎也找不到太多。

I figured out my problem. 我发现了我的问题。 The classes that I mentioned above that get registered are in another library (DLL). 我上面提到的要注册的类在另一个库(DLL)中。 I'm generating keys for each class and storing those keys in the Lua registry. 我为每个类生成密钥,并将这些密钥存储在Lua注册表中。 The key generation uses a static templated method that returns the address of a static char for each type. 密钥生成使用静态模板化方法,该方法为每种类型返回静态char的地址。 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. 当调用代码从DLL返回并进入我的EXE时,我正在注册一个新类,该类继承自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. 当我查找基类的键时,它有所不同,因为执行最初在DLL上下文中运行,而现在在EXE上下文中运行。 I assume this is a result of using templates in C++ and where the code is executing. 我认为这是在C ++中使用模板并在其中执行代码的结果。 After using C++ for more than 15 years, I learned something new. 使用C ++超过15年后,我学到了一些新东西。 Hopefully this will help someone else. 希望这会帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM