Skip to content

Commit

Permalink
Merge branch 'fix_use' into 'master'
Browse files Browse the repository at this point in the history
Fix regression added in  !3354: Set `force` to false when applying UseItem from inventory.

See merge request OpenMW/openmw!3422
  • Loading branch information
psi29a committed Sep 15, 2023
2 parents 59f8034 + fb8ccf5 commit ed022a6
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion apps/openmw/mwbase/luamanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace MWBase
virtual void objectTeleported(const MWWorld::Ptr& ptr) = 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;
virtual void useItem(const MWWorld::Ptr& object, const MWWorld::Ptr& actor) = 0;
virtual void useItem(const MWWorld::Ptr& object, const MWWorld::Ptr& actor, bool force) = 0;
virtual void exteriorCreated(MWWorld::CellStore& cell) = 0;
virtual void questUpdated(const ESM::RefId& questId, int stage) = 0;

Expand Down
2 changes: 1 addition & 1 deletion apps/openmw/mwgui/inventorywindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ namespace MWGui
}
}
else
MWBase::Environment::get().getLuaManager()->useItem(ptr, MWMechanics::getPlayer());
MWBase::Environment::get().getLuaManager()->useItem(ptr, MWMechanics::getPlayer(), false);

// If item is ingredient or potion don't stop drag and drop to simplify action of taking more than one 1
// item
Expand Down
2 changes: 1 addition & 1 deletion apps/openmw/mwlua/engineevents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace MWLua
MWWorld::Ptr actor = getPtr(event.mActor);
if (actor.isEmpty() || obj.isEmpty())
return;
mGlobalScripts.onUseItem(GObject(obj), GObject(actor));
mGlobalScripts.onUseItem(GObject(obj), GObject(actor), event.mForce);
}

void operator()(const OnConsume& event) const
Expand Down
1 change: 1 addition & 0 deletions apps/openmw/mwlua/engineevents.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace MWLua
{
ESM::RefNum mActor;
ESM::RefNum mObject;
bool mForce;
};
struct OnConsume
{
Expand Down
5 changes: 4 additions & 1 deletion apps/openmw/mwlua/globalscripts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ namespace MWLua
{
callEngineHandlers(mOnActivateHandlers, obj, actor);
}
void onUseItem(const GObject& obj, const GObject& actor) { callEngineHandlers(mOnUseItemHandlers, obj, actor); }
void onUseItem(const GObject& obj, const GObject& actor, bool force)
{
callEngineHandlers(mOnUseItemHandlers, obj, actor, force);
}
void onNewExterior(const GCell& cell) { callEngineHandlers(mOnNewExteriorHandlers, cell); }

private:
Expand Down
8 changes: 4 additions & 4 deletions apps/openmw/mwlua/luabindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,16 +330,16 @@ namespace MWLua
},
"_runStandardActivationAction");
};
api["_runStandardUseAction"] = [context](const GObject& object, const GObject& actor) {
api["_runStandardUseAction"] = [context](const GObject& object, const GObject& actor, bool force) {
context.mLuaManager->addAction(
[object, actor] {
[object, actor, force] {
const MWWorld::Ptr& actorPtr = actor.ptr();
const MWWorld::Ptr& objectPtr = object.ptr();
if (actorPtr == MWBase::Environment::get().getWorld()->getPlayerPtr())
MWBase::Environment::get().getWindowManager()->useItem(objectPtr, true);
MWBase::Environment::get().getWindowManager()->useItem(objectPtr, force);
else
{
std::unique_ptr<MWWorld::Action> action = objectPtr.getClass().use(objectPtr, true);
std::unique_ptr<MWWorld::Action> action = objectPtr.getClass().use(objectPtr, force);
action->execute(actorPtr, true);
}
},
Expand Down
4 changes: 2 additions & 2 deletions apps/openmw/mwlua/luamanagerimp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ namespace MWLua
{
mEngineEvents.addToQueue(EngineEvents::OnActivate{ getId(actor), getId(object) });
}
void useItem(const MWWorld::Ptr& object, const MWWorld::Ptr& actor) override
void useItem(const MWWorld::Ptr& object, const MWWorld::Ptr& actor, bool force) override
{
mEngineEvents.addToQueue(EngineEvents::OnUseItem{ getId(actor), getId(object) });
mEngineEvents.addToQueue(EngineEvents::OnUseItem{ getId(actor), getId(object), force });
}
void exteriorCreated(MWWorld::CellStore& cell) override
{
Expand Down
4 changes: 2 additions & 2 deletions docs/source/reference/lua-scripting/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ Examples:
**UseItem**

Any script can send global event ``UseItem`` with arguments ``object`` and ``actor``.
Any script can send global event ``UseItem`` with arguments ``object``, ``actor``, and optional boolean ``force``.
The actor will use (e.g. equip or consume) the object. The object should be in the actor's inventory.

Example:

.. code-block:: Lua
core.sendGlobalEvent('UseItem', {object = potion, actor = player})
core.sendGlobalEvent('UseItem', {object = potion, actor = player, force = true})
UI events
---------
Expand Down
15 changes: 8 additions & 7 deletions files/data/scripts/omw/usehandlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ local world = require('openmw.world')
local handlersPerObject = {}
local handlersPerType = {}

local function useItem(obj, actor)
local function useItem(obj, actor, force)
local options = { force = force or false }
local handlers = handlersPerObject[obj.id]
if handlers then
for i = #handlers, 1, -1 do
if handlers[i](obj, actor) == false then
if handlers[i](obj, actor, options) == false then
return -- skip other handlers
end
end
end
handlers = handlersPerType[obj.type]
if handlers then
for i = #handlers, 1, -1 do
if handlers[i](obj, actor) == false then
if handlers[i](obj, actor, options) == false then
return -- skip other handlers
end
end
end
world._runStandardUseAction(obj, actor)
world._runStandardUseAction(obj, actor, force)
end

return {
Expand Down Expand Up @@ -53,7 +54,7 @@ return {
version = 0,

--- Add new use action handler for a specific object.
-- If `handler(object, actor)` returns false, other handlers for
-- If `handler(object, actor, options)` returns false, other handlers for
-- the same object (including type handlers) will be skipped.
-- @function [parent=#ItemUsage] addHandlerForObject
-- @param openmw.core#GameObject obj The object.
Expand All @@ -68,7 +69,7 @@ return {
end,

--- Add new use action handler for a type of objects.
-- If `handler(object, actor)` returns false, other handlers for
-- If `handler(object, actor, options)` returns false, other handlers for
-- the same object (including type handlers) will be skipped.
-- @function [parent=#ItemUsage] addHandlerForType
-- @param #any type A type from the `openmw.types` package.
Expand All @@ -91,7 +92,7 @@ return {
if not data.actor or not types.Actor.objectIsInstance(data.actor) then
error('UseItem: invalid argument "actor"')
end
useItem(data.object, data.actor)
useItem(data.object, data.actor, data.force)
end
}
}

0 comments on commit ed022a6

Please sign in to comment.