为了从lua访问c++,我已经阅读了一些有关luabridge的教程,但是我遇到了一个问题,我似乎无法通过搜索google找到答案。
我已经设置了一个示例程序来运行脚本并从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调用,我得到了对脚本的期望。
也许有什么明显的事情我已经错过了,因为我没有发现任何类似的东西?
我将非常感谢所有的帮助!
发布于 2014-08-05 15:56:31
问题是
luaL_openlibs(L);是在脚本运行之后。问题是在我遵循的教程中,我在开始尝试lua时遇到了同样的事情。
在创建了新的lua状态之后,所有这些都通过调用它来完美地工作。
https://stackoverflow.com/questions/25120227
复制相似问题