c++ - LuaBridge 断言错误

标签 c++ lua luabridge

我一直在阅读一些关于 luabridge 的教程,以便从 lua 访问 c++,但我遇到了一个问题,我似乎无法通过搜索谷歌找到答案。

我已经设置了一个示例程序来运行脚本并从 C++ 访问 lua,它运行良好。但是当我尝试在全局命名空间中注册一个函数时,它会在运行时提示 - 编译正常。

#include <iostream>
#include <string>

#include "../LuaBridge/LuaBridge.h"

using namespace luabridge;

void printMessage(const std::string& s) {
    std::cout << s << std::endl;
}

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

    getGlobalNamespace(L).addFunction("printMessage", printMessage);

    luaL_dofile(L, "script.lua");
    luaL_openlibs(L);
    lua_pcall(L, 0, 0, 0);
    LuaRef s = getGlobal(L, "testString");
    LuaRef n = getGlobal(L, "number");
    std::string luaString = s.cast<std::string>();
    int answer = n.cast<int>();
    std::cout << luaString << std::endl;
    std::cout << "And here's our number:" << answer << std::endl;
}

所以,这段代码 addFunction 调用给了我这个错误

Lua: /home/linuxxon/Programming/kingdoms-online/Script/Lua/../LuaBridge/detail/Namespace.h:1080: luabridge::Namespace& luabridge::Namespace::addFunction(const char*, FP) [with FP = void (*)(const std::basic_string<char>&)]: Assertion `(lua_type(L, (-1)) == 5)' failed.

在没有调用 addFunction 的情况下,我得到了脚本的预期结果。

因为我没有找到类似的东西,是否可能有什么明显的东西被我遗漏了?

非常感谢所有帮助!

最佳答案

问题是

luaL_openlibs(L);

是在脚本运行之后。问题出在我正在学习的教程中,我之前在尝试使用 lua 时遇到过同样的事情。

在创建新的 lua 状态后调用它,这一切都完美地工作。

关于c++ - LuaBridge 断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25120227/

相关文章:

lua - 当 key 正确时,OpenAI API 返回 "unauthorized"错误

android - Corona 构建的 apk 无法安装在三星 Galaxy S2 上

c++ - 类中的 luabridge 类对象

c++ 操作系统独立(windows 和 linux)开源随机数生成器库?

c++ - 如何在 SendInput 中使用扩展扫描码

c++ - 结构数组的初始化列表的行为

lua - 为什么 lua 模块会将其表的 __index 设置为自身?

c++ - 如何使用 LuaBridge 注册从同一个类继承的两个类?

c++ - 使用 LuaBridge 将 LuaJIT 绑定(bind)到 C++ 导致 "PANIC: unprotected error"

c++ - AVL 树和双向链表