From 690e5ef7574f6eb0e8038ab9f1c6b4b61b724fd5 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Thu, 11 Mar 2021 20:47:26 +0100 Subject: [PATCH] Don't resolve a script's target just to get the ID --- apps/openmw/mwscript/globalscripts.cpp | 20 ++++++++++++++++++++ apps/openmw/mwscript/globalscripts.hpp | 2 ++ apps/openmw/mwscript/interpretercontext.cpp | 7 ++++--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/apps/openmw/mwscript/globalscripts.cpp b/apps/openmw/mwscript/globalscripts.cpp index b287d33b3d..9de08fbce3 100644 --- a/apps/openmw/mwscript/globalscripts.cpp +++ b/apps/openmw/mwscript/globalscripts.cpp @@ -92,6 +92,21 @@ namespace return false; } }; + + struct IdGettingVisitor : public boost::static_visitor + { + std::string operator()(const MWWorld::Ptr& ptr) const + { + if(ptr.isEmpty()) + return {}; + return ptr.mRef->mRef.getRefId(); + } + + std::string operator()(const std::pair& pair) const + { + return pair.second; + } + }; } namespace MWScript @@ -110,6 +125,11 @@ namespace MWScript return ptr; } + std::string GlobalScriptDesc::getId() const + { + return boost::apply_visitor(IdGettingVisitor(), mTarget); + } + GlobalScripts::GlobalScripts (const MWWorld::ESMStore& store) : mStore (store) diff --git a/apps/openmw/mwscript/globalscripts.hpp b/apps/openmw/mwscript/globalscripts.hpp index 049e78804a..cd70f4e667 100644 --- a/apps/openmw/mwscript/globalscripts.hpp +++ b/apps/openmw/mwscript/globalscripts.hpp @@ -44,6 +44,8 @@ namespace MWScript const MWWorld::Ptr* getPtrIfPresent() const; // Returns a Ptr if one has been resolved MWWorld::Ptr getPtr(); // Resolves mTarget to a Ptr and caches the (potentially empty) result + + std::string getId() const; // Returns the target's ID -- if any }; class GlobalScripts diff --git a/apps/openmw/mwscript/interpretercontext.cpp b/apps/openmw/mwscript/interpretercontext.cpp index 270b0ea549..2e8f72e629 100644 --- a/apps/openmw/mwscript/interpretercontext.cpp +++ b/apps/openmw/mwscript/interpretercontext.cpp @@ -131,9 +131,10 @@ namespace MWScript std::string InterpreterContext::getTarget() const { - auto ptr = getReference(false); - if(!ptr.isEmpty()) - return ptr.mRef->mRef.getRefId(); + if(!mReference.isEmpty()) + return mReference.mRef->mRef.getRefId(); + else if(mGlobalScriptDesc) + return mGlobalScriptDesc->getId(); return {}; }