1周前,我对LuaBridge进行了首次小型测试,它可以从脚本中获取数据。

现在,我删除了此代码,并尝试在游戏引擎中包含Lua脚本,但该脚本不再起作用。
我尝试使用以下代码返回基本代码:

#include <iostream>

#include "lua5.2/lua.hpp"
#include "LuaBridge/LuaBridge.h"

using namespace luabridge;


int main()
{
    lua_State* L;
    L = luaL_newstate();

    if(!luaL_loadfile(L, "../../script.lua"))
        std::cout << "failed loading" << std::endl;

    LuaRef s = getGlobal(L, "nmbr");
    int luaInt = s.cast<int>();
    std::cout << luaInt << std::endl;

    return 0;
}

用这个脚本
nmbr = 30

它给了我:

PANIC:Lua API的单元格中出现未保护的错误(错误的参数2(预期数量,为零))
中止(核心已弃用)

当我尝试从脚本中获取字符串或函数时也是如此,但我不知道自己在做什么错。

感谢您的回答:)

最佳答案

luaL_loadfileex 的文档中:



这意味着脚本已加载,但尚未执行,因此实际上没有变量nmbr可供获取。您需要首先运行脚本以使代码正常工作(例如,通过调用 lua_call 进行示例)。

第一个简单示例in this LuaBridge tutorial很好地显示了这一点。

关于c++ - LuaBridge getGlobal总是返回nil,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32223334/

10-11 01:11