在C++中获取指向Lua对象实例的指针

发布于 2024-08-04 15:27:41 字数 1594 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满意归宿 2024-08-11 15:27:41

如果在 Lua 中你这样做了,

zombie = Zombie('example zombie', 1)

那么你可以像这样获取僵尸的值:(

object_cast<Enemy*>(globals(L)["zombie"]);

object_castglobals 是 luabind 命名空间的成员,L是你的Lua状态)
这假设您知道在 Lua 中创建的变量的名称。

您始终可以从 Lua 调用带有指向 Enemy 的指针的 C++ 方法:

void AddEnemy(Enemy* enemy) { /* ... */ }
//...
module(L) [ def("AddEnemy", &AddEnemy) ]

并从 Lua 调用它。

a = Zombie("zombie", 1);
AddEnemy(a)

请注意,如果您这样做,

AddEnemy(Zombie("temp zombie", 1));

Lua 将在方法调用后删除“临时僵尸”并使指向该对象的任何指针无效。

If in Lua you do

zombie = Zombie('example zombie', 1)

then you can get the value of the zombie like this:

object_cast<Enemy*>(globals(L)["zombie"]);

(object_cast and globals 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:

void AddEnemy(Enemy* enemy) { /* ... */ }
//...
module(L) [ def("AddEnemy", &AddEnemy) ]

and call it from Lua

a = Zombie("zombie", 1);
AddEnemy(a)

Be aware that if you do

AddEnemy(Zombie("temp zombie", 1));

Lua will delete the "temp zombie" after the method call and invalidate any pointers to the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文