Thread Tools Display Modes
11-08-12, 10:26 AM   #1
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
Using a slash command in an addon

How would i go about using a blizzard slash command in an addon?

Specifically this slash command /petautocastoff

I would like to be able to use petautocastoff to disable my pet from automatically using an ability
  Reply With Quote
11-08-12, 10:37 AM   #2
efindel
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 8
In general, you don't. Instead, you look for the API function that does whatever the slash command does, and call that. Alternatively, you can create a button that runs a macro that runs the slash command you want. There's no direct way to simply run a slash command from Lua, by design.

In this case, the API call is DisableSpellAutocast() - which is a Blizzard protected function, so you can't call it. This should immediately tell you that you're heading into areas where Blizzard does not want addons to go. You can use SecureButtonTemplate to create a button that runs a macro that has that command. You could then potentially use the /click command in another macro to click that button, if you wanted, but that's the extent of the automation you can do with this.
  Reply With Quote
11-08-12, 10:42 AM   #3
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
I was afraid of that =\

I guess I'll have to change it to an alert, thank you for the info =)
  Reply With Quote
11-08-12, 10:59 AM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
The command runs DisableSpellAutocast(), which may be a protected function.



Note this would activate a chat command, but it's horribly inefficient compared to finding the API used and calling that directly.
lua Code:
  1. local editbox=ChatEdit_ChooseBoxForSend(DEFAULT_CHAT_FRAME);--  Get an editbox
  2. ChatEdit_ActivateChat(editbox);--   Show the editbox
  3. editbox:SetText("/petautocastoff Growl");-- Command goes here
  4. ChatEdit_OnEnterPressed(editbox);-- Process command and hide (runs ChatEdit_SendText() and ChatEdit_DeactivateChat() respectively)

This code also follows the same rules that protected functions cannot be executed.
__________________
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 : 11-08-12 at 11:11 AM.
  Reply With Quote
11-09-12, 12:14 PM   #5
efindel
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 8
What SDPhantom gave there will work for slash commands that do non-protected functions... but in general, there's no point to doing it that way, since you can either call the API that does the thing directly, or, if it's a protected function, it won't work doing it that way either. According to www.wowprogramming.com, the one you want is a protected function, so it shouldn't work. You could give it a try, though, just in case the API reference there is wrong.
  Reply With Quote
11-10-12, 02:15 AM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
The documentation is not wrong. Turning off autocast is a protected action, and cannot be done without a hardware event and a secure code path. In fact, the protection is so complete that even if an addon puts the slash command in the chat box and then you physically press Enter to submit the command, it will not work, because the command's entry was automated.

However, if you are more specific about when/why/how you want to turn off autocast, we can either tell you how it can be done or, if it cannot be done, tell you what the closest things you can do are.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
11-10-12, 11:49 AM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by Phanx View Post
In fact, the protection is so complete that even if an addon puts the slash command in the chat box and then you physically press Enter to submit the command, it will not work, because the command's entry was automated.
This is the same with any protected function in which having an addon set the text in an EditBox sets the internal value as tainted. The EditBox itself isn't tainted, but the value from EditBox:GetText() will be.

This very mechanic caused me to discontinue the autocomplete function of SlashHelp as it was tainting the EditBox text in this very manner. It wasn't a total loss since the Default UI does nearly the same thing if you hit Tab while typing in a command.
__________________
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

WoWInterface » Developer Discussions » Lua/XML Help » Using a slash command in an addon


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