Go Back   WowAce Forums > General > Lua Code Discussion
Lua Code Discussion You scared? Terrified. Mortified. Petrified. Stupefied... by [coding].

Reply
 
Thread Tools
Old 2 Weeks Ago   #11
OrionShock
Legendary Member
 
OrionShock's Avatar
 
Join Date: May 2006
Location: Arizona
Posts: 3,379
Default Re: Can't Get SavedVariables to Work...

ok, a few things that are obvious that people didn't point out.

From your original code.
Code:
self.db.char.config = { ['OptionA']  = True };
Does nothing but create an empty table attached to self.db.char.config. try this in wow.

[[ /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 };
Will do all sorts of bad things to your code. All tables are Unique, when you use a {} pair, that's a new table ALWAYS. [[ hint , {} ~= {} ]] A correct form would be
Code:
self.db.char.config = { ['OptionA']  = true };
self.db.char.config['OptionB'] = false;
--or
self.db.char.config = { 
    ['OptionA']  = true,
    ['OptionB'] = false
    };
Some bedtime reading material for you

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.
OrionShock is online now   Reply With Quote
Old 2 Weeks Ago   #12
Adirelle
Amazing Member
 
Adirelle's Avatar
 
Join Date: Dec 2006
Location: Lyon, France
Posts: 1,639
Default Re: Can't Get SavedVariables to Work...

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
Do this:

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, ".")
AceDB-3.0 will take care of initializing the values for you.
__________________
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.
Adirelle is offline   Reply With Quote
Old 2 Weeks Ago   #13
highflight1985
Member
 
Join Date: Sep 2007
Posts: 30
Default Re: Can't Get SavedVariables to Work...

Quote:
Originally Posted by OrionShock View Post
Code:
self.db.char.config = { ['OptionA']  = True };
self.db.char.config = { ['OptionB'] = False };
Will do all sorts of bad things to your code.
Hm. I won't take credit for writing that bad code. 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:
Originally Posted by OrionShock View Post
Some bedtime reading material for you

http://www.lua.org/pil/
Read the majority of that. Other than the very basics of Lua, not much of it seemed WOW-specific applicable. Of course, for wow-specific stuff, I should probably be reading what the WowWikki has related to addon programming... What I mean to say is: thanks for the link.

Quote:
Originally Posted by OrionShock View Post
Last but not least, here is 2 cookies.
http://paste.wowace.com/1379/
http://paste.wowace.com/1380/
Moreover, thanks for the pastes! Those worked with one correction: When you defined the defaults table, you left out the "s" in "local defaultsDB" and then later setup the LibStub reference with the "s" included. Anyway, I fixed it and it seems to work. Thanks again.

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.
highflight1985 is offline   Reply With Quote
Old 2 Weeks Ago   #14
OrionShock
Legendary Member
 
OrionShock's Avatar
 
Join Date: May 2006
Location: Arizona
Posts: 3,379
Default Re: Can't Get SavedVariables to Work...

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)
would print out false. So in your origional code, when you set optionB you removed the table that held optionA from being referenced.
__________________
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.
OrionShock is online now   Reply With Quote
Old 2 Weeks Ago   #15
highflight1985
Member
 
Join Date: Sep 2007
Posts: 30
Default Re: Can't Get SavedVariables to Work...

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
Notice the addition of a global saved variable...I don't intend on actually using this in a real addon...it's just for my learning purpose...anyway...

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
Is what refers to it...it prints out nil.
highflight1985 is offline   Reply With Quote
Old 2 Weeks Ago   #16
egingell
Hero Member
 
egingell's Avatar
 
Join Date: May 2006
Location: Cenarion Circle
Posts: 805
Send a message via AIM to egingell Send a message via MSN to egingell
Default Re: Can't Get SavedVariables to Work...

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).
egingell is offline   Reply With Quote
Old 2 Weeks Ago   #17
Torhal
Moderator
 
Torhal's Avatar
 
Join Date: Feb 2008
Posts: 1,431
Send a message via ICQ to Torhal
Default Re: Can't Get SavedVariables to Work...

Quote:
Originally Posted by highflight1985 View Post
Ah! I understand now about the tables...
<snip>
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.
Torhal is offline   Reply With Quote
Old 2 Weeks Ago   #18
highflight1985
Member
 
Join Date: Sep 2007
Posts: 30
Default Re: Can't Get SavedVariables to Work...

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
highflight1985 is offline   Reply With Quote
Old 2 Weeks Ago   #19
Torhal
Moderator
 
Torhal's Avatar
 
Join Date: Feb 2008
Posts: 1,431
Send a message via ICQ to Torhal
Default Re: Can't Get SavedVariables to Work...

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.
Torhal is offline   Reply With Quote
Old 2 Weeks Ago   #20
Seerah
Legendary Member
 
Seerah's Avatar
 
Join Date: May 2006
Posts: 6,102
Default Re: Can't Get SavedVariables to Work...

*nod* *nod*

What do you see when you actually open up the saved vars file for your addon?
__________________
"ACE = Accidental Caps-Lock Error" -Dameek


Seerah is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 08:46 AM.