diff --git a/apps/openmw/mwlua/mwscriptbindings.cpp b/apps/openmw/mwlua/mwscriptbindings.cpp index 1f0a081fe4..6ed3486c8a 100644 --- a/apps/openmw/mwlua/mwscriptbindings.cpp +++ b/apps/openmw/mwlua/mwscriptbindings.cpp @@ -8,6 +8,7 @@ #include "../mwbase/world.hpp" #include "../mwscript/globalscripts.hpp" #include "../mwworld/esmstore.hpp" +#include "../mwworld/worldimp.hpp" #include "object.hpp" @@ -27,6 +28,16 @@ namespace MWLua else return MWBase::Environment::get().getScriptManager()->getGlobalScripts().getLocals(mId); } + bool isRunning() const + { + if (mObj.has_value()) // local script + { + MWWorld::LocalScripts& localScripts = MWBase::Environment::get().getWorld()->getLocalScripts(); + return localScripts.isRunning(mId, mObj->ptr()); + } + + return MWBase::Environment::get().getScriptManager()->getGlobalScripts().isRunning(mId); + } }; struct MWScriptVariables { @@ -114,6 +125,7 @@ namespace MWLua = context.mLua->sol().new_usertype("MWScriptVariables"); mwscript[sol::meta_function::to_string] = [](const MWScriptRef& s) { return std::string("MWScript{") + s.mId.toDebugString() + "}"; }; + mwscript["isRunning"] = sol::readonly_property([](const MWScriptRef& s) { return s.isRunning(); }); mwscript["recordId"] = sol::readonly_property([](const MWScriptRef& s) { return s.mId.serializeText(); }); mwscript["variables"] = sol::readonly_property([](const MWScriptRef& s) { return MWScriptVariables{ s }; }); mwscript["object"] = sol::readonly_property([](const MWScriptRef& s) -> sol::optional { diff --git a/apps/openmw/mwworld/localscripts.cpp b/apps/openmw/mwworld/localscripts.cpp index 955e1a91f8..8f5dc63dfb 100644 --- a/apps/openmw/mwworld/localscripts.cpp +++ b/apps/openmw/mwworld/localscripts.cpp @@ -177,3 +177,8 @@ void MWWorld::LocalScripts::remove(const Ptr& ptr) break; } } + +bool MWWorld::LocalScripts::isRunning(const ESM::RefId& scriptName, const Ptr& ptr) const +{ + return std::ranges::find(mScripts, std::pair(scriptName, ptr)) != mScripts.end(); +} diff --git a/apps/openmw/mwworld/localscripts.hpp b/apps/openmw/mwworld/localscripts.hpp index 09a913e655..fd7ae23edf 100644 --- a/apps/openmw/mwworld/localscripts.hpp +++ b/apps/openmw/mwworld/localscripts.hpp @@ -45,6 +45,9 @@ namespace MWWorld void remove(const Ptr& ptr); ///< Remove script for given reference (ignored if reference does not have a script listed). + + bool isRunning(const ESM::RefId&, const Ptr&) const; + ///< Is the local script running?. }; } diff --git a/files/lua_api/openmw/world.lua b/files/lua_api/openmw/world.lua index 4e67fd4c7c..d92471153b 100644 --- a/files/lua_api/openmw/world.lua +++ b/files/lua_api/openmw/world.lua @@ -52,6 +52,7 @@ -- @field #string recordId Id of the script -- @field openmw.core#GameObject object The object the script is attached to. -- @field openmw.core#GameObject player The player the script refers to. +-- @field #boolean isRunning Whether the script is currently running -- @field #MWScriptVariables variables Local variables of the script (mutable) -- @usage -- for _, script in ipairs(world.mwscript.getLocalScripts(object)) do