WoW:Creating a slash command: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
m (→Simple Example: Unfinished sentence) |
||
Line 1: | Line 1: | ||
This HOWTO teaches the basics of creating [[slash command|slash commands]] for your [[AddOn]]. | |||
== Simple Example == | == Simple Example == | ||
Line 9: | Line 9: | ||
end | end | ||
In | In the '''OnLoad function''' of your script, ''or'' in the .lua '''any place after the function is defined''', put in the following: | ||
SLASH_MYSCRIPT1 = "/myscript"; | SLASH_MYSCRIPT1 = "/myscript"; | ||
Line 15: | Line 15: | ||
SlashCmdList["MYSCRIPT"] = MyScript_Command; | SlashCmdList["MYSCRIPT"] = MyScript_Command; | ||
'''Note''': The slash command(s) must be defined in an OnLoad Function or after the function has been defined. If the slash command is defined when the function it "points to" (which is <tt>MyScript_Command</tt> in the above script) has not been defined yet, the slash command '''will not work'''. | |||
== Further Examples == | |||
OnLoad Function: | '''OnLoad Function:''' | ||
function | function AddonName_OnLoad() | ||
SlashCmdList["ADDONNAME"] = FunctionToCall; | SlashCmdList["ADDONNAME"] = FunctionToCall; | ||
SLASH_ADDONNAME1 = "/slash1"; | SLASH_ADDONNAME1 = "/slash1"; | ||
SLASH_ADDONNAME2 = "/slash2"; | SLASH_ADDONNAME2 = "/slash2"; | ||
end | end | ||
function AddonName_SlashCmdHandler() | |||
-- do stuff | |||
end | |||
And in your XML file: | |||
<OnLoad> | |||
AddonName_OnLoad(); | |||
</OnLoad> | |||
The above example will work as long as <tt>AddonName_OnLoad</tt> is called. | |||
function | '''After the function has been defined:''' | ||
function MySlashCmdFunction() | |||
-- Some stuff here | -- Some stuff here | ||
end | end | ||
SlashCmdList["ADDONNAME"] = | SlashCmdList["ADDONNAME"] = MySlashCmdFunction; | ||
SLASH_ADDONNAME1 = "/slash1"; | SLASH_ADDONNAME1 = "/slash1"; | ||
SLASH_ADDONNAME2 = "/slash2"; | SLASH_ADDONNAME2 = "/slash2"; | ||
Line 53: | Line 65: | ||
== Conventions == | == Conventions == | ||
* Command ID's are generally all caps, with underscores if necessary. | * Command ID's are generally all caps, with underscores if necessary. These can also contain lowercase characters. | ||
* Pick a command ID that's going to be unique to your addon, a common convention is ADDONNAME_COMMANDNAME, or if you just have one, then ADDONNAME (Where ADDONNAME is the name of your addon). | * Pick a command ID that's going to be unique to your addon, a common convention is ADDONNAME_COMMANDNAME, or if you just have one, then ADDONNAME (Where ADDONNAME is the name of your addon). | ||
* The actual '''SLASH_''<CommandId>''''<Number>''''' values should be localizable if you plan on your addon being translated, but the command ID will remain the same. | * The actual '''SLASH_''<CommandId>''''<Number>''''' values should be localizable if you plan on your addon being translated, but the command ID will remain the same. | ||
* Be prudent about how many aliases you make for your command, and try and pick something that's unlikely to collide with someone else's | * Be prudent about how many aliases you make for your command, and try and pick something that's unlikely to collide with someone else's (including Blizzard's!) | ||
[[Category:AddOns|Create a Slash Command]] | [[Category:AddOns|Create a Slash Command]] | ||
[[Category:HOWTOs|Create a Slash Command]] | [[Category:HOWTOs|Create a Slash Command]] |
Revision as of 14:32, 26 July 2008
This HOWTO teaches the basics of creating slash commands for your AddOn.
Simple Example
Suppose you have:
function MyScript_Command(cmd) DEFAULT_CHAT_FRAME:AddMessage("Command: " .. cmd); end
In the OnLoad function of your script, or in the .lua any place after the function is defined, put in the following:
SLASH_MYSCRIPT1 = "/myscript"; SLASH_MYSCRIPT2 = "/mys"; -- A shortcut or alias SlashCmdList["MYSCRIPT"] = MyScript_Command;
Note: The slash command(s) must be defined in an OnLoad Function or after the function has been defined. If the slash command is defined when the function it "points to" (which is MyScript_Command in the above script) has not been defined yet, the slash command will not work.
Further Examples
OnLoad Function:
function AddonName_OnLoad() SlashCmdList["ADDONNAME"] = FunctionToCall; SLASH_ADDONNAME1 = "/slash1"; SLASH_ADDONNAME2 = "/slash2"; end function AddonName_SlashCmdHandler() -- do stuff end
And in your XML file:
<OnLoad> AddonName_OnLoad(); </OnLoad>
The above example will work as long as AddonName_OnLoad is called.
After the function has been defined:
function MySlashCmdFunction() -- Some stuff here end SlashCmdList["ADDONNAME"] = MySlashCmdFunction; SLASH_ADDONNAME1 = "/slash1"; SLASH_ADDONNAME2 = "/slash2";
The Details
SLASH_<CommandId><num>
SlashCmdList["<CommandId>"] = <CodeToExecute>
Slash command parsing is done by the chat frame, and it works as follows.
- The key of each entry is the <CommandId>. It should be the unique name of your Addon.
- The parser then examines SLASH_<CommandId>1, SLASH_<CommandId>2, ... and so on, until it either finds a match, or gets a nil value (undefined) on the next SLASH_<CommandId>'<Number>.
- If there was a hit, then the function value of SlashCmdList["<CommandId>"] is invoked, with the rest of the command line as its single parameter
- Special 'built in' slash commands for chat are handled first.
- The chat handler then iterates over the contents of the global table SlashCmdList
- Otherwise, the next command is searched until a match is found or the list is exhausted.
Conventions
- Command ID's are generally all caps, with underscores if necessary. These can also contain lowercase characters.
- Pick a command ID that's going to be unique to your addon, a common convention is ADDONNAME_COMMANDNAME, or if you just have one, then ADDONNAME (Where ADDONNAME is the name of your addon).
- The actual SLASH_<CommandId>'<Number> values should be localizable if you plan on your addon being translated, but the command ID will remain the same.
- Be prudent about how many aliases you make for your command, and try and pick something that's unlikely to collide with someone else's (including Blizzard's!)