在 SWIG 调用中返回 lua 表

发布于 2024-08-30 15:23:14 字数 1108 浏览 5 评论 0原文

我有一个类,其中有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初 2024-09-06 15:23:14

您的“GetEnemies”函数返回 void,SWIG 将按字面意思处理该值,并丢弃您尝试返回的任何值。您需要指定 C 函数返回一个数组或获取一个指向数组的指针。

例如,

std::vector<Unit*>& CGame::GetEnemiesLua()
{
    return callback->GetEnemyUnits();
}

下一步,告诉 SWIG 如何解释此输出值:

// convert the return value into a Lua table
%typemap(argout, noblock=1) std::vector<Unit*>&
{
     SWIG_arg += ConvertEnemyVectorToLuaTable(*$1);
}

您的转换应返回“1”以指示您将单个表推送到堆栈上。

int ConvertEnenyVectorToLuaTable(std::vector<Unit*>& enemies)
{
    if(enemies->empty())
    {
        lua_pushnil(ai->L);
        return 1;  // you did push a NIL value
    } 
    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);

            //value
            ai->PushIUnit(*it);

            // set the table entry
            lua_settable(ai->L, -3);

            ++index;
         }

        // push the new table
        ::lua_pushvalue(ai->L,-1);
        return 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,

std::vector<Unit*>& CGame::GetEnemiesLua()
{
    return callback->GetEnemyUnits();
}

Next, tell SWIG how to interpret this out value:

// convert the return value into a Lua table
%typemap(argout, noblock=1) std::vector<Unit*>&
{
     SWIG_arg += ConvertEnemyVectorToLuaTable(*$1);
}

Your conversion should return '1' to indicate you pushed a single table onto the stack.

int ConvertEnenyVectorToLuaTable(std::vector<Unit*>& enemies)
{
    if(enemies->empty())
    {
        lua_pushnil(ai->L);
        return 1;  // you did push a NIL value
    } 
    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);

            //value
            ai->PushIUnit(*it);

            // set the table entry
            lua_settable(ai->L, -3);

            ++index;
         }

        // push the new table
        ::lua_pushvalue(ai->L,-1);
        return 1;
    }
}
菊凝晚露 2024-09-06 15:23:14

只有天知道 SWIG 在做什么(Lua API 太简单了,我避开了 SWIG 和它的小伙伴),但是在某个地方你需要与 Lua 沟通,你不仅在堆栈顶部留下了一个表,而且你想要返回该表。如果您自己编写 C 代码,return 1; 就可以了。我不知道如何说服 SWIG 让它为您返回一个值,但我敢打赌 void 返回类型不会给您带来任何好处。

您可以尝试解决 SWIG 问题并创建一个带有原型的函数

int lua_get_enemies(lua_State *L);

如果您可以让内部的东西发挥作用,只需用 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 of void is not doing you any favors.

You might try working around SWIG and just create a function with prototype

int lua_get_enemies(lua_State *L);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文