【问题标题】:C++ Lua embedding, pushing table to a function that on argumentsC ++ Lua嵌入,将表推送到参数上的函数
【发布时间】:2021-12-09 02:25:56
【问题描述】:

所以我试图在参数内的函数上推送一个表
*lua

function test1(varlist)
print(varlist[1])
print(varlist[2])
print(varlist[3])
end

addHook("string", "string2", test1)

*cpp

static int lua_addHook(lua_State* L) {
    if (lua_isstring(L, 1) && lua_isstring(L, 2) && lua_isfunction(L, 3)) {
        lua_newtable(L);

        lua_newtable(L);
        for (size_t i = 0; i < 3; ++i) {
            lua_pushinteger(L, i + 1);
            lua_pushstring(L, string("string varlist: " + to_string(i)).c_str());
            lua_settable(L, -3);
        }
        if (lua_pcall(L, 1, 0, 0) != 0) {
            printf("error: %s\n", lua_tostring(L, -1));
            lua_pop(L, 1);
        }
    }
    return 1;
}

所以它应该打印

字符串变量列表:0
字符串变量列表:1
字符串变量列表:2

但我不断收到错误“尝试调用表值”
你知道是什么问题吗?

【问题讨论】:

  • 您是不是意思是将两张表压入堆栈而不是一张?
  • 我的意思是在一个有参数的函数上推一个表

标签: c++ lua


【解决方案1】:

堆栈在lua_pcall处看起来像这样:

table (constructed by the loop above) # STACK TOP and arg1 to function call
table (empty)                         # interpreted as the function to call
function test1
string "string1"
string "string"

摆脱lua_newtable 调用之一应该可以解决它。

【讨论】:

  • 天哪,我为什么这么笨,谢谢先生,你救了我的一天
猜你喜欢
  • 2012-06-28
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 2017-06-05
  • 2021-12-27
  • 1970-01-01
  • 2012-01-27
  • 2014-05-14
相关资源
最近更新 更多