WoW:Creating a slash command: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
m (→Simple Example) |
||
Line 7: | Line 7: | ||
end | end | ||
In your OnLoad function of your script, put in the following: | In your OnLoad function of your script, or in the .lua anyplace after the function is defined, put in the following: | ||
SLASH_MYSCRIPT1 = "/myscript"; | SLASH_MYSCRIPT1 = "/myscript"; |
Revision as of 17:32, 8 September 2006
Simple Example
Suppose you have:
function MyScript_Command(cmd) DEFAULT_CHAT_FRAME:AddMessage("Command: " .. cmd); end
In your OnLoad function of your script, or in the .lua anyplace after the function is defined, put in the following:
SLASH_MYSCRIPT1 = "/myscript"; SLASH_MYSCRIPT2 = "/mys"; -- A shortcut or alias SlashCmdList["MYSCRIPT"] = MyScript_Command;
The Details
Slash command parsing is done by the chat frame, and it works as follows.
- Special 'built in' slash commands for chat are handled first.
- The chat handler then iterates over the contents of the global table SlashCmdList
- The key of each entry is the CommandId. 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
- 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.
- 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.