This hooks calls to target an item and store the ItemID in an upvalue. I added support for Classic Era, which still has
UseContainerItem() in the global table instead of
C_Container. Feel free to remove this support if you don't plan on dealing with that client. There are other situations that'll overwrite the value, but when read immediately when the spellcast starts, it should be fine.
Lua Code:
local C_Container=C_Container or _G;-- C_Container doesn't exist in Classic Era (added to Wrath Classic in 3.4.1)
local TargetItemID;
-- Runs from ContainerFrameItemButton_OnClick() (also SecureActionButton "target-bag"/"target-slot" attributes for container items)
hooksecurefunc(C_Container,"UseContainerItem",function(bag,slot)
TargetItemID=C_Container.GetContainerItemID(bag,slot);
end);
-- Runs from SecureActionButton "target-slot" attribute for inventory items
hooksecurefunc("UseInventoryItem",function(slot)
TargetItemID=GetInventoryItemID("player",slot);
end);
-- Runs from SecureActionButton "target-item" attribute
hooksecurefunc("SpellTargetItem",function(item)
TargetItemID=GetItemInfoInstant(item);-- Accepts ItemID, ItemLink, or ItemName; Returns ItemID as first value
end);
PS: I have concerns over reading from the
arg2 global. I had thought Blizzard stopped using
arg# as globals a long time ago.