在C++中获取指向Lua对象实例的指针
我正在使用 Luabind 将一个基类从 C++ 公开到 Lua,我可以从中 在 Lua 中派生类。这部分工作正常,我可以从 Lua 中的派生类调用 C++ 方法。
现在我想做的是在我的 C++ 程序中获取一个指向基于 Lua 的实例的指针。
C++->绑定
class Enemy {
private:
std::string name;
public:
Enemy(const std::string& n)
: name(n) {
}
const std::string& getName() const {
return name;
}
void setName(const std::string& n) {
name = n;
}
virtual void update() {
std::cout << "Enemy::update() called.\n";
}
};
class EnemyWrapper : public Enemy, public luabind::wrap_base {
public:
EnemyWrapper(const std::string& n)
: Enemy(n) {
}
virtual void update() {
call<void>("update");
}
static void default_update(Enemy* ptr) {
ptr->Enemy::update();
}
};
// Now I bind the class as such:
module(L)
[
class_<Enemy, EnemyWrapper>("Enemy")
.def(constructor<const std::string&, int>())
.property("name", &Enemy::getName, &Enemy::setName)
.def("update", &Enemy::update, &EnemyWrapper::default_update)
];
基于 Lua 的派生类
class 'Zombie' (Enemy) function Zombie:__init(name) Enemy.__init(self, name) end function Zombie:update() print('Zombie:update() called.') end
现在假设我从 Lua 创建了以下对象:
a = Zombie('example zombie', 1)
如何获取该对象的引用作为 C++ 中基类的指针?
I am using Luabind to expose a base class from C++ to Lua from which I can derive classes in Lua. This part works correctly and I am able to call C++ methods from my derived class in Lua.
Now what I want to do is obtain a pointer to the Lua-based instance in my C++ program.
C++ -> Binding
class Enemy {
private:
std::string name;
public:
Enemy(const std::string& n)
: name(n) {
}
const std::string& getName() const {
return name;
}
void setName(const std::string& n) {
name = n;
}
virtual void update() {
std::cout << "Enemy::update() called.\n";
}
};
class EnemyWrapper : public Enemy, public luabind::wrap_base {
public:
EnemyWrapper(const std::string& n)
: Enemy(n) {
}
virtual void update() {
call<void>("update");
}
static void default_update(Enemy* ptr) {
ptr->Enemy::update();
}
};
// Now I bind the class as such:
module(L)
[
class_<Enemy, EnemyWrapper>("Enemy")
.def(constructor<const std::string&, int>())
.property("name", &Enemy::getName, &Enemy::setName)
.def("update", &Enemy::update, &EnemyWrapper::default_update)
];
Lua-based Derived Class
class 'Zombie' (Enemy) function Zombie:__init(name) Enemy.__init(self, name) end function Zombie:update() print('Zombie:update() called.') end
Now let's say I have the following object created from Lua:
a = Zombie('example zombie', 1)
How can I get a reference of that object as a pointer to the base class in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果在 Lua 中你这样做了,
那么你可以像这样获取僵尸的值:(
object_cast
和globals
是 luabind 命名空间的成员,L
是你的Lua状态)这假设您知道在 Lua 中创建的变量的名称。
您始终可以从 Lua 调用带有指向 Enemy 的指针的 C++ 方法:
并从 Lua 调用它。
请注意,如果您这样做,
Lua 将在方法调用后删除“临时僵尸”并使指向该对象的任何指针无效。
If in Lua you do
then you can get the value of the zombie like this:
(
object_cast
andglobals
are members of the luabind namespace,L
is your Lua state)This assumes you know the names of the variables you create in Lua.
You can always call a C++ method from Lua that takes a pointer to Enemy:
and call it from Lua
Be aware that if you do
Lua will delete the "temp zombie" after the method call and invalidate any pointers to the object.