在 SWIG 调用中返回 lua 表
我有一个类,其中有一个名为 GetEnemiesLua 的方法。我已经使用 SWIG 将此类绑定到 lua,并且可以使用我的 lua 代码调用此方法。
我正在尝试获取返回 lua 对象表的方法。
这是我当前的代码:
void CGame::GetEnemiesLua(){
std::vector<Unit*> enemies = callback->GetEnemyUnits();
if( enemies.empty()){
lua_pushnil(ai->L);
return;
} else{
lua_newtable(ai->L);
int top = lua_gettop(ai->L);
int index = 1;
for (std::vector<Unit*>::iterator it = enemies.begin(); it != enemies.end(); ++it) {
//key
lua_pushinteger(ai->L,index);//lua_pushstring(L, key);
//value
CUnit* unit = new CUnit(callback,*it,this);
ai->PushIUnit(unit);
lua_settable(ai->L, -3);
++index;
}
::lua_pushvalue(ai->L,-1);
}
}
PushIUnit 如下:
void CTestAI::PushIUnit(IUnit* unit){
SWIG_NewPointerObj(L,unit,SWIGTYPE_p_IUnit,1);
}
为了测试这一点,我有以下代码:
t = game:GetEnemiesLua()
if t == nil then
game:SendToConsole("t is nil! ")
end
结果始终是“t is nil”,尽管这是不正确的。我在代码中放置了断点,它确实在循环,而不是执行 lua_pushnil 。
那么如何让我的方法在通过 lua 调用时返回一个表呢?
I have a class with a method called GetEnemiesLua. I have bound this class to lua using SWIG, and I can call this method using my lua code.
I am trying to get the method to return a lua table of objects.
Here is my current code:
void CGame::GetEnemiesLua(){
std::vector<Unit*> enemies = callback->GetEnemyUnits();
if( enemies.empty()){
lua_pushnil(ai->L);
return;
} else{
lua_newtable(ai->L);
int top = lua_gettop(ai->L);
int index = 1;
for (std::vector<Unit*>::iterator it = enemies.begin(); it != enemies.end(); ++it) {
//key
lua_pushinteger(ai->L,index);//lua_pushstring(L, key);
//value
CUnit* unit = new CUnit(callback,*it,this);
ai->PushIUnit(unit);
lua_settable(ai->L, -3);
++index;
}
::lua_pushvalue(ai->L,-1);
}
}
PushIUnit is as follows:
void CTestAI::PushIUnit(IUnit* unit){
SWIG_NewPointerObj(L,unit,SWIGTYPE_p_IUnit,1);
}
To test this I have the following code:
t = game:GetEnemiesLua()
if t == nil then
game:SendToConsole("t is nil! ")
end
The result is always 't is nil', despite this being incorrect. I have put breakpoints in the code and it is indeed going over the loop, rather than doing lua_pushnil
.
So how do I make my method return a table when called via lua?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的“GetEnemies”函数返回 void,SWIG 将按字面意思处理该值,并丢弃您尝试返回的任何值。您需要指定 C 函数返回一个数组或获取一个指向数组的指针。
例如,
下一步,告诉 SWIG 如何解释此输出值:
您的转换应返回“1”以指示您将单个表推送到堆栈上。
Your 'GetEnemies' function returns void which SWIG will take literally, throwing away any values you attempt to return. You'll want to specify your C function to either return an array or take a pointer to one.
For example,
Next, tell SWIG how to interpret this out value:
Your conversion should return '1' to indicate you pushed a single table onto the stack.
只有天知道 SWIG 在做什么(Lua API 太简单了,我避开了 SWIG 和它的小伙伴),但是在某个地方你需要与 Lua 沟通,你不仅在堆栈顶部留下了一个表,而且你想要返回该表。如果您自己编写 C 代码,
return 1;
就可以了。我不知道如何说服 SWIG 让它为您返回一个值,但我敢打赌void
返回类型不会给您带来任何好处。您可以尝试解决 SWIG 问题并创建一个带有原型的函数
如果您可以让内部的东西发挥作用,只需用
return 1;
结束例程,堆栈顶部的表就可以了它。God only knows what SWIG is doing (the Lua API is so simple that I avoid SWIG and its little friends), but somewhere you need to communicate to Lua that you are not only leaving a table on the top of the stack, but that you want to return that table. If you were writing the C code yourself
return 1;
would do it. I don't know how to persuade SWIG to get it to return a value for you, but I bet a return type ofvoid
is not doing you any favors.You might try working around SWIG and just create a function with prototype
If you can then get your inner stuff to work, just ending the routine with
return 1;
and the table on the top of the stack might do it.