WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Creating Secure Action Buttons + Keybinding them (https://www.wowinterface.com/forums/showthread.php?t=57171)

CrittenxD 05-26-19 06:49 PM

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.

SDPhantom 05-26-19 09:49 PM

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

CrittenxD 05-27-19 09:14 AM

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?

SDPhantom 05-27-19 11:49 AM

Quote:

Originally Posted by CrittenxD (Post 332244)
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.



Quote:

Originally Posted by CrittenxD (Post 332244)
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.



Quote:

Originally Posted by CrittenxD (Post 332244)
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_.



Quote:

Originally Posted by CrittenxD (Post 332244)
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.

CrittenxD 05-28-19 11:39 AM

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.

SDPhantom 05-28-19 01:35 PM

Quote:

Originally Posted by CrittenxD (Post 332253)
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

CrittenxD 05-29-19 10:03 AM

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.


All times are GMT -6. The time now is 11:23 PM.

vBulletin © 2019, Jelsoft Enterprises Ltd
© 2004 - 2019 MMOUI