标签: luaplus

LuaPlus:如何使函数返回表?

我想知道如何从C++端创建和注册一个函数,该函数在从Lua端调用时返回一个表.
我尝试过很多东西,但没有什么能真正起作用.:/

(对不起长代码)这个例子不起作用,因为Register()需要一个"luaCFunction"样式函数:

LuaPlus::LuaObject Test( LuaPlus::LuaState* state ) {
    int top = state->GetTop();
    std::string var( state->ToString(1) );

    LuaPlus::LuaObject tableObj(state);
    tableObj.AssignNewTable(state);

    if (var == "aaa")
        tableObj.SetString("x", "ABC");
    else if (var == "bbb")
        tableObj.SetString("x", "DEF");
    tableObj.SetString("y", "XYZ");
    return tableObj;
}
int main()
{
    LuaPlus::LuaState* L = LuaPlus::LuaState::Create(true);
     //without true I can't access the standard libraries like "math.","string."...
     //with true, GetLastError returns 2 though (ERROR_FILE_NOT_FOUND)
     //no side effects noticed though

    LuaPlus::LuaObject globals = L->GetGlobals();

    globals.Register("Test",Test);

    char pPath[MAX_PATH];
    GetCurrentDirectory(MAX_PATH,pPath);
    strcat_s(pPath,MAX_PATH,"\\test.lua");
    if(L->DoFile(pPath)) {
        if( L->GetTop() …
Run Code Online (Sandbox Code Playgroud)

c++ lua function lua-table luaplus

6
推荐指数
1
解决办法
1203
查看次数

标签 统计

c++ ×1

function ×1

lua ×1

lua-table ×1

luaplus ×1