luabind - 将 C++ 转换成 Lua 再转回 C++。
2014-6-26 7:29:3
收藏:0
阅读:54
评论:1
我在luabind方面遇到了一个问题,或者至少我认为是个问题。
我有一个实体类,已经在lua中进行了注册。理想情况下,我想对其进行子类化并覆盖其函数。然后我想将其发送回C++并存储它。此外,我希望能够从存储的对象/指针的C++中调用其新函数。然而,我现在甚至无法让C++重新接受cEntity*类型的对象?在lua脚本中,我可以加载类、调用其变量和函数,我试图发送它到takeClass或takebOject,但它以空白类的形式出现,没有任何设置。例如,foo->name是""而不是"Entity1",id是0而不是1。你有什么想法吗?我已经在Google上搜索了至少一个星期,但没有理解这个问题,这完全阻碍了我的项目进展。
```
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
#include <iostream> #include <string> #include "luabind/luabind.hpp" extern "C" { #include "lauxlib.h" #include "lualib.h" } class cEntity { public: std::string name; int id; cEntity(); ~cEntity(); std::string getName() { return name; } void setName(std::string n) { name = n; } int getID() { return id; } void setID(int n) { id = n; } virtual void testFunction(){}; static void luabindClass(lua_State* L); }; //####################################################################### // Test function //####################################################################### struct luaTest { void TakeClass(cEntity* foo) { std::cout << foo->name << std::endl; } void TakeObject(luabind::object foo) { cEntity* foobar = luabind::object_cast<cEntity*>(foo); std::cout << foobar->name << std::endl; } void luabindClass(lua_State* L) { //Somewhere else luabind::module(L) [ luabind::class_<luaTest>("luaTest") // < "Animation" how we want to name the Class in the Lua runtime .def(luabind::constructor<>()) .def("TakeClass", &luaTest::TakeClass) .def("TakeObject", &luaTest::TakeObject) ]; luabind::globals(L)["test"] = this; } }; //####################################################################### // Entiy Class //####################################################################### //####################################################################### cEntity::cEntity() { name = "NotSet"; id = 0; } cEntity::~cEntity() { } void cEntity::luabindClass(lua_State* L) { luabind::module(L) [ luabind::class_<cEntity>("cEntity") // < "Animation" how we want to name the Class in the Lua runtime .def(luabind::constructor<>()) // < Binds the empty constructor .def_readwrite("name", &cEntity::name) .def_readwrite("id", &cEntity::id) ]; } char const* script = "entity = cEntity();\n" "entity.name = \"Entity1\";\n" "entity.id = 1;\n" "\n" "test:TakeClass(entity);\n" "test:TakeObject(entity);\n"; int main() { lua_State* L = lua_open(); luabind::open(L); cEntity::luabindClass(L); luaTest test; test.luabindClass(L); /* run the script */ if (luaL_dostring(L, script)) { std::cout << lua_tostring(L, -1) << std::endl; // 输出错误信息 } lua_close(L); }输出:
$ ccache clang++ -lluabind -llua -g3 ~/test.cxx && ./a.out Entity1 Entity1因此,在加载脚本时可能出现问题。 有任何错误信息输出吗?
(很抱歉我无法在 IRC 中回答这个问题。创建测试环境需要一些时间。)