当 Lua 绑定在该类中时,使用 LuaBind 调用类中的 Lua 函数
基本上,我只是希望能够在 Manager 类中创建一个干净的 Lua 实例,然后将类中的函数导出到 Lua,以便我可以在 Lua 中调用已创建的 C++ 类上的函数。
这是我目前正在考虑解决问题的方法。它可以编译,但 Lua 中没有任何反应。
有谁知道我做错了什么,或者有人有其他建议吗?
Manager.lua
newObject("Object", 1234)
printAll()
Manager.h
#ifndef MANAGER_H
#define MANAGER_H
#include <iostream>
#include <vector>
#include <string>
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include "luabind/luabind.hpp"
#include "Object.h"
class Manager
{
private :
lua_State *L;
std::vector<Object> obj;
public :
Manager();
void newObject(std::string t, int nm);
void printAll();
};
#endif
Manager.cpp
#include "Manager.h"
Manager::Manager()
{
luabind::open(L);
luabind::module(L) [
luabind::class_<Manager>("Manager")
.def(luabind::constructor<>())
.def("newObject", &Manager::newObject)
];
luaL_dofile(L, "Manager.lua");
}
void Manager::newObject(std::string t, int nm)
{
if(t == "Object")
{
Object object(nm);
obj.push_back(object);
}
}
void Manager::printAll()
{
for(unsigned int i = 0; i < obj.size(); i++)
std::cout << obj[i].getNum() << std::endl;
}
Basically, I just want to be able to have a clean Lua instance made inside of my Manager class, then export the functions in the class to Lua, so that I can call functions on the already created C++ class inside of Lua.
This is the current way I am looking at solving the issue. It compiles but nothing happens in Lua.
Does anyone know what I am doing wrong, or does anyone have any other suggestions?
Manager.lua
newObject("Object", 1234)
printAll()
Manager.h
#ifndef MANAGER_H
#define MANAGER_H
#include <iostream>
#include <vector>
#include <string>
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include "luabind/luabind.hpp"
#include "Object.h"
class Manager
{
private :
lua_State *L;
std::vector<Object> obj;
public :
Manager();
void newObject(std::string t, int nm);
void printAll();
};
#endif
Manager.cpp
#include "Manager.h"
Manager::Manager()
{
luabind::open(L);
luabind::module(L) [
luabind::class_<Manager>("Manager")
.def(luabind::constructor<>())
.def("newObject", &Manager::newObject)
];
luaL_dofile(L, "Manager.lua");
}
void Manager::newObject(std::string t, int nm)
{
if(t == "Object")
{
Object object(nm);
obj.push_back(object);
}
}
void Manager::printAll()
{
for(unsigned int i = 0; i < obj.size(); i++)
std::cout << obj[i].getNum() << std::endl;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 Luabind 创建一个类,然后提供该类的成员,那么 Luabind 就会这样做。它将向 Lua 公开一个具有成员的类。
如果没有该类类型的对象,则无法在 C++ 中调用成员函数。因此,当您通过 Luabind 公开一个类及其成员时,如果没有该类类型的对象,您将无法调用 Lua 中的成员函数。
因此,如果你有一些全局
Manager
对象,将其公开给 Lua 的正确方法是将对象本身公开给 Lua。使用 Luabind 获取全局表,然后将指向 Manager 对象的指针放入其中。或者,您可以在执行脚本时将Manager
对象实例作为参数传递。第二种方法的工作方式如下:
您的 Lua 脚本可以使用 Lua 的 varargs 机制获取一个实例:
If you use Luabind to create a class, and then provide members of that class, then Luabind will do exactly that. It will expose a class to Lua that has members.
You cannot call a member function in C++ without an object of that class's type. And therefore, when you expose a class and its members through Luabind, you will not be able to call member functions in Lua without an object of that class's type.
Therefore, if you have some global
Manager
object, the proper way to expose this to Lua is to expose the object itself to Lua. Use Luabind to get the global table, then put a pointer to your Manager object in it. Alternatively, you can pass theManager
object instance as a parameter when you execute the script.The second method would work something like this:
Your Lua script can get an instance with Lua's varargs mechanism: