diff --git a/apps/openmw/mwbase/luamanager.hpp b/apps/openmw/mwbase/luamanager.hpp index 6c1ce6a27b..c174dddce3 100644 --- a/apps/openmw/mwbase/luamanager.hpp +++ b/apps/openmw/mwbase/luamanager.hpp @@ -37,7 +37,7 @@ namespace MWBase virtual void deregisterObject(const MWWorld::Ptr& ptr) = 0; virtual void objectAddedToScene(const MWWorld::Ptr& ptr) = 0; virtual void objectRemovedFromScene(const MWWorld::Ptr& ptr) = 0; - virtual void appliedToObject(const MWWorld::Ptr& toPtr, std::string_view recordId, const MWWorld::Ptr& fromPtr) = 0; + virtual void itemConsumed(const MWWorld::Ptr& consumable, const MWWorld::Ptr& actor) = 0; virtual void objectActivated(const MWWorld::Ptr& object, const MWWorld::Ptr& actor) = 0; // TODO: notify LuaManager about other events // virtual void objectOnHit(const MWWorld::Ptr &ptr, float damage, bool ishealth, const MWWorld::Ptr &object, diff --git a/apps/openmw/mwclass/npc.cpp b/apps/openmw/mwclass/npc.cpp index d7a74ee7c1..74de6fb764 100644 --- a/apps/openmw/mwclass/npc.cpp +++ b/apps/openmw/mwclass/npc.cpp @@ -1105,7 +1105,7 @@ namespace MWClass MWBase::Environment::get().getWorld()->breakInvisibility(actor); MWMechanics::CastSpell cast(actor, actor); std::string recordId = consumable.getCellRef().getRefId(); - MWBase::Environment::get().getLuaManager()->appliedToObject(actor, recordId, actor); + MWBase::Environment::get().getLuaManager()->itemConsumed(consumable, actor); actor.getClass().getContainerStore(actor).remove(consumable, 1, actor); return cast.cast(recordId); } diff --git a/apps/openmw/mwlua/localscripts.cpp b/apps/openmw/mwlua/localscripts.cpp index 1224411453..cca31361ff 100644 --- a/apps/openmw/mwlua/localscripts.cpp +++ b/apps/openmw/mwlua/localscripts.cpp @@ -177,7 +177,7 @@ namespace MWLua else { static_assert(std::is_same_v); - callEngineHandlers(mOnConsumeHandlers, arg.mRecordId); + callEngineHandlers(mOnConsumeHandlers, arg.mConsumable); } }, event); } diff --git a/apps/openmw/mwlua/localscripts.hpp b/apps/openmw/mwlua/localscripts.hpp index 58cf421d4a..ff2e2bff25 100644 --- a/apps/openmw/mwlua/localscripts.hpp +++ b/apps/openmw/mwlua/localscripts.hpp @@ -62,7 +62,7 @@ namespace MWLua }; struct OnConsume { - std::string mRecordId; + LObject mConsumable; }; using EngineEvent = std::variant; diff --git a/apps/openmw/mwlua/luamanagerimp.cpp b/apps/openmw/mwlua/luamanagerimp.cpp index b71f5919c3..5854820361 100644 --- a/apps/openmw/mwlua/luamanagerimp.cpp +++ b/apps/openmw/mwlua/luamanagerimp.cpp @@ -374,9 +374,10 @@ namespace MWLua mWorldView.getObjectRegistry()->deregisterPtr(ptr); } - void LuaManager::appliedToObject(const MWWorld::Ptr& toPtr, std::string_view recordId, const MWWorld::Ptr& fromPtr) + void LuaManager::itemConsumed(const MWWorld::Ptr& consumable, const MWWorld::Ptr& actor) { - mLocalEngineEvents.push_back({getId(toPtr), LocalScripts::OnConsume{std::string(recordId)}}); + mWorldView.getObjectRegistry()->registerPtr(consumable); + mLocalEngineEvents.push_back({getId(actor), LocalScripts::OnConsume{LObject(getId(consumable), mWorldView.getObjectRegistry())}}); } void LuaManager::objectActivated(const MWWorld::Ptr& object, const MWWorld::Ptr& actor) diff --git a/apps/openmw/mwlua/luamanagerimp.hpp b/apps/openmw/mwlua/luamanagerimp.hpp index 41eb076783..0e94e37f24 100644 --- a/apps/openmw/mwlua/luamanagerimp.hpp +++ b/apps/openmw/mwlua/luamanagerimp.hpp @@ -51,7 +51,7 @@ namespace MWLua void registerObject(const MWWorld::Ptr& ptr) override; void deregisterObject(const MWWorld::Ptr& ptr) override; void inputEvent(const InputEvent& event) override { mInputEvents.push_back(event); } - void appliedToObject(const MWWorld::Ptr& toPtr, std::string_view recordId, const MWWorld::Ptr& fromPtr) override; + void itemConsumed(const MWWorld::Ptr& consumable, const MWWorld::Ptr& actor) override; void objectActivated(const MWWorld::Ptr& object, const MWWorld::Ptr& actor) override; MWBase::LuaManager::ActorControls* getActorControls(const MWWorld::Ptr&) const override; diff --git a/docs/source/reference/lua-scripting/engine_handlers.rst b/docs/source/reference/lua-scripting/engine_handlers.rst index ca05c9b8e1..a731ceef6f 100644 --- a/docs/source/reference/lua-scripting/engine_handlers.rst +++ b/docs/source/reference/lua-scripting/engine_handlers.rst @@ -66,8 +66,10 @@ Engine handler is a function defined by a script, that can be called by the engi | the item is placed to the actor's inventory, (2) count of | the original item is set to zero, (3) and only then onActivated is | called on the original item, so self.count is already zero. - * - onConsume(recordId) - - Called if `recordId` (e.g. a potion) is consumed. + * - onConsume(item) + - | Called on an actor when they consume an item (e.g. a potion). + | Similarly to onActivated, the item has already been removed + | from the actor's inventory, and the count was set to zero. **Only for local scripts attached to a player**