From 670abd7229bead4d91b3dc76b7621eca018ef17a Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 1 Aug 2020 10:27:46 +0200 Subject: [PATCH] Fixed Segfault when overriding talent. --- src/Battling/Models/Creature.cpp | 6 ++++-- tests/BattleTests/CreatureTests.cpp | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 0268aee..116d5ff 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -171,9 +171,11 @@ void Battling::Creature::Heal(uint32_t amount, bool canRevive) { void Battling::Creature::OverrideActiveTalent(const ArbUt::StringView& talent) { _hasOverridenTalent = true; - _activeTalent->OnRemove(); + if (_activeTalent != nullptr) { + _activeTalent->OnRemove(); + _activeTalent.reset(this->_library->LoadScript(ScriptCategory::Talent, talent)); + } _overridenTalentName = talent; - _activeTalent.reset(this->_library->LoadScript(ScriptCategory::Talent, talent)); } const std::unordered_set& Battling::Creature::GetTypes() const noexcept { return _types; } diff --git a/tests/BattleTests/CreatureTests.cpp b/tests/BattleTests/CreatureTests.cpp index 3ba17c8..a17697f 100644 --- a/tests/BattleTests/CreatureTests.cpp +++ b/tests/BattleTests/CreatureTests.cpp @@ -46,4 +46,12 @@ TEST_CASE("Increase creature stat boost", "[Library]") { delete creature; } +TEST_CASE("Override Creature talent", "[Library]") { + auto library = TestLibrary::Get(); + auto creature = CreateCreature(library, "testSpecies1"_cnc, 1).Create(); + creature->OverrideActiveTalent("foobar"); + REQUIRE(creature->GetActiveTalent() == "foobar"); + delete creature; +} + #endif \ No newline at end of file