WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Slash commands (https://www.wowinterface.com/forums/showthread.php?t=21679)

zork 04-03-09 08:39 AM

Slash commands
 
I want to start using slash commands and storing some values in the WTF folder. Has someone any good links on this topic? What I want to know is how to save values and how to re-read them from the WTF folder at the next login.

Slakah 04-03-09 09:23 AM

There are quite a few ways of doing it, but this is what I do.

Add a saved Variables field to the toc:

Code:

## SavedVariables: MyAddonSV
Create an event handler for ADDON_LOADED and assign a table to MyAddonSV if it doesn't have one, and optionally set up defaults.
ie.
Code:

local eframe = CreateFrame("Frame")
eframe:RegisterEvent("ADDON_LOADED")
eframe:SetScript("OnEvent", function(self, event, addon)
  self:UnregisterEvent("ADDON_LOADED")
  self:SetScript("OnEvent", nil)
  MyAddonSV = MyAddonSV or {}
end)

MyAddonSV now operates like any other table except it's stored on reload.

Aezay 04-03-09 01:11 PM

I've always been using VARIABLES_LOADED.

p3lim 04-03-09 02:05 PM

Both are usable, I use a bit different system to handle my savedvariables.
Here is an example from pMinimap:

Code:

local defaults = {
        one = true,
        two = false,
        three = 'test',
}

local function LoadDefaults()
        pMinimapDB = pMinimapDB or {}
        for k,v in next, defaults do
                if(type(pMinimapDB[k]) == 'nil') then
                        pMinimapDB[k] = v
                end
        end
end

local function SlashHandler(str)
        if(str == 'reset') then
                pMinimapDB = nil
                LoadDefaults()
                print'savedvariables reset'
        else
                -- whatever
        end
end

SLASH_PMINIMAP1 = '/pmm'
SlashCmdList.PMINIMAP = SlashHandler

Works quite well, it allows for defaults to be set, even when adding new entries.

I also put in a sample of my slashhandler

Slakah 04-03-09 02:07 PM

Quote:

Originally Posted by Aezay (Post 123992)
I've always been using VARIABLES_LOADED.

I just use "ADDON_LOADED" as all of my mods use AddonLoader.

Also as a slight aside I couldn't help but notice this
Quote:

(type(pMinimapDB[k]) == 'nil')
nil == nil, there's really no need for an extra function call, which is odd as you seem determined to avoid them here
Quote:

for k,v in next, defaults do

p3lim 04-03-09 02:19 PM

Quote:

Originally Posted by Slakah (Post 124003)
I just use "ADDON_LOADED" as all of my mods use AddonLoader.

Also as a slight aside I couldn't help but notice this
nil == nil, there's really no need for an extra function call, which is odd as you seem determined to avoid them here

Code:

for k, v in next, defaults do
this checks the entries
Code:

if(type(pMinimapDB[k]) == 'nil') then
this checks if the value has been reset and/or does not exist.

if so, proceed to add the default settings.

Slakah 04-03-09 02:32 PM

Quote:

Originally Posted by p3lim (Post 124005)
Code:

for k, v in next, defaults do
this checks the entries
Code:

if(type(pMinimapDB[k]) == 'nil') then
this checks if the value has been reset and/or does not exist.

if so, proceed to add the default settings.

I know exactly what the code does, I was just interested why you were so keen to avoid a pairs() call and yet will call type() and do a table lookup on each iteration when you could just do
Code:

if pMinimapDB[k] == nil then
.

But I digress.

p3lim 04-03-09 02:39 PM

Not sure if you saw or not, it checks the default table, matches it with my savedvariable.
using 'if(v == nil) then' wouldnt result in anything, because nothing in the default table is nil.

again:
Code:

for k, v in next, defaults do
      if(type(pMinimapDB[k]) == 'nil') then
            ...


Slakah 04-03-09 02:58 PM

Quote:

Originally Posted by p3lim (Post 124010)
Not sure if you saw or not, it checks the default table, matches it with my savedvariable.
using 'if(v == nil) then' wouldnt result in anything, because nothing in the default table is nil.

again:
Code:

for k, v in next, defaults do
      if(type(pMinimapDB[k]) == 'nil') then
            ...


I knew there was I reason I didn't notice that one at first glance :P.
I meant
Code:

pMinimapDB[k] == nil
but anyway theres really no point discussing this I was just being pedantic (which I seem to be doing a lot recently :P).

p3lim 04-03-09 03:00 PM

Quote:

Originally Posted by Slakah (Post 124013)
I knew there was I reason I didn't notice that one at first glance :P.
I meant
Code:

pMinimapDB[k] == nil
but anyway theres really no point discussing this I was just being pedantic (which I seem to be doing a lot recently :P).

Ahh now I understand.
The reason why I used type() was because we've already discussed this in a different topic, and it seemed to work, so Ive used it ever since.

Anyways, this has driven off topic quite a bit.

Aezay 04-03-09 03:11 PM

A thing I started to do recently was to, instead of populating the users saved variables with the defaults from a table, I would just set the __index metatable to the default table, much easier.

Code:

local function LoadDefaults()
        pMinimapDB = setmetatable(pMinimapDB or {},{ __index = defaults });
end


Spellshaper 04-04-09 05:56 PM

You might also like to take a look at how to set up slash commands:

Code:

SlashCmdList["ParaUISlashCOMMAND"] = function(msg)
    if not msg or msg == "" or msg == "help" or msg == "about" then
      print("some general information on what this addon does and how to use it")
    else
        -- some more slash command processing.. string.find() inside
        -- an ipairs(listOfArguments) loop comes to mind.

    end
end
SLASH_ParaUISlashCOMMAND1 = "/paraui" -- it's not case sensitive, as far as I know.



All times are GMT -6. The time now is 06:08 PM.

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