diff --git a/apps/openmw/mwlua/types/item.cpp b/apps/openmw/mwlua/types/item.cpp index 648229a5e5..d1f80e44f4 100644 --- a/apps/openmw/mwlua/types/item.cpp +++ b/apps/openmw/mwlua/types/item.cpp @@ -1,5 +1,6 @@ #include +#include "../../mwmechanics/spellutil.hpp" #include "../../mwworld/class.hpp" #include "../itemdata.hpp" @@ -10,10 +11,16 @@ namespace MWLua { void addItemBindings(sol::table item, const Context& context) { - item["getEnchantmentCharge"] - = [](const Object& object) { return object.ptr().getCellRef().getEnchantmentCharge(); }; - item["setEnchantmentCharge"] - = [](const GObject& object, float charge) { object.ptr().getCellRef().setEnchantmentCharge(charge); }; + item["getEnchantmentCharge"] = [](const Object& object) -> sol::optional { + float charge = object.ptr().getCellRef().getEnchantmentCharge(); + if (charge == -1) + return sol::nullopt; + else + return charge; + }; + item["setEnchantmentCharge"] = [](const GObject& object, sol::optional charge) { + object.ptr().getCellRef().setEnchantmentCharge(charge.value_or(-1)); + }; item["isRestocking"] = [](const Object& object) -> bool { return object.ptr().getCellRef().getCount(false) < 0; }; diff --git a/files/lua_api/openmw/types.lua b/files/lua_api/openmw/types.lua index 4eb8459a6b..44dd578e41 100644 --- a/files/lua_api/openmw/types.lua +++ b/files/lua_api/openmw/types.lua @@ -652,7 +652,7 @@ -- Get this item's current enchantment charge. -- @function [parent=#Item] getEnchantmentCharge -- @param openmw.core#GameObject item --- @return #number The charge remaining. -1 if the enchantment has never been used, implying the charge is full. Unenchanted items will always return a value of -1. +-- @return #number The charge remaining. `nil` if the enchantment has never been used, implying the charge is full. Unenchanted items will always return a value of `nil`. --- -- Checks if the item restocks. @@ -665,7 +665,7 @@ -- Set this item's enchantment charge. -- @function [parent=#Item] setEnchantmentCharge -- @param openmw.core#GameObject item --- @param #number charge +-- @param #number charge Can be `nil` to reset the unused state / full --- -- Whether the object is supposed to be carriable. It is true for all items except