簡體   English   中英

C++ Lua 嵌入,將表推送到基於參數的函數

[英]C++ Lua embedding, pushing table to a function that on arguments

所以我試圖在參數內的函數上推一個表
*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

但我不斷收到錯誤“嘗試調用表值”
你知道問題是什么嗎?

lua_pcall處的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調用之一應該可以解決它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM