Quantcast WoWInterface - View Single Post - (Classic) Optimizing table search for auto dismount addon
View Single Post
06-26-19, 08:05 AM   #5
A Scalebane Royal Guard
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 425
Originally Posted by DashingSplash View Post
Was really surprised to see that

Lua Code:
  1. local prefix = SecureButton_GetModifierPrefix()
  2. local binding = GetBindingAction(prefix .. key)

was a thing. Although, I assume I'll get a better understanding of the API with some experience.
I didn't really know this either, but given a copy of the UI source code, there's a method to finding what you're looking for by going to the source of something you want to modify and looking at how it works. If you haven't already, get a copy of the source code and use an editor that allows you to search in the code for the things you're interested in. In this case, it seems likely that Blizzard would at some point implement a utility function that returns the current modifier chain, because it's useful in a lot of cases.

Originally Posted by DashingSplash View Post
The only thing that had to be included in your code was to allow companion, item, and macro functionality. See modified function below. Any input on that?
I would actually use a table for this, partly for code clarity, and also because it's easier to modify on a whim.
Lua Code:
  1. local dismountTypes = {
  2.     spell = true,
  3.     item = true,
  4.     companion = true,
  5.     macro = false,
  6. }
Lua Code:
  1. if HasAction(action) then
  2.     local actionType, spellID = GetActionInfo(action)
  4.     if dismountTypes[actionType] and not protectedSkills[spellID] then
  5.         Dismount()
  6.     elseif actionType == "macro" then
  7.         spellID, _ = GetMacroSpell(spellID)
  8.         if spellID ~= nil and not protectedSkills[spellID] then
  9.             Dismount()
  10.         end
  11.     end
  12. end
  13. return

Originally Posted by DashingSplash View Post
The only thing I am unsure about is how

Lua Code:
  1. button = stanceButtons[binding]
  2. if button and not button:GetChecked() then
  3.       Dismount()
  4. end

works. When and how is the button state set to checked?
I'll give you an example of how to track execution path in this case, which I don't think any tutorial brings up. If you look in Bindings.xml, you'll find that e.g. SHAPESHIFTBUTTON1 is calling StanceBar_Select(1). A quick search for this function gives you this result:
Lua Code:
  1. -- Searching 1364 files for "function StanceBar_Select" (case sensitive)
  2. -- UISourceCode\StanceBar.lua:
  3. function StanceBar_Select (id)
  4.     StanceBarFrame.lastSelected = id;
  5.     CastShapeshiftForm(id);
  6. end

Casting the shapeshift form generates an event that updates the stance bar, which results in the current stance active having its button set to checked.
Lua Code:
  1. function StanceBar_OnEvent(self, event)
  2.     if(event == "UPDATE_SHAPESHIFT_COOLDOWN") then
  3.         StanceBar_UpdateState();
  4.     end
  5. end
  7. function StanceBar_UpdateState ()
  8.     ...
  9.     if ( isActive ) then
  10.         StanceBarFrame.lastSelected = button:GetID();
  11.         button:SetChecked(true);
  12.     else
  13.         button:SetChecked(false);
  14.     end
  15.     ...
  16. end
  Reply With Quote