WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Create Slash Commands (https://www.wowinterface.com/forums/showthread.php?t=55625)

Eragon_Edward 08-05-17 07:20 AM

Create Slash Commands
 
Hi!, im new in Addon's development, and I'm having problems with the next Slash commands that I want to create, my idea is simple I just want to shortcuts, some personalize phrases , this is the code:
And btw, i don't know exactly what means the "msg" or "(txt)" does with "function " , and how does it affect the script

SLASH_PHRASE = "/ph"
SLASH_SPHRASE = "/sph"

SlashCmdList ["PHRASE"] = function (msg)
SendChatMessage ("Go to RBG!", GUILD")

SlashCmdList["SPHRASE"] = function (msg)
SendChatMessage("Do you want come with us to RBG?", "SAY")
end

Ok, the First /ph, works for the guild, but "SAY" is not working, and I have no clue.

Q1: if I write "function ()" what it does?
Q2: Could I use {} to just write 1 code line "SlashCmdList?

-- sry for my English --

Mortimerlol 08-05-17 08:20 AM

Lua Code:
  1. --Try it.
  2. SLASH_PHRASE = "/ph";
  3. SLASH_SPHRASE = "/sph";
  4.  
  5. local function PHRASEfunc(msg)
  6.     SendChatMessage("Go to RBG!", "GUILD")
  7. end
  8.  
  9. local function SPHRASEfunc(msg)
  10.     SendChatMessage("Do you want come with us to RBG?", "SAY")
  11. end
  12.  
  13. SlashCmdList["PHRASE"] = PHRASEfunc;
  14. SlashCmdList["SPHRASE"] = SPHRASEfunc;

Eragon_Edward 08-05-17 08:43 AM

(Thx for answering )

Now both stoped working :S "/ph" and "/sph", but I realized something (while waiting for an answer)
I need to write first "/ph" so "/sph" could work

if I use "/sph" before "/ph" , it doesn't work :S, any idea?

Eragon_Edward 08-05-17 10:04 AM

Edit
 
I could make it work , adding the 1 , at the end of each Slash_

SLASH_PHRASE1
So It works! now :)
now question 1,
what is the difference between

SlashCmdList = function(msg)
and
local function = <name>(msg)
?
and what exactly does (msg) or (txt) in that line?

question 2, is there function or array that I could use to get in one place all "local function " and "SendChatMessage", like a table or array"?

Fizzlemizz 08-05-17 11:10 AM

Code:

SLASH_PHRASE1 = "/ph";
SLASH_PHRASE2 = "/sph";
SlashCmdList["PHRASE"] = function(msg)
...
end

if you type "/ph something" or "/sph something", "something" will be passed as a string to the msg/txt parameter of your slash comand function.

local anything is local to that module/code chunk and can't be "seen" outside of it. I'm not sure what you mean by SendChatMessage but if you mrean calls to the SendChatMessage function in code then no.

Eragon_Edward 08-05-17 11:16 AM

Oh thx!,
now I would like to make an index, like Blizzard did with "/help" command but I having issues how to make

print_array() into variable. the code is kind of

tmp = {"alpha", "beta", "gamma"}
function print_array(tbl)
for i = 1, #tbl do
print(i, tbl[i])
end end
print_array(tmp)

and that displays
1, alpha
2, beta
3, gamma


I would like to show my /commands
After I write
/hcm -- Help command
could display the next kind of table.
1, /ph , "you say , hello guild mates!"
2, /sph " an other phrase "


but until now I have no clue how to make it just the so simple ones, with foo= 14, print (foo) :S

Fizzlemizz 08-05-17 11:59 AM

I'm not sure if that was an actual question but if so, something like:
Lua Code:
  1. local msgList = {
  2.     { "GUILD", "Message 1" },
  3.     { "YELL", "Message 2" },
  4.     { "PARTY", "Message 3" },
  5. }
  6. local function PrintList()
  7.     print("Help commands = /ph or /sph\nOption = Send - Message:")
  8.     for i=1, #msgList do
  9.         print(i, "=", msgList[i][1], "-", msgList[i][2])
  10.     end
  11. end
  12.  
  13. SLASH_PHRASE1 = "/ph";
  14. SLASH_PHRASE2 = "/sph";
  15. SlashCmdList["PHRASE"] = function(msg)
  16.     if msg == "" then
  17.         PrintList()
  18.     else
  19.         local cmd = tonumber(msg)
  20.         if msgList[cmd] then
  21.             SendChatMessage(msgList[cmd][2], msgList[cmd][1])
  22.         else
  23.             print("Not a valid option")
  24.         end
  25.     end
  26. end

Eragon_Edward 08-05-17 12:35 PM

Quote:

Originally Posted by Fizzlemizz (Post 324514)
I'm not sure if that was an actual question but if so, something like:
Lua Code:
  1. local msgList = {
  2.     { "GUILD", "Message 1" },
  3.     { "YELL", "Message 2" },
  4.     { "PARTY", "Message 3" },
  5. }
  6. local function PrintList()
  7.     print("Help commands = /ph or /sph\nOption = Send - Message:")
  8.     for i=1, #msgList do
  9.         print(i, "=", msgList[i][1], "-", msgList[i][2])
  10.     end
  11. end
  12.  
  13. SLASH_PHRASE1 = "/ph";
  14. SLASH_PHRASE2 = "/sph";
  15. SlashCmdList["PHRASE"] = function(msg)
  16.     if msg == "" then
  17.         PrintList()
  18. ----------------------------------------------------------------------------------------------------
  19.     else
  20.         local cmd = tonumber(msg)
  21.         if msgList[cmd] then
  22.             SendChatMessage(msgList[cmd][2], msgList[cmd][1])
  23.         else
  24.             print("Not a valid option")
  25.         end
  26.     end
  27. end
  28. ----------------------------------------------------------------------------------------------------

Ok now I got it! (the first part) :), thank you :banana:, now i could not understand all the rest of the code (the one between the lines) exactly how it works down there? :confused: ,if you are so kind to explain it for a really dummy :P

Fizzlemizz 08-05-17 12:43 PM

The part between the lines, if you type /ph (or /sph) without anything after it, it will print the list of options.
/ph
Quote:

Help commands = /ph or /sph
Option = Send - Message:
1 = GUILD - Message 1
2 = YELL - Message 2
3 = PARTY - Message 3
if you type say /ph 1, it will use the options in the first msgList entry to create a SendChatMessage():
/ph 1
would be like doing
Quote:

/g Message 1
/ph 2
would be like doing
Quote:

/y Message 2
The entries in the msgList are in two parts
{ "chattype", "message" }
as per SendChatMessage

If you're only sending one chattype then you don't need that part.


All times are GMT -6. The time now is 09:26 AM.

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