Thread Tools Display Modes
04-03-09, 08:39 AM   #1
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
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.
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)
  Reply With Quote
04-03-09, 09:23 AM   #2
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
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.
  Reply With Quote
04-03-09, 01:11 PM   #3
Aezay
A Theradrim Guardian
 
Aezay's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 66
I've always been using VARIABLES_LOADED.
  Reply With Quote
04-03-09, 02:05 PM   #4
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
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
  Reply With Quote
04-03-09, 02:07 PM   #5
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by Aezay View Post
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
(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
for k,v in next, defaults do

Last edited by Slakah : 04-03-09 at 02:11 PM.
  Reply With Quote
04-03-09, 02:19 PM   #6
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
Originally Posted by Slakah View Post
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.
  Reply With Quote
04-03-09, 02:32 PM   #7
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by p3lim View Post
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.

Last edited by Slakah : 04-03-09 at 02:55 PM.
  Reply With Quote
04-03-09, 02:39 PM   #8
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
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
            ...
  Reply With Quote
04-03-09, 02:58 PM   #9
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Originally Posted by p3lim View Post
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).
  Reply With Quote
04-03-09, 03:00 PM   #10
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
Originally Posted by Slakah View Post
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.
  Reply With Quote
04-03-09, 03:11 PM   #11
Aezay
A Theradrim Guardian
 
Aezay's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2007
Posts: 66
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
  Reply With Quote
04-04-09, 05:56 PM   #12
Spellshaper
A Murloc Raider
 
Spellshaper's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2008
Posts: 8
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.
__________________
"We shaman don't command the magic we wield. As mages and warlocks strain and sweat to produce a tiny flame, I ask for the elements to lend me their strength."

Last edited by Spellshaper : 04-04-09 at 06:46 PM. Reason: I'm stupid and it's late
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Slash commands

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off