Note that there are some explanatory texts on larger screens.

plurals
  1. POBoost Python - Losing Data across function call
    primarykey
    data
    text
    <p>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:</p> <p>In my program, I have a class called Attack. With the following layout (simplified for example)</p> <pre><code>class Attack : public Action { public: virtual int CalculateDamage(const std::vector&lt;BattleCharacter*&gt;&amp; users, BattleCharacter* target, const std::vector&lt;Actions::ActionTarget&gt;&amp; targets, BattleField *field); protected: bool Hit; } </code></pre> <p>I exposed Attack to python, making it overridable, as follows:</p> <pre><code>struct AttackWrapper : Game::Battles::Actions::Attack { int AttackWrapper::CalculateDamage(const std::vector&lt;Game::Battles::BattleCharacter*&gt;&amp; users, Game::Battles::BattleCharacter* target, const std::vector&lt;Actions::ActionTarget&gt;&amp; targets, Game::Battles::BattleField *field) { return call_method&lt;int&gt;(self, "CalculateDamage", users, ptr(target), targets, ptr(field)); } int AttackWrapper::CalculateDamageDefault(const std::vector&lt;Game::Battles::BattleCharacter*&gt;&amp; users, Game::Battles::BattleCharacter* target, const std::vector&lt;Actions::ActionTarget&gt;&amp; targets, Game::Battles::BattleField *field) { return this-&gt;Attack::CalculateDamage(users, target, Targets, field); } } </code></pre> <p>And the python exposing is done as follows:</p> <pre><code>class_&lt;Attack, AttackWrapper, boost::shared_ptr&lt;Attack&gt;, bases&lt;Action&gt; &gt;("Attack") .def("CalculateDamage", &amp;AttackWrapper::CalculateDamageDefault); </code></pre> <p>I initially thought everything was working fine, as I can override the <code>CalculateDamage</code> method within python and have it work correctly. However, When I want to use the normal <code>Attack-&gt;CalculateDamage</code>, the following happens:</p> <p>I only call <code>CalculateDamage</code> when Hit is true, and I can confirm via break point when I hit this line, Hit is true:</p> <pre><code>return call_method&lt;int&gt;(self, "CalculateDamage", users, ptr(target), targets, ptr(field)); </code></pre> <p>Now, because I haven't overriden <code>CalculateDamage</code> in Python for this attack instance, it ends up resolving to <code>AttackWrapper::CalculateDamageDefault</code>. But by the time I enter AttackWrapper::CalculateDamageDefault, Hit is no longer true. That is, when I break on this line:</p> <pre><code>return this-&gt;Attack::CalculateDamage(users, target, Targets, field); </code></pre> <p>Hit is false. So somewhere between </p> <pre><code>return call_method&lt;int&gt;(self, "CalculateDamage", users, ptr(target), targets, ptr(field)); </code></pre> <p>resolving to </p> <pre><code>return this-&gt;Attack::CalculateDamage(users, target, Targets, field); </code></pre> <p>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...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload