开发者

Boost Python - Losing Data across function call

开发者 https://www.devze.com 2023-03-31 04:20 出处:网络
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:

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>开发者_如何学JAVA& 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...

0

精彩评论

暂无评论...
验证码 换一张
取 消