我有一些通过指针接收参数的类函数。但是,当我在boost :: python中公开并覆盖这些函数时,我对参数所做的任何更改都不会反映回原始函数,这意味着它不会像我预期的那样被指针传递。我以前把它们作为传递参考,但同样的问题仍然存在。举一个我的意思的例子,这里有一些我的代码,其中包含了不重要的功能和细节。
class StatusEffect
{
public:
virtual void TickCharacter(Character::BaseCharacter* character, Battles::BattleField *field, int ticks = 1);
}
//In my Scripting Code header file
struct StatusEffectWrapper : Game::StatusEffects::StatusEffect
{
virtual void TickCharacter(Game::Character::BaseCharacter* character, Game::Battles::BattleField *field, int ticks = 1);
virtual void TickCharacterDefault(Game::Character::BaseCharacter* character, Game::Battles::BattleField *field, int ticks = 1);
}
//In the actual scripting code
void StatusEffectWrapper::TickCharacter(Game::Character::BaseCharacter* character, Game::Battles::BattleField *field, int ticks)
{
call_method<void>(self, "TickCharacter", character, field, ticks);
}
void StatusEffectWrapper::TickCharacterDefault(Game::Character::BaseCharacter* character, Game::Battles::BattleField *field, int ticks)
{
this->StatusEffect::TickCharacter(character, field, ticks);
}
class_<StatusEffects::StatusEffect, boost::shared_ptr<StatusEffectWrapper> >("StatusEffect")
.def("TickCharacter", &StatusEffect::TickCharacter, &StatusEffectWrapper::TickCharacterDefault)
最后在我的Python文件中,我尝试做这样的事情
class StatCreepEffect(StatusEffect):
def __init__(self, name, stat_, rate_, descript, uid, turns, prior, canrefresh, poseffect, cats, conds, flgs, sep, pers):
StatusEffect.__init__(self, name, descript, uid, turns, prior, canrefresh, poseffect, cats, conds, flgs, sep, pers)
self.rate = rate_
self.stat = stat_
def TickCharacter(self, Character, Field, Ticks):
if (self.stat == Stat.HP):
Character.SetHP(int(round(Character.GetHP() + Character.GetInBattleStat(self.stat) * self.rate)))
print("HP Regenerated on character " + Character.GetName() + " New HP: " + str(Character.GetHP()))
elif (self.stat == Stat.MP):
Character.SetMP(int(round(Character.GetMP() + Character.GetInBattleStat(self.stat) * self.rate)))
elif (self.stat == Stat.SP):
Character.SetSP(int(round(Character.SetSP() + Character.GetInBattleStat(self.stat) * self.rate)))
Regen = StatCreepEffect("Regen", Stat.HP, 0.05, "Regenerates a small amount of HP every turn", PrimaryEngine.GetUID(), 14, 0, True, True, StatusCategories.SC_Regen, 0, StatusFlags.TickEffect, True, StatusPersistence.SP_Timer_Cure)
SELibrary.AddPythonEffect(Regen);
我在我的C ++代码中调用这样的效果
StatusEffect* regen = game.GetSELibrary().GetStatusEffect(std::string("Regen"));
while(character.GetHP() < 600)
{
regen->TickCharacter(&character, &field, 1);
std::cout << "Character HP: " << character.GetHP() << std::endl;
}
我按照http://wiki.python.org/moin/boost.python/OverridableVirtualFunctions
中的示例创建了此代码脚本编译并正确添加效果。 python脚本中的打印行打印正确的值。字符对象以400 HP开始,打印行输出430.但是,我的C ++代码中的std :: cout行总是输出400,这意味着脚本添加的额外30 HP不会反映在原始对象中。为清楚起见,这是上面C ++代码片段的控制台输出:
HP Regenerated on character Arian New HP: 430
Character HP: 400
有人可以帮我弄清楚如何使这项工作正常吗?非常感谢。
由于
如果重要,AddPythonEffect定义如下:
void StatusEffectsLibrary::AddPythonEffect(boost::shared_ptr<StatusEffect> effect)
{
if (effect.get() != nullptr)
{
NameToSEMap[effect->GetName()] = effect;
IDToSEMap[effect->GetUID()] = effect;
}
}
答案 0 :(得分:2)
在搜索了一下之后我找到了答案。事实证明,当通过引用(通过使用ref(T))或指针(通过使用ptr(T))传递时,必须明确指示call_method不要复制。 通过改变
void StatusEffectWrapper::TickCharacter(Game::Character::BaseCharacter* character, Game::Battles::BattleField *field, int ticks)
{
call_method<void>(self, "TickCharacter", character, field, ticks);
}
到
void StatusEffectWrapper::TickCharacter(Game::Character::BaseCharacter* character, Game::Battles::BattleField *field, int ticks)
{
call_method<void>(self, "TickCharacter", ptr(character), ptr(field), ticks);
}
我得到了我想要的行为。