View Single Post
04/10/14, 11:18 PM   #14
tiomun
 
tiomun's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 25
Post

Thought I'd share my slash function now that I have it working.

When the slash command is entered this checks for a trailing command. The value of the command is then converted to the arrays key name that defines the message to be displayed, It also generates a function name which will be executed if a function by that name exists.

To avoid picking up a normal function in the addon, these dynamically triggered functions use a unique nomenclature. RVD_ instead of RV_ in this example.

This is just a snipet of the slash command code. not the complete addon script. there are earlier failsafes to prevent the execution of this affecting other addons

Lua Code:
  1. RVSLASH = {
  2.     default = RVNAME .." version " .. RVVERSION .. " \nType /rv help for a list of commands \nControls are in the settings interface!",
  3.     test = "Test sub command",
  4.     help = "List of commands",
  5. }
  6.  
  7. function RVD_TEST()
  8.     d("Function ok")
  9. end
  10.  
  11. function RV_SLASH(command)
  12.     local RVSUBCOM = {}
  13.     local rvSlashSearch = { string.match(command,"^(%S*)%s*(.-)$") }
  14.     for i,v in pairs(rvSlashSearch) do
  15.         if (v ~= nil and v ~= "") then
  16.             RVSUBCOM[i] = string.lower(v)
  17.         end
  18.     end
  19.     if RVSUBCOM[1] then
  20.         if RVSLASH[ RVSUBCOM[1] ] then
  21.             local RVFUNC = "RVD_" .. string.upper(RVSUBCOM[1])
  22.             if type ( _G[RVFUNC] ) == 'function' then
  23.                 _G[RVFUNC]()
  24.             end
  25.             d( RVSUBCOM[1] .. ": " .. RVSLASH[ RVSUBCOM[1] ] )
  26.         else
  27.             d(RVSLASH.default)
  28.         end
  29.     else
  30.         d(RVSLASH.default)
  31.     end
  32. end

It took a bit of playing with, but I am happy with the results. Thank you again for all the help

Last edited by tiomun : 04/10/14 at 11:26 PM.
  Reply With Quote