提升Python - 在函数调用中丢失数据

时间:2011-08-25 09:19:35

标签: c++ 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);

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

当Hit为真时我只调用CalculateDamage,当我点击此行时我可以通过断点确认,Hit为真:

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

现在,因为我没有在Python中为此攻击实例覆盖CalculateDamage,它最终会解析为AttackWrapper::CalculateDamageDefault。但是当我输入AttackWrapper :: CalculateDamageDefault时,Hit不再是真的。也就是说,当我打破这一行时:

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);

我的财产的价值已经丢失。我不知道是什么原因引起的。有没有人遇到过这样的事情?我想它可能正在复制......

0 个答案:

没有答案