WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Function to put Spells on Actionbar? (https://www.wowinterface.com/forums/showthread.php?t=49259)

chumii 05-05-14 06:00 AM

Function to put Spells on Actionbar?
 
Hi,

I am trying some simple things to learn addon development, and came across with sth i would like to achieve. It doesn't really make sense to do it, but i was wondering if it is possible, cause i couldn't find sth about it on wowprogramming.

So, I would like to create an addon, that has a button, and if you click it, it will put specific spells in an actionbar slot. maybe another button to remove all spells from all bars first..

is there some API function I didnt see, or some other ways to achieve that? Please, I dont want any complete code, I just need a hint if its possible, and if yes, push me into the right direction.. i want to learn, not copy / paste ;)

thanks in advance
chumii

Lombra 05-05-14 03:39 PM

Sure, PickupSpell and PickupAction (don't let the name fool you; it's not limited to picking up) should be of use. As long as you're not trying to do it in combat it's easy enough. If you need to do it in combat I'm not sure whether that's possible, let alone how.

chumii 05-06-14 01:34 AM

no, not in combat.. will have a look at those, thank you!

chumii 05-06-14 04:38 AM

hmm.. dont want to start a new thread but got another question, kind of related:

if i have a lua table with some spells, like
Code:

spells_arms = {
    49203, -- Hungering Cold
    6770, -- Sap
  }

how can i get the first, and then inside while the next spell from that list? so, in the code below, how assign "n" with spells form the list?

Code:

function putSpellsOnBars()
        local i = 1
        local n = SPELLS_FROM_LIST
        while i  <= 2 do
                PickupSpell(n);
                PlaceAction(i);
                ClearCursor();
                i = i + 1
                n = NEXT_SPELL_FROM_LIST
        end
end

found out myself.. easier using a for loop..

Code:

function putSpellsOnBars()
        local i = 1
        local spell_list = {
    12294,
    100,
    78,
          }
        for spellcount = 1,3 do
                PickupSpell(spell_list[spellcount]);
                PlaceAction(i);
                ClearCursor();
                i = i + 1
        end
end


Lombra 05-06-14 06:01 AM

Yeah, that'll be the best way. You can use the length operator # instead of manually entering the table length. If you're gonna place the spells sequentially you could just use the loop counter spellcount for PlaceAction instead of keeping a separate i. Also, for future reference you'll want to move the table out of the function if possible, so it's only created once instead of each time the function is called.
Code:

local spell_list = {
    12294,
    100,
    78,
}

function putSpellsOnBars()
        local i = 1
        for spellcount = 1,#spell_list do
                PickupSpell(spell_list[spellcount]);
                PlaceAction(i);
                ClearCursor();
                i = i + 1
        end
end


chumii 05-06-14 10:36 AM

Thanks man, that #spell_list saves a lot of work!

i got it running now as I want, and made a simple "UI" for it. But now I got the problem, that its showing up everytime I login to wow. How can I make it only show up via slashcommand? I know how to register a slashcommand, but what would be the function to call? MainFrame:Show(); for example? cause thats not working for me.. I added MainFrame:Hide(); to the onLoad but thats not doing it..

Code:

function MainFrame_OnLoad()
        --MainFrame:Hide();
        SlashCmdList["MYADDON"] = MainFrame:Show();
        SLASH_MYADDON1= "/myaddon";
end


Duugu 05-06-14 01:44 PM

The SlashCmdList[] value should point to a function that handles the slash command. Basically like this:

Lua Code:
  1. function myAddon_OnLoad()
  2.     SLASH_myaddon1 = "/myaddon"
  3.     SlashCmdList["myaddon"] = myAddon_SlashHandler
  4. end
  5.  
  6. function myAddon_SlashHandler(msg)
  7.     local fields = split(msg, " ")
  8.  
  9.     if #fields == 0 or (fields[1] == "?" or fields[1] == "help") then
  10.         print("The available slash commands for myaddon are: <show> and <hide>")
  11.     elseif fields[1] == "show" then
  12.         myAddonMainFrame:Show()
  13.     elseif fields[1] == "hide" then
  14.         myAddonMainFrame:Hide()
  15.     end
  16. end

Phanx 05-06-14 07:03 PM

Duugu, what you've written won't work, since (a) there is no global "split" alias for the string.split function, only "strsplit", (b) string.split requires the delimiter as the first argument, not the string to be split, and (c) string.split returns a list, not a table. Even if you enclosed it in curly brackets to create a table, which I suppose was your intention, that's rather inefficient anyway. Also, there's no need to delay creating your slash command; you can just do it in the main chunk, outside of any other function. Here's the usual way to handle commands with arguments that doesn't create new tables all the time:

Lua Code:
  1. SLASH_MYADDON1 = "/myaddon"
  2. SlashCmdList["MYADDON"] = function(msg)
  3.     local command, args = strsplit(" ", strtrim(msg), 2) -- trim leading/trailing spaces before splitting
  4.  
  5.     if command == "show" then
  6.         MyAddonMainFrame:Show()
  7.     elseif command == "hide" then
  8.         MyAddonMainFrame:Hide()
  9.     elseif command == "toggle" then
  10.         MyAddonMainFrame:SetShown(not MyAddonMainFrame:IsShown())
  11.     else
  12.         print("|cffffc000MyAddon:|r Use /myaddon with the following commands:")
  13.         print("   - show - shows the main frame")
  14.         print("   - hide - hides the main frame")
  15.         print("   - toggle - toggles the main frame")
  16.     end
  17. end

Other benefits of this approach include:

- The user gets the help list no matter what they type, as long as it's not a valid command, instead of limiting them to a few pre-set help commands like "help" and "?". If you limit them, then typing an invalid command (even making a typo, like "hied" instead of "hide") will result in nothing at all happening, which is confusing.

- Each command can handle its arguments however it likes, rather than being forced to handle them one word at a time and use more string functions to join them back up if that's not what it wants.

Phanx 05-06-14 07:08 PM

Also, if you're going to make everything a global (though really, you shouldn't make anything global that doesn't need to be global) you should make sure their names are unique and clearly identify which addon they belong to. "spell_list" and "putSpellsOnBars" don't meet either of those criteria. If another addon author makes globals with the same names -- probably accidentally, as globals are generally avoided -- then one addon will break the other, or both addons will break. And if Blizzard makes a global with the same name, your addon could potentially break the entire interface; this has happened before, when Blizzard leaked a global "_" and the hundreds of addons who authors were lazily leaking globals with the same name caused the entire UI to become tainted and stop working.

chumii 05-07-14 01:59 AM

You guys are awesome, thank you so far! Editing names is a good point, tho I wont ever make this public cause its only for me to learn, but I get your point :)

Macros works now, but the window still shows up on login.. If I type /run CutieRestMainFrame:Hide() it will hide, but I dont want to type that in everytime I login..

Lua Code:
  1. --MainFrame
  2. function CutieRestMainFrame_OnLoad()
  3.     CutieRestMainFrame:Hide();
  4. end

the frame.xml file is edited too, to match the names ofc

edit: got it, forgot to call the OnLoad() function inside my frame.xml... but a last little thing i wondered.. is it possible to grey out buttons? I mean like, making them inactive and not clickable?

Phanx 05-07-14 04:20 AM

Quote:

Originally Posted by chumii (Post 292586)
is it possible to grey out buttons? I mean like, making them inactive and not clickable?

NameOfTheButton:Disable()
NameOfTheButton:Enable()
NameOfTheButton:SetEnabled(nil/false to disable, true or anything else to enable)

Also:
NameOfTheButton:SetMotionScriptsWhenDisabled(true|false)
...if you want to keep the OnEnter/OnLeave scripts enabled (eg. to show a tooltip) while the button is disabled.

Duugu 05-07-14 08:06 AM

Quote:

Originally Posted by Phanx (Post 292572)
Duugu, what you've written won't work, since (a) there is no global "split" alias for the string.split function, only "strsplit",

Or, sorry for that. I basically copied that out of one of my addons, and split was a local utility function. :)

chumii 05-07-14 01:23 PM

Quote:

Originally Posted by Phanx (Post 292587)
NameOfTheButton:Disable()
NameOfTheButton:Enable()
NameOfTheButton:SetEnabled(nil/false to disable, true or anything else to enable)

Also:
NameOfTheButton:SetMotionScriptsWhenDisabled(true|false)
...if you want to keep the OnEnter/OnLeave scripts enabled (eg. to show a tooltip) while the button is disabled.

Thank you very much!


All times are GMT -6. The time now is 05:37 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI