View Single Post
11-02-10, 02:46 PM   #28
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
Yey, home to a real comp!

Ok, back to business. When you pass slash arguments, it gets passed as one string. In your current code, if someone were to "/gmove PlayerFrame UIParent" (assuming "parvar" is supposed to indicate the parent, not clear from the snippet to me) it would get passed to the function like this (inserted as literal terms):

lua Code:
  1. SlashCmdList["GMOVE_SHORTHAND"] = function("PlayerFrame UIParent", nil )
  2.     addon:AddMoveableFrame("PlayerFrame UIParent", nil)
  3. end
  4.  
  5. SLASH_GMOVE_SHORTHAND1 = "/gmove"

The 2nd argument is passing nil to the slash handler of the API and breaking stuff (as you know). To fix this, you need separate your arguments before you pass them:

lua Code:
  1. SlashCmdList["GMOVE_SHORTHAND"] = function(cmd)
  2.         local name, parvar = strsplit(" ", cmd)  -- split your cmd string into segments with " " indicating the break points
  3.         -- alternately, you can use "function(...) local name, parvar = ..." and return the same results
  4.     addon:AddMoveableFrame(name, parvar)
  5. end
  6.  
  7. SLASH_GMOVE_SHORTHAND1 = "/gmove"

That should get that part working for you.

On a side note, you may want to explore what I like to call "universal" slash/event handlers to make your coding a bit less tedious and confusing along the way. Examples:

Slash Handler:
lua Code:
  1. SlashCmdList["myAddon1"] = function(...)
  2.     local cmd, arg1 = ...
  3.     if myAddon[cmd] then
  4.         return myAddon[cmd](arg1)
  5.     end
  6.     end

In this case, you can add slash commands without having to add a bunch of "if-then-else" which is just a pain. Just add the function myAddon:<cmd> and off you go.

Event Handler (maybe a bit easier to understand, was for me when learning it):
lua Code:
  1. myAddon = myAddon or CreateFrame("FRAME")
  2. myAddon:RegisterEvent("PLAYER_ENTERING_WORLD")
  3. myAddon:RegisterEvent("PLAYER_REGEN_ENABLED")
  4. myAddon:SetScript("OnEvent", function(self, event, ...)
  5.     if myAddon[event] then
  6.         return myAddon[event](self, event, ...)
  7.     end
  8.     end)
  9.  
  10. function myAddon:PLAYER_ENTERING_WORLD(self, event, ...)
  11.     -- do stuff
  12. end
  13.  
  14. function myAddon:PLAYER_REGEN_ENABLED(self, event, ...)
  15.     -- do stuff
  16. end

Same concept. Instead of adding a bunch of new "if-then-else" event comparisons, it simply goes to function if it exists. If not, it ignores the event. Adding new event watchers is a simple addition of registering the event and adding the new function. If something ends up being removed just delete the function without worrying about possibly having to change "elseif" statements. Just something I thought I'd throw out there for you since your project is becoming "a beast" to help clean/organize a bit easier.
  Reply With Quote