Looking for help to create UseContainerItem secure button

Hello everyone. Just as the title says I am looking for help to create and inventory button to use specific container slot, bag 1 slot 1 for example. I want it to act just like a normal inventory right click: use or move on Right Button. Here is a piece of my code:

local icon, itemCount, locked, quality, readable, lootable, itemLink, isFiltered, noValue, itemID = GetContainerItemInfo(1, 1) 
local btn = CreateFrame("Button", "1_01", UIParent,"SecureActionButtonTemplate");
btn:SetPoint("CENTER",UIParent,"CENTER")
btn:SetSize(40,40)
local texture = btn:CreateTexture("1_01_Texture", "BACKGROUND")
texture:SetTexture(icon)
texture:SetAllPoints()

btn:RegisterForClicks("AnyUp")
btn:SetAttribute("type2", "item")
btn:SetAttribute("item", 1,1)
		
btn:SetScript("OnClick", function(self, button)
	UseContainerItem(1,1)
	print(button.." "..1.." - "..1)
end)

It woks just fine with transfering items to bank, vendoring, etc, but I get a “AddOn tried to call the protected function” error when I try to use any kind of consumables, hearthstones. Please help me find an error, because I feel like I hit a wall.

You shouldn’t add OnClick function to secure button. You should only use attributes to determine the action that the buttons does. Some spells, including some that are on items are protected meaning that only Blizzard made addons can call them directly in code. So when you try to call UseContainerItem and the spell for use on that item is protected the addon breaks.

btn:SetAttribute("item", 1,1) is enough and pretty much should work just like UseContainerItem(1,1) inside OnClick hanler. So just remove that SetScript and see if it works. You might also wanna replace 1,1 with "1,1" becasue wowpedia says the format you use is deprecated.

2 Likes

Yeah, i just learned it hard way than I should just use following SetAttribute instead of setting any scripts. This pretty much solved all my issues.

frame:SetAttribute("type2", "item")
frame:SetAttribute("bag", bag)
frame:SetAttribute("slot", bagslot)

Thank you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.