Quantcast Creating Secure Action Buttons + Keybinding them - WoWInterface
Thread Tools Display Modes
05-26-19, 06:49 PM   #1
CrittenxD
A Deviate Faerie Dragon
Join Date: Apr 2018
Posts: 13
Creating Secure Action Buttons + Keybinding them

Hi, I am a noob at scripting and I'm struggling to create a secure action button so I can use something along the lines of UseContainerItem or UseItemByName. I also would like to use this button via a Keybind in the Key Bindings options.

I got some code which I tried to use on my keybind resulting in a popup saying that I'm trying to use a protected function:


Code:
function UseItem()
local a
local b
local s = 0
    while s <= 4 do
    local c = 1
    local n = GetContainerNumSlots(s)
	    while c <= n and n > 0 do
	    local r = GetContainerItemLink(s, c)             						
		if r then 
			local x = string.find(r, "%[") +1;
                        local y = string.find(r, "%]") -1;
                        local z = string.sub(r, x, y)
                        if useitemtable[z] then a = nil UseContainerItem(s, c) c = n s = 5
		        elseif questitemtable[z] then
				if not a then a = s b = c
				end
			end
	        end
            c = c + 1
	    end
    s = s + 1
    end
if a then UseContainerItem(a, b)
end
end
I think this will just use the first item it finds in the inventory that is in the set called useitemtable, if there is none it will use questitemtable. I just don't really understand how the button frame works at all, I think I have to use "item" type and the ["bag", "slot"] attribute in the button frame, but I have no idea how to actually use the button with a keybind or how exactly the button frame would look. I know I could just do a macro with /use, but there are roughly 20-30 items on each list so that just won't fit.

Last edited by CrittenxD : 05-26-19 at 08:34 PM.
  Reply With Quote
05-26-19, 09:49 PM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,844
Using a SecureActionButtonTemplate, if you set type to "macro", you can set a string to macrotext that'll let you run it as a macro with unlimited length.
https://wow.gamepedia.com/SecureActionButtonTemplate

Example:
Lua Code:
  1. local MacroButton=CreateFrame("Button","MyMacroButton",nil,"SecureActionButtonTemplate");
  2. MacroButton:RegisterForClicks("AnyUp");--   Respond to all buttons
  3. MacroButton:SetAttribute("type","macro");-- Set type to "macro"
  4. MacroButton:SetAttribute("macrotext","/use Hearthstone\n/say Using my Hearthstone.");-- Set our macro text

This creates a button named MyMacroButton that can be activated by using the CLICK MyMacroButton binding command.
https://wow.gamepedia.com/Creating_key_bindings
https://wow.gamepedia.com/API_SetBinding
__________________
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
05-27-19, 09:14 AM   #3
CrittenxD
A Deviate Faerie Dragon
Join Date: Apr 2018
Posts: 13
Alright, so I shamelessly copied your frame and ran SetBindingClick("R", "MyMacroButton") and it works.
A few more questions did arise from this:
How efficient is a list of 60x /use item if only item 60 is in the inventory? Does the Blizzard code for /use item iterate through the whole inventory for each item?
Does "RegisterForClicks("AnyDown");" check for every single button press if it was R?


How do I bind the button through the key bindings menu? I tried a bunch of different ways to get the CLICK MyMacroButton in there but I am not familiar with xml at all.
Code:
<Binding name="USEITEM"  >(whatdoIwritehere?)</Binding>

One more thing I ran into is changing the loot key from shift to alt. In my bindings-cache.wtf there is a line 'modifiedclick ALT AUTOLOOTTOGGLE', but how do I do this via SetBinding or something similar?

Last edited by CrittenxD : 05-27-19 at 09:25 AM.
  Reply With Quote
05-27-19, 11:49 AM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,844
Originally Posted by CrittenxD View Post
How efficient is a list of 60x /use item if only item 60 is in the inventory? Does the Blizzard code for /use item iterate through the whole inventory for each item?
Whatever happens in C code is hidden to us. What we do know is /use is the same as /cast and tells the game to use an ability/item by name. On our end, it's the quickest and most efficient way to do this. If any iteration needs to happen, C code is magnitudes faster at doing it than Lua.



Originally Posted by CrittenxD View Post
Does "RegisterForClicks("AnyDown");" check for every single button press if it was R?
This tells the ActionButton which mouse buttons it should listen for. The keybind sends a click event to the ActionButton and it processes the request thinking the user clicked on it.



Originally Posted by CrittenxD View Post
How do I bind the button through the key bindings menu? I tried a bunch of different ways to get the CLICK MyMacroButton in there but I am not familiar with xml at all.
Code:
<Binding name="USEITEM"  >(whatdoIwritehere?)</Binding>
To get around the Protected Function issue, what we did is create a protected button which Blizzard supplied a template for us to use and configure to perform the action we want. To trigger the protected button we made, we have to use the built-in CLICK binding command. Unintuitively, you use this command as the name of the binding. You can't define any Lua code within the <Binding> tag anymore when doing this, it will be ignored.
Code:
<Binding name="CLICK MyActionButton"/>
If you wish to have the entry come up with a custom name, you need to write it to a specific global in your one of your Lua files.
Code:
_G["BINDING_NAME_CLICK MyActionButton"]="Run My Macro";
Note: This global has to be the same name as your binding (this is the CLICK command we set) prefixed by BINDING_NAME_.



Originally Posted by CrittenxD View Post
One more thing I ran into is changing the loot key from shift to alt. In my bindings-cache.wtf there is a line 'modifiedclick ALT AUTOLOOTTOGGLE', but how do I do this via SetBinding or something similar?
This is handled in Interface Options, not Keybindings.
__________________
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 05-27-19 at 11:52 AM.
  Reply With Quote
05-28-19, 11:39 AM   #5
CrittenxD
A Deviate Faerie Dragon
Join Date: Apr 2018
Posts: 13
Thanks again, everything is now working as desired. It was CLICK MyActionButton:LeftButton, btw.
I still got a few CVars which don't seem to pop up in either the character or account settings in the WTF folder, and I checked the ALT setting for auto loot again and it is definitely saved in bindings-cache and none of the two config-cache gets modified when changing that setting. It should be a CVar, but I think it might be some sort of weird exception and I have no idea how to modify it via the api. Probably gonna make a thread about this in a few days in case I don't make any progress on that front.
  Reply With Quote
05-28-19, 01:35 PM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,844
Originally Posted by CrittenxD View Post
I checked the ALT setting for auto loot again and it is definitely saved in bindings-cache and none of the two config-cache gets modified when changing that setting. It should be a CVar, but I think it might be some sort of weird exception and I have no idea how to modify it via the api.
Might want to take a look at this.
https://wow.gamepedia.com/API_SetModifiedClick
__________________
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
05-29-19, 10:03 AM   #7
CrittenxD
A Deviate Faerie Dragon
Join Date: Apr 2018
Posts: 13
Awesome. Gonna play for a few days before I'll look at the addon again. Fortunately I'm running out of things to fix though, thanks again.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Creating Secure Action Buttons + Keybinding them

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off