SWIG_NewPointerObj 且值始终为零
我正在使用 SWIG 来包装 C++ 对象以便在 lua 中使用,并且我尝试将数据传递给我的 lua 脚本中的方法,但它总是显示为“nil”
void CTestAI::UnitCreated(IUnit* unit){
lua_getglobal(L, "ai");
lua_getfield(L, -1, "UnitCreated");
swig_module_info *module = SWIG_GetModule( L );
swig_type_info *type = SWIG_TypeQueryModule( module, module, "IUnit *" );
SWIG_NewPointerObj(L,unit,type,0);
lua_epcall(L, 1, 0);
}
这是 lua 代码:
function AI:UnitCreated(unit)
if(unit == nil) then
game:SendToConsole("I CAN HAS nil ?")
else
game:SendToConsole("I CAN HAS UNITS!!!?")
end
end
unit 始终为 nil。我已经检查过,在 C++ 代码中,单元指针永远不会无效/为空
我也尝试过:
void CTestAI::UnitCreated(IUnit* unit){
lua_getglobal(L, "ai");
lua_getfield(L, -1, "UnitCreated");
SWIG_NewPointerObj(L,unit,SWIGTYPE_p_IUnit,0);
lua_epcall(L, 1, 0);
}
结果相同。
为什么会失败?我该如何修复它?
I'm using SWIG to wrap C++ objects for use in lua, and Im trying to pass data to a method in my lua script, but it always comes out as 'nil'
void CTestAI::UnitCreated(IUnit* unit){
lua_getglobal(L, "ai");
lua_getfield(L, -1, "UnitCreated");
swig_module_info *module = SWIG_GetModule( L );
swig_type_info *type = SWIG_TypeQueryModule( module, module, "IUnit *" );
SWIG_NewPointerObj(L,unit,type,0);
lua_epcall(L, 1, 0);
}
Here is the lua code:
function AI:UnitCreated(unit)
if(unit == nil) then
game:SendToConsole("I CAN HAS nil ?")
else
game:SendToConsole("I CAN HAS UNITS!!!?")
end
end
unit is always nil. I have checked and in the C++ code, the unit pointer is never invalid/null
I've also tried:
void CTestAI::UnitCreated(IUnit* unit){
lua_getglobal(L, "ai");
lua_getfield(L, -1, "UnitCreated");
SWIG_NewPointerObj(L,unit,SWIGTYPE_p_IUnit,0);
lua_epcall(L, 1, 0);
}
with identical results.
Why is this failing? How do I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在
函数 AI:UnitCreated(unit)
中使用冒号时,它会创建一个隐藏的self
参数来接收AI
实例。它实际上的行为是这样的:因此,当从 C 调用该函数时,您需要传递两个参数:
ai
实例和unit
参数。由于您只传递了一个参数,因此将self
设置为该参数,并将unit
设置为nil
。When you use the colon in
function AI:UnitCreated(unit)
, it creates a hiddenself
parameter that receives theAI
instance. It actually behaves like this:So when calling that function from C, you need to pass both parameters: the
ai
instance and theunit
parameter. Since you passed only one parameter,self
was set to it andunit
was set tonil
.