Boost Python - 在函数调用中丢失数据

发布于 2024-12-01 05:48:59 字数 2426 浏览 0 评论 0原文

我在 boost python 中遇到了一个非常奇怪的问题。我专注于特定的属性/方法来简化示例。情况是这样的:

在我的程序中,我有一个名为 Attack 的类。通过以下布局(例如简化的),

class Attack : public Action
{
public:
    virtual int CalculateDamage(const std::vector<BattleCharacter*>& users, BattleCharacter* target, const std::vector<Actions::ActionTarget>& targets, BattleField *field);
protected:
    bool Hit;
}

我将 Attack 暴露给 python,使其可重写,如下所示:

struct AttackWrapper : Game::Battles::Actions::Attack
{
    int AttackWrapper::CalculateDamage(const std::vector<Game::Battles::BattleCharacter*>& users, Game::Battles::BattleCharacter* target, const std::vector<Actions::ActionTarget>& targets, Game::Battles::BattleField *field)
    {
        return call_method<int>(self, "CalculateDamage", users, ptr(target), targets, ptr(field));
    }
    int AttackWrapper::CalculateDamageDefault(const std::vector<Game::Battles::BattleCharacter*>& users, Game::Battles::BattleCharacter* target, const std::vector<Actions::ActionTarget>& targets, Game::Battles::BattleField *field)
    {
        return this->Attack::CalculateDamage(users, target, Targets, field);
    }
}

并且 python 暴露的完成如下:

class_<Attack, AttackWrapper, boost::shared_ptr<Attack>, bases<Action> >("Attack")
    .def("CalculateDamage", &AttackWrapper::CalculateDamageDefault);

我最初认为一切正常,因为我可以重写 CalculateDamageCalculateDamage 方法并使其正常工作。但是,当我想使用正常的 Attack->CalculateDamage 时,会发生以下情况:

我仅在 Hit 为 true 时调用 CalculateDamage,并且可以通过断点确认我点击了这一行,Hit is true:

return call_method<int>(self, "CalculateDamage", users, ptr(target), targets, ptr(field));

现在,因为我没有在 Python 中针对此攻击实例覆盖 CalculateDamage,所以它最终解析为AttackWrapper::CalculateDamageDefault。但当我进入 AttackWrapper::CalculateDamageDefault 时,Hit 不再为 true。也就是说,当我打破这条线时:

return this->Attack::CalculateDamage(users, target, Targets, field);

命中是错误的。因此,在

return call_method<int>(self, "CalculateDamage", users, ptr(target), targets, ptr(field));

解决

return this->Attack::CalculateDamage(users, target, Targets, field);

我的财产价值之间的某个地方就丢失了。我不知道是什么原因造成的。以前有人遇到过这样的事情吗?我觉得可能是被抄袭了……

I'm having a really weird issue in boost python. I'm focusing on a particular property/method to simplify the example. Here's the situation:

In my program, I have a class called Attack. With the following layout (simplified for example)

class Attack : public Action
{
public:
    virtual int CalculateDamage(const std::vector<BattleCharacter*>& users, BattleCharacter* target, const std::vector<Actions::ActionTarget>& targets, BattleField *field);
protected:
    bool Hit;
}

I exposed Attack to python, making it overridable, as follows:

struct AttackWrapper : Game::Battles::Actions::Attack
{
    int AttackWrapper::CalculateDamage(const std::vector<Game::Battles::BattleCharacter*>& users, Game::Battles::BattleCharacter* target, const std::vector<Actions::ActionTarget>& targets, Game::Battles::BattleField *field)
    {
        return call_method<int>(self, "CalculateDamage", users, ptr(target), targets, ptr(field));
    }
    int AttackWrapper::CalculateDamageDefault(const std::vector<Game::Battles::BattleCharacter*>& users, Game::Battles::BattleCharacter* target, const std::vector<Actions::ActionTarget>& targets, Game::Battles::BattleField *field)
    {
        return this->Attack::CalculateDamage(users, target, Targets, field);
    }
}

And the python exposing is done as follows:

class_<Attack, AttackWrapper, boost::shared_ptr<Attack>, bases<Action> >("Attack")
    .def("CalculateDamage", &AttackWrapper::CalculateDamageDefault);

I initially thought everything was working fine, as I can override the CalculateDamage method within python and have it work correctly. However, When I want to use the normal Attack->CalculateDamage, the following happens:

I only call CalculateDamage when Hit is true, and I can confirm via break point when I hit this line, Hit is true:

return call_method<int>(self, "CalculateDamage", users, ptr(target), targets, ptr(field));

Now, because I haven't overriden CalculateDamage in Python for this attack instance, it ends up resolving to AttackWrapper::CalculateDamageDefault. But by the time I enter AttackWrapper::CalculateDamageDefault, Hit is no longer true. That is, when I break on this line:

return this->Attack::CalculateDamage(users, target, Targets, field);

Hit is false. So somewhere between

return call_method<int>(self, "CalculateDamage", users, ptr(target), targets, ptr(field));

resolving to

return this->Attack::CalculateDamage(users, target, Targets, field);

my property's value is lost. I have no idea what could be causing this. Has anyone encountered something like this before? I think it may be being copied...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文