From 645ba95bbc46f4add28ba6440a707e89ea609a25 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 22 Feb 2020 16:01:01 +0100 Subject: [PATCH] Performance improvements for battle and creature AddVolatileScript methods when passing a string as parameter. Instead of loading a script, then passing it to the ScriptSet class and checking if it already exists before invoking stack, we now check whether the ScriptSet has that script. If one already exists we just invoke stack on it, if not we continue to the process of creating a new script instance. --- src/Battling/Models/Battle.hpp | 7 ++++++- src/Battling/Models/Creature.cpp | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index dcc3280..b547474 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -77,7 +77,12 @@ namespace CreatureLib::Battling { const std::vector& GetSides() const { return _sides; } Script* GetVolatileScript(const std::string& key) const { return _volatile.Get(key); } void AddVolatileScript(const std::string& key) { - auto script = _library->LoadScript(ScriptCategory::Battle, key); + auto script = _volatile.Get(key); + if (script != nullptr) { + script->Stack(); + return; + } + script = _library->LoadScript(ScriptCategory::Battle, key); return _volatile.Add(script); } diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index 9df41cf..fa96d3c 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -186,7 +186,12 @@ void Battling::Creature::SetHeldItem(const std::string& itemName) { _heldItem = item; } void Battling::Creature::AddVolatileScript(const std::string& name) { - auto script = this->_library->LoadScript(ScriptCategory::Creature, name); + auto script = _volatile.Get(name); + if (script != nullptr) { + script->Stack(); + return; + } + script = this->_library->LoadScript(ScriptCategory::Creature, name); _volatile.Add(script); }