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++?