|
||||||
| Lua Code Discussion You scared? Terrified. Mortified. Petrified. Stupefied... by [coding]. | ||||||
![]() |
|
|
Thread Tools |
|
|
#11 |
|
Legendary Member
Join Date: May 2006
Location: Arizona
Posts: 3,379
|
ok, a few things that are obvious that people didn't point out.
From your original code. Code:
self.db.char.config = { ['OptionA'] = True };
[[ /run print(True, true) ]] it'll print out [[ nil, true ]] Meaning that True ~= true, remember Case Maters In Lua. ----- Code:
self.db.char.config = { ['OptionA'] = True };
self.db.char.config = { ['OptionB'] = False };
Code:
self.db.char.config = { ['OptionA'] = true };
self.db.char.config['OptionB'] = false;
--or
self.db.char.config = {
['OptionA'] = true,
['OptionB'] = false
};
http://www.lua.org/pil/ Last but not least, here is 2 cookies. http://paste.wowace.com/1379/ http://paste.wowace.com/1380/
__________________
Author of GuildCraft, SickOfClickingDailies, and CursorCooldown "I was there in the beginning... and things were very different back then" --An Echo from a time before. |
|
|
|
|
|
#12 |
|
Amazing Member
Join Date: Dec 2006
Location: Lyon, France
Posts: 1,639
|
Another point, AceDB can takes care of settings defaults values for you.
Instead of doing : Code:
self.db = LibStub("AceDB-3.0"):New("TestAddonDB")
if not self.db.char.config then
self.db.char.config = {}
end
if self.db.char.config.OptionA == nil then
self.db.char.config.OptionA = true
end
if self.db.char.config.OptionB == nil then
self.db.char.config.OptionB = false
end
Code:
local defaults = { -- self.db
char = { -- self.db.char
config = { -- self.db.char.config
OptionA = true, -- self.db.char.config.OptionA
OptionB = false -- self.db.char.config.OptionB
}
}
}
self.db = LibStub("AceDB-3.0"):New("TestAddonDB", defaults)
TestAddon:Print("OptionA is", self.db.char.config.OptionA, ".")
TestAddon:Print("OptionB is", self.db.char.config.OptionB, ".")
__________________
Author of Squire, Inline Aura and several other addons. If you have a problem and you think the solution involves using a regular expression, then you have two problems. |
|
|
|
|
|
#13 | |||
|
Member
Join Date: Sep 2007
Posts: 30
|
Quote:
I actually copied and pasted it from the addon Advertiser. Not really sure why it uses that code in that way. Aside from that, your good attempt at explaining why that is bad went completely over my head. I did not understand how the corrected form is better; and probably for the same reason why I didn't understand why the uncorrected form was bad. ![]() Anyway, I'm going to try and avoid using that format altogether. If I need to store information into the saved variables anytime outside of my defaults declaration, I'll use this: Code:
self.db.<some-level-of-global-char-profile-etc>.<variable-name-goes-here> = <value> i.e. self.db.profile.ProfileName = "Healing" Quote:
Quote:
One more thing: In the defaults table, or anytime you are creating a saved variable, is "config" required? What I mean is, in the following code... self.db.char.config.OptionA = True ...is "config" required? I ask because I copied the code I used from another addon. I've looked into other addons and several seem to use this as well, but I'm not yet convinced that it's required based on that. |
|||
|
|
|
|
|
#14 |
|
Legendary Member
Join Date: May 2006
Location: Arizona
Posts: 3,379
|
is .config required ? no, and yes. It's a mater of personal preference. OCD people would have it while normal people probabbly wouldn't use it if nothing else is being put there.
![]() As for working with tables, each and every time you see { and } in code, that's a new table. No 2 tables are the same ever. IE Code:
local table1 = {}
local table2 = {}
print(table1 == table2)
__________________
Author of GuildCraft, SickOfClickingDailies, and CursorCooldown "I was there in the beginning... and things were very different back then" --An Echo from a time before. |
|
|
|
|
|
#15 |
|
Member
Join Date: Sep 2007
Posts: 30
|
Ah! I understand now about the tables...
As far as config goes, what I think you're saying is that it's required for obsessive compulsive people, but not required by Lua or Wow. Right? Okay...one last thing and I'll leave this blasted thing alone and move on to the next part of my self-torture.... I made the following changes to your paste code: Code:
local defaultsDB = {
char = {
config = {
optionA = true,
optionB = false,
}, --config
}, --char
global = {
Version = "v0.01a Rev 31",
}, -- global
} --- defaultsDB
Problem is: I'm not able to access it just like the problem I was having originally with OptionA and OptionB. Code:
function MyTestAddon:OnEnable()
D("OnEnable")
D("self.db.char.config.OptionA =", self.db.char.config.OptionA )
D("self.db.char.config.OptionB =", self.db.char.config.OptionB )
D("Version = ", self.db.global.Version )
D("End OnEnable")
end
|
|
|
|
|
|
#16 |
|
Hero Member
|
Have you tried changing "self.db.char.config" to "self.db.global.config"?
__________________
Bruce Lee killed Chuck Norris! Don't call me a nerd. That is offensive to my people. My Characters (yes, I have a guild with only me in it). |
|
|
|
|
|
#17 |
|
Moderator
|
Read Programming in Lua again, though not from beginning to end - find and read the sections that relate to what you are currently working with so you'll have a better understanding.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes". Author of Revelation, Volumizer, and many other AddOns. |
|
|
|
|
|
#18 |
|
Member
Join Date: Sep 2007
Posts: 30
|
Man...I feel like I'm beating a dead horse here, but this thing is still acting funny...
In the following code, I define the defaults and apply them with the LibStub statement... However, the defaults don't get applied. The values are still == nil. I know this because of my if-then statement. The values only get applied once the if-then statement is run. I've checked the spelling and variable names...everything seems right. Maybe I'm suffering from code-blindness? Code:
local debug = true
local function D(...)
local s = string.join(", ", tostringall(...) )
s = s:gsub("([=:]),", "%1")
if debug then
print("|cFFff6633TestAddon Debug:|r ", s)
end
return s
end
MyTestAddon = LibStub("AceAddon-3.0"):NewAddon("MyTestAddon", "AceConsole-3.0")
local defaultsDB = {
char = {
optionA = true,
optionB = false,
}, --char
global = {
Version = "v0.01a Rev 1651215",
}, -- global
} --- defaultsDB
function MyTestAddon:OnInitialize()
-- This code is run as soon as the addon is loaded.
D("OnInit")
D("Init DB")
self.db = LibStub("AceDB-3.0"):New("MyTestAddonDB", defaultsDB)
D("End OnInit")
end
function MyTestAddon:OnEnable()
D("OnEnable")
if (self.db.char.OptionA == nil) then
D("self.db.char.OptionA == nil; setting values.")
self.db.char.OptionA = true
self.db.char.OptionB = false
self.db.global.Version = "v0.01a Rev 1651215"
D("Values are now set.")
end
D("self.db.char.OptionA =", self.db.char.OptionA )
D("self.db.char.OptionB =", self.db.char.OptionB )
D("self.db.global.Version =", self.db.global.Version )
D("End OnEnable")
end
|
|
|
|
|
|
#19 |
|
Moderator
|
In your defaults, you are defining "optionA", yet your code checks for "OptionA".
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes". Author of Revelation, Volumizer, and many other AddOns. |
|
|
|
|
|
#20 |
|
Legendary Member
Join Date: May 2006
Posts: 6,102
|
*nod* *nod*
What do you see when you actually open up the saved vars file for your addon? |
|
|
|
![]() |
«
Previous Thread
|
Next Thread
»
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
All times are GMT. The time now is 08:46 AM.
WowAce Forums





I actually copied and pasted it from the addon Advertiser. Not really sure why it uses that code in that way. 





