diff --git a/apps/openmw/mwlua/types/actor.cpp b/apps/openmw/mwlua/types/actor.cpp index 321aaa20e6..335ba517d5 100644 --- a/apps/openmw/mwlua/types/actor.cpp +++ b/apps/openmw/mwlua/types/actor.cpp @@ -88,7 +88,7 @@ namespace MWLua [](const LObject& o) { return Inventory{o}; }, [](const GObject& o) { return Inventory{o}; } ); - actor["equipment"] = [context](const Object& o) + auto getAllEquipment = [context](const Object& o) { const MWWorld::Ptr& ptr = o.ptr(); sol::table equipment(context.mLua->sol(), sol::create); @@ -106,6 +106,20 @@ namespace MWLua } return equipment; }; + auto getEquipmentFromSlot = [context](const Object& o, int slot) -> sol::object + { + const MWWorld::Ptr& ptr = o.ptr(); + sol::table equipment(context.mLua->sol(), sol::create); + if (!ptr.getClass().hasInventoryStore(ptr)) + return sol::nil; + MWWorld::InventoryStore& store = ptr.getClass().getInventoryStore(ptr); + auto it = store.getSlot(slot); + if (it == store.end()) + return sol::nil; + context.mWorldView->getObjectRegistry()->registerPtr(*it); + return o.getObject(context.mLua->sol(), getId(*it)); + }; + actor["equipment"] = sol::overload(getAllEquipment, getEquipmentFromSlot); actor["hasEquipped"] = [](const Object& o, const Object& item) { const MWWorld::Ptr& ptr = o.ptr(); diff --git a/files/lua_api/openmw/types.lua b/files/lua_api/openmw/types.lua index b9b5df44d6..d62d92169d 100644 --- a/files/lua_api/openmw/types.lua +++ b/files/lua_api/openmw/types.lua @@ -108,12 +108,14 @@ --- -- Get equipment. --- Returns a table `slot` -> @{openmw.core#GameObject} of currently equipped items. +-- Has two overloads: +-- 1) With single argument: returns a table `slot` -> @{openmw.core#GameObject} of currently equipped items. -- See @{#EQUIPMENT_SLOT}. Returns empty table if the actor doesn't have --- equipment slots. +-- equipment slots. +-- 2) With two arguments: returns an item equipped to the given slot. -- @function [parent=#Actor] equipment -- @param openmw.core#GameObject actor --- @return #map<#number,openmw.core#GameObject> +-- @param #number slot (optional argument) --- -- Set equipment.