Thread: slash commands

  1. #1

    slash commands

    It's not about the syntax, more about what i can/can't do with them.

    The idea should be this
    Code:
    SLASH_COLD1 = "/lolcommand" 
    SlashCmdList.COLD = function (msg, editbox)
      Print("awesome lolsentence")
      -- want to set a varable here. should be something like cfg.variable = true. is it possible?
      ReloadUI() -- can i use this function?
    end
    I think code "should" be correct. Is it also logically correct? the cfg.variable should be in another file, which is imported at the beginning (so it should be visible.

  2. #2
    The Patient Ketho's Avatar
    Join Date
    Jan 2011
    Location
    The Netherlands
    Posts
    278
    Your code should be correct, except that the default Lua print function is with a lowercase p
    I think ReloadUI can be used in this case, since slash commands generally are triggered from hardware events/keypresses, which is required for this function

    and why not just check if cfg is already defined/imported?
    Code:
    SLASH_COLD1 = "/lolcommand" 
    SlashCmdList.COLD = function(msg, editbox)
        print("awesome lolsentence")
        if cfg then
            cfg.variable = true
        else
            print("cfg is not defined or visible")
        end
        --ReloadUI()
    end
    Last edited by Ketho; 2011-07-28 at 12:05 PM.

  3. #3
    If cfg is a global, there's no reason why it shouldn't work.
    If cfg is a local from another file, that obviously won't quite work - you can use the addon-wide table to pass it around without exposing it globally, though.
    Code:
    local addontab = (select(2,...))
    UI & AddOns expert | Interface & Macros moderator - My work

  4. #4
    cfg is a variable defined in the addon namespace, i use it as a container for the config file, and it's aldready defined.

    so, if i use
    Code:
    local addon, ns = ...
    local cfg = ns.cfg
    it should work.

    Another question: let's put i have cfg.variable = false in file 1, then with the slash command i want to set cfg.variable = true.
    Does it work? I have the suspect that reloading the ui after he keeps the cfg.variable in file 1, so negating the slash command effect. Need to try as soon i get home.

    Thanks for the answers

  5. #5
    Well, if you want to keep it through reloads/relogs, you'll have to make it a SavedVar, obviously.
    UI & AddOns expert | Interface & Macros moderator - My work

  6. #6
    And that's what i was seraching for never done one before, methods i must use?

    A link is just fine, learning is always good.

  7. #7
    ## SavedVariables: GlobalKey

    Code:
    if event == "ADDON_LOADED" and arg1 == "AddonFolderName" then
        GlobalKey = GlobalKey or {} -- defaults
    end
    Last edited by Treeston; 2011-07-28 at 02:57 PM.
    UI & AddOns expert | Interface & Macros moderator - My work

  8. #8
    Quote Originally Posted by Treeston View Post
    ## SavedVariables: GlobalKey

    Code:
    if event == "ADDON_LOADED" and arg1 == "AddonFolderName" then
        GlobalKey = GlobalKey or {} -- defaults
    end
    Thanks, gonna try this weekend.

    EDIT: if i use ##SavedVariablesperCharachter, i will have a separate copy of GlobalKey for every char, right?
    Last edited by Coldkil; 2011-07-29 at 10:41 AM.

  9. #9
    Yea, that's the idea.
    SVPC are saved in WTF\Account\ACC\REALM\CHAR\SavedVariables\AddonFolderName.lua.
    Personally, I'd recommend something along the following lines:
    Code:
    if event == "ADDON_LOADED" and arg1 == "AddonFolderName" then
        GlobalKey = GlobalKey or {}
        local r = GetRealmName()
        GlobalKey[r] = GlobalKey[r] or {}
        local u = UnitName("player")
        GlobalKey[r][u] = GlobalKey[r][u] or {} -- defaults
        AddonObject.savedVar = GlobalKey[r][u]
    end
    Basically does the same as SVPC, but allows you to copy settings from other characters by simply accessing GlobalKey[r][name], while you can use AddonObject.savedVar for everyday purposes.
    UI & AddOns expert | Interface & Macros moderator - My work

  10. #10
    Uhm, this sounds utterly complicated. But fun. I want to have access to my wow client right now

    EDIT: the code you posted above, how can i implement into a slashcommand? it's legit code that could be copy-pasted? just trying to understand the code, you are creating an empty table if it doesn't exist, and at the "coordinates" (realmname, playername) you save the value of GlobalKey.

    It seems more like an array of arrays; what "AddonObject.savedVar" stands for? are they placeholders?
    Last edited by Coldkil; 2011-07-29 at 02:16 PM.

  11. #11
    It should go into your OnEvent code. It assigns the table GlobalKey[current realm][current character] to the AddonObject.savedVar key (while making sure all those tables even exist). AddonObject should be your main addon table - savedVar can be anything.
    The reason we do this on ADDON_LOADED is that, before that event, all savedVar values are nil - ADDON_LOADED indicates that all code and savedVars are loaded.

    In your slash command, you'd do something like this:
    Code:
    AddonObject.savedVar.someValue = true
    UI & AddOns expert | Interface & Macros moderator - My work

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •