diff --git a/apps/openmw/mwlua/types/misc.cpp b/apps/openmw/mwlua/types/misc.cpp index c565094022..c451ee4f98 100644 --- a/apps/openmw/mwlua/types/misc.cpp +++ b/apps/openmw/mwlua/types/misc.cpp @@ -1,5 +1,6 @@ #include "types.hpp" +#include #include #include #include @@ -25,6 +26,25 @@ namespace MWLua addRecordFunctionBinding(miscellaneous, context); + miscellaneous["setSoul"] = [](const GObject& object, std::string_view soulId) { + ESM::RefId creature = ESM::RefId::deserializeText(soulId); + const MWWorld::ESMStore& store = MWBase::Environment::get().getWorld()->getStore(); + + if (!store.get().search(creature)) + { + // TODO: Add Support for NPC Souls + throw std::runtime_error("Cannot use non-existent creature as a soul: " + std::string(soulId)); + } + + object.ptr().getCellRef().setSoul(creature); + }; + miscellaneous["soul"] = [](const Object& object) -> sol::optional { + ESM::RefId soul = object.ptr().getCellRef().getSoul(); + if (soul.empty()) + return sol::nullopt; + else + return soul.serializeText(); + }; sol::usertype record = context.mLua->sol().new_usertype("ESM3_Miscellaneous"); record[sol::meta_function::to_string] diff --git a/files/lua_api/openmw/types.lua b/files/lua_api/openmw/types.lua index 6eaa809cac..3c4e95f88b 100644 --- a/files/lua_api/openmw/types.lua +++ b/files/lua_api/openmw/types.lua @@ -802,6 +802,18 @@ -- @param #any objectOrRecordId -- @return #MiscellaneousRecord +--- +-- Returns the read-only soul of a miscellaneous item +-- @function [parent=#Miscellaneous] soul +-- @param openmw.core#GameObject object +-- @return #string + +--- +-- Sets the soul of a miscellaneous item, intended for soul gem objects; Must be used in a global script. +-- @function [parent=#Miscellaneous] setSoul +-- @param openmw.core#GameObject object +-- @param #string soulId Record ID for the soul of the creature to use + --- -- @type MiscellaneousRecord -- @field #string id The record ID of the miscellaneous item