diff --git a/apps/openmw/mwlua/luamanagerimp.cpp b/apps/openmw/mwlua/luamanagerimp.cpp index c11a87eaac..8516e5e21f 100644 --- a/apps/openmw/mwlua/luamanagerimp.cpp +++ b/apps/openmw/mwlua/luamanagerimp.cpp @@ -401,7 +401,7 @@ namespace MWLua return localScripts->getActorControls(); } - void LuaManager::addCustomLocalScript(const MWWorld::Ptr& ptr, int scriptId) + void LuaManager::addCustomLocalScript(const MWWorld::Ptr& ptr, int scriptId, std::string_view initData) { LocalScripts* localScripts = ptr.getRefData().getLuaScripts(); if (!localScripts) @@ -411,7 +411,7 @@ namespace MWLua if (ptr.isInCell() && MWBase::Environment::get().getWorldScene()->isCellActive(*ptr.getCell())) mActiveLocalScripts.insert(localScripts); } - localScripts->addCustomScript(scriptId); + localScripts->addCustomScript(scriptId, initData); } LocalScripts* LuaManager::createLocalScripts( diff --git a/apps/openmw/mwlua/luamanagerimp.hpp b/apps/openmw/mwlua/luamanagerimp.hpp index 9d02d11a3d..6482b7ddeb 100644 --- a/apps/openmw/mwlua/luamanagerimp.hpp +++ b/apps/openmw/mwlua/luamanagerimp.hpp @@ -57,7 +57,7 @@ namespace MWLua void setupPlayer(const MWWorld::Ptr& ptr) override; // Should be called once after each "clear". // Used only in Lua bindings - void addCustomLocalScript(const MWWorld::Ptr&, int scriptId); + void addCustomLocalScript(const MWWorld::Ptr&, int scriptId, std::string_view initData); void addUIMessage(std::string_view message) { mUIMessages.emplace_back(message); } void addInGameConsoleMessage(const std::string& msg, const Misc::Color& color) { diff --git a/apps/openmw/mwlua/objectbindings.cpp b/apps/openmw/mwlua/objectbindings.cpp index ab0e686e86..4fe520e68c 100644 --- a/apps/openmw/mwlua/objectbindings.cpp +++ b/apps/openmw/mwlua/objectbindings.cpp @@ -200,9 +200,9 @@ namespace MWLua if constexpr (std::is_same_v) { // Only for global scripts - objectT["addScript"] = [lua = context.mLua, luaManager = context.mLuaManager]( - const GObject& object, std::string_view path) { - const LuaUtil::ScriptsConfiguration& cfg = lua->getConfiguration(); + objectT["addScript"] = [context](const GObject& object, std::string_view path, + sol::optional initData) { + const LuaUtil::ScriptsConfiguration& cfg = context.mLua->getConfiguration(); std::optional scriptId = cfg.findId(path); if (!scriptId) throw std::runtime_error("Unknown script: " + std::string(path)); @@ -211,7 +211,12 @@ namespace MWLua "Script without CUSTOM tag can not be added dynamically: " + std::string(path)); if (object.ptr().getType() == ESM::REC_STAT) throw std::runtime_error("Attaching scripts to Static is not allowed: " + std::string(path)); - luaManager->addCustomLocalScript(object.ptr(), *scriptId); + if (initData) + context.mLuaManager->addCustomLocalScript( + object.ptr(), *scriptId, LuaUtil::serialize(*initData, context.mSerializer)); + else + context.mLuaManager->addCustomLocalScript( + object.ptr(), *scriptId, cfg[*scriptId].mInitializationData); }; objectT["hasScript"] = [lua = context.mLua](const GObject& object, std::string_view path) { const LuaUtil::ScriptsConfiguration& cfg = lua->getConfiguration(); diff --git a/components/lua/scriptscontainer.cpp b/components/lua/scriptscontainer.cpp index 6b6151b293..6f18727019 100644 --- a/components/lua/scriptscontainer.cpp +++ b/components/lua/scriptscontainer.cpp @@ -35,14 +35,13 @@ namespace LuaUtil mAPI.emplace(std::move(packageName), makeReadOnly(std::move(package))); } - bool ScriptsContainer::addCustomScript(int scriptId) + bool ScriptsContainer::addCustomScript(int scriptId, std::string_view initData) { - const ScriptsConfiguration& conf = mLua.getConfiguration(); - assert(conf.isCustomScript(scriptId)); + assert(mLua.getConfiguration().isCustomScript(scriptId)); std::optional onInit, onLoad; bool ok = addScript(scriptId, onInit, onLoad); if (ok && onInit) - callOnInit(scriptId, *onInit, conf[scriptId].mInitializationData); + callOnInit(scriptId, *onInit, initData); return ok; } diff --git a/components/lua/scriptscontainer.hpp b/components/lua/scriptscontainer.hpp index ebc7fd6d55..0177281be1 100644 --- a/components/lua/scriptscontainer.hpp +++ b/components/lua/scriptscontainer.hpp @@ -91,7 +91,7 @@ namespace LuaUtil // new script, adds it to the container, and calls onInit for this script. Returns `true` if the script was // successfully added. The script should have CUSTOM flag. If the flag is not set, or file not found, or has // syntax errors, returns false. If such script already exists in the container, then also returns false. - bool addCustomScript(int scriptId); + bool addCustomScript(int scriptId, std::string_view initData = ""); bool hasScript(int scriptId) const { return mScripts.count(scriptId) != 0; } void removeScript(int scriptId); diff --git a/files/lua_api/openmw/core.lua b/files/lua_api/openmw/core.lua index 38f73ec669..a39df78911 100644 --- a/files/lua_api/openmw/core.lua +++ b/files/lua_api/openmw/core.lua @@ -144,6 +144,7 @@ -- @function [parent=#GameObject] addScript -- @param self -- @param #string scriptPath Path to the script in OpenMW virtual filesystem. +-- @param #table initData (optional) Initialization data to be passed to onInit. If missed then Lua initialization data from content files will be used (if exists for this script). --- -- Whether a script with given path is attached to this object.