Boost Python - 在函数调用中丢失数据
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论