c++ to lua to c++
c++ to lua to c++
我有一个luabbind问题,或者至少我认为这是一个问题
我有一个实体类,我注册了lua,
理想情况下,我想子类化它并覆盖它的函数,从那里我想把它发送回c++并存储它
另外,我希望能够从c++中从存储的对象/指针
调用它的新函数然而,我目前正在努力甚至让c++采取cEntity*类型的对象?在lua脚本中,我可以加载类,调用它的变量和函数,我试着发送它来调用takeClass或者takebOject但是它会以空白类的形式出现
例如foo->name是"而不是"Entity1", id是0而不是1
你知道我做错了什么吗?我在谷歌上搜索了至少一个星期没有运气理解这个问题,它完全停止了我的进步在我的项目上?//#######################################################################
// Test function
//#######################################################################
void luaTest::TakeClass(cEntity* foo)
{
cout << foo->name << endl;
}
void luaTest::TakeObject(luabind::object foo)
{
cEntity* foobar = luabind::object_cast<cEntity*>(foo);
cout << foobar->name << endl;
}
void luaTest::luabindClass(lua_State* L)
{
//Somewhere else
module(L)
[
class_<luaTest>("luaTest")
.def(constructor<>())
.def("TakeClass", &luaTest::TakeClass)
.def("TakeObject", &luaTest::TakeObject)
];
globals(L)["test"] = this;
}
//#######################################################################
// Entiy Class
//#######################################################################
class cEntity
{
public:
string name;
int id;
cEntity();
~cEntity();
static void luabindClass(lua_State* L);
};
//#######################################################################
cEntity::cEntity()
{
name = "NotSet";
id = 0;
}
cEntity::~cEntity()
{
}
void cEntity::luabindClass(lua_State* L)
{
module(L)
[
class_<cEntity>("cEntity")
.def(constructor<>())
.def_readwrite("name", &cEntity::name)
.def_readwrite("id", &cEntity::id)
];
}
//#######################################################################
// Lua File
//#######################################################################
entity = cEntity();
entity.name = "Entity1";
entity.id = 1;
test:TakeClass(entity);
test:TakeObject(entity);
//#######################################################################
//#######################################################################
// main
//#######################################################################
....
/* run the script */
if (luaL_dofile(L, "avg.lua")) {
std::cout << lua_tostring(L, -1) << std::endl; // Print out the error message
}
....
//#######################################################################
https://gist.github.com/take-cheeze/7264dbf1ea6e08a2d24a
#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; // Print out the error message
}
lua_close(L);
}
印刷:$ ccache clang++ -lluabind -llua -g3 ~/test.cxx && ./a.out
Entity1
Entity1
所以在脚本加载可能有问题。是否打印了任何错误信息?
对不起,我不能在IRC中回答这个问题。创建测试环境需要一些时间相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- "no matching function for call to 'Vector::Vector'"错误
- vector<vector<double>> to mxArray using memcpy
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 当覆盖存在时调用基本虚拟"binded to object"函数
- OpenGL VBO Indexing ( How to compute Index Array)
- 为什么我会" void value not ignored as it ought to be"?
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")