Thread Tools Display Modes
01-28-14, 11:14 AM   #1
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
Testing if a table is nil

My table keeps getting overwritten with the default values because when I test to see if it's nil it returns true even though it's already created and saved.
I have ## SavedVariables: SDRC_DB

and heres the code

Lua Code:
  1. local rName = GetRealmName()
  2. local pName = UnitName("player")
  3. SDRC_DB = SDRC_DB or {}
  4.  
  5. if SDRC_DB[rName] == nil then
  6.     print("realm name nil")
  7.     SDRC_DB[rName] = {
  8.         ["Player1"] = {
  9.             ["test"] = 0
  10.         }
  11.     }
  12. end
  13.  
  14. if SDRC_DB[rName][pName] == nil then
  15.     print("realm name player nil")
  16.     SDRC_DB[rName][pName] = {
  17.         ["world"] = false,
  18.         ["threshold"] = 30000
  19.     }
  20. end

I've also tried if not SDRC_DB[rName][pName] but it always returns nil

although my values are being overwritten in the lua, they are already created and saved correctly in the savedvariables file and are not the default values, so I'm somewhat confused.

Also, I don't know if my table setup is as condensed\optimized as it could be so any tips there would be great too

Last edited by Spawnova : 01-28-14 at 11:26 AM.
  Reply With Quote
01-28-14, 11:31 AM   #2
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
The reason for this is that SDRC_DB doesn't exists when you addon code is loaded.

You'll have to register for ADDON_LOADED or VARIABLES_LOADED and to do your test then. It will return SDRC_DB is ~= nil (if there is savevars data).

http://www.wowwiki.com/Saving_variab..._game_sessions ("The loading process")

Last edited by Duugu : 01-28-14 at 11:40 AM.
  Reply With Quote
01-28-14, 11:41 AM   #3
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
I see, that makes sense now.

However, having the table creation in the addon loaded event I now get nil values on everything that uses the variables before the addon loaded event.

is there a way to fix this without having to put all my code in the addon loaded event?
  Reply With Quote
01-28-14, 11:43 AM   #4
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Without any further information: nope.

Could you please show the full code?
  Reply With Quote
01-28-14, 11:51 AM   #5
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
I've got it now. =)

I just moved the the SetScript for a few things into the addon loaded event and left out the frame creations.

Thanks for the help!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Testing if a table is nil

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