Thread Tools Display Modes
11-28-16, 06:03 PM   #61
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
The error is telling you that you're trying to write to a table that doesn't exist. You need to create a table before you can store entries in it.

Originally Posted by _Max_Cavalera_ View Post
Code:
if not addonDB["chars"][realm .. " " .. name] then
addonDB["chars"][realm .. " " .. name]["profile"] = "default"
Assuming you have the addonDB table defined as you posted earlier, you need to create a table at [realm .. " " .. name] before you can store anything at ["profile"].
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 11-28-16 at 06:33 PM.
  Reply With Quote
11-28-16, 07:20 PM   #62
_Max_Cavalera_
A Fallenroot Satyr
 
_Max_Cavalera_'s Avatar
Join Date: Dec 2007
Posts: 28
So the problem is trying to create 2 new table "levels" at the same time? One table that doesn't exist [profile] inside another table that also doesn't exist yet [realm .. " " .. name].

If that's it I get it now.
But if it's just simply trying to write "default" on A table that doesn't exist, then I don't get it, because if I remove [profile] from the equation (so i'm just creating 1 new table "level" [realm .. " " .. name]), that same line works.

Sorry for the dumb questions
  Reply With Quote
11-28-16, 08:28 PM   #63
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
If you know that addonDB["chars"] exists then you could:
Code:
if not addonDB["chars"][realm .. " " .. name] then
   addonDB["chars"][realm .. " " .. name] = { ["profile"] = "default" }
end
Creating the [realm .. " " .. name] table to add the ["profile"] entry to.

or:
Code:
if not addonDB["chars"][realm .. " " .. name] then
   addonDB["chars"][realm .. " " .. name] = {}
   addonDB["chars"][realm .. " " .. name]["profile"] = "default"
end
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-28-16 at 08:49 PM.
  Reply With Quote
11-28-16, 09:51 PM   #64
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by _Max_Cavalera_ View Post
So the problem is trying to create 2 new table "levels" at the same time? One table that doesn't exist [profile] inside another table that also doesn't exist yet [realm .. " " .. name].
To explain this better and give you a thorough understanding on how Lua indexes tables, think of using the square brackets as an index operation rather than part of the variable name. You have your table addonDB and the first thing you do is try to grab what's at index ["chars"]. This happens to be another table, which is good. You then try to grab from this new table, whatever is at [realm .. " " .. name], which at this point we know is nil. The problem comes up when the next step is to try to index ["profile"] from the resulting nil from the previous step. As Fizzlemizz posted previously, you need to create the table at [realm .. " " .. name].

PS: You can use the .name notation to index tables as well. For example, addonDB.chars is the same as addonDB["chars"].
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
11-28-16, 10:40 PM   #65
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
PS: You can use the .name notation to index tables as well. For example, addonDB.chars is the same as addonDB["chars"].
But you will still need the brackets for variable entries like the [realm .. " " .. name] table.

Code:
if not addonDB.chars[realm .. " " .. name] then
   addonDB.chars[realm .. " " .. name] = { profile="default" }
end
Code:
local profile = addonDB.chars[realm .. " " .. name].profile
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-29-16 at 01:19 AM.
  Reply With Quote
11-29-16, 06:30 AM   #66
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by Fizzlemizz View Post
But you will still need the brackets for variable entries like the [realm .. " " .. name] table.
Among other things like .name can only be used with keys that follow the restrictions on variable names (only alphanumerics and underscores and the first character cannot be a number). Indexing by an expression or variable is a special case even though it is quite common.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 11-29-16 at 06:33 AM.
  Reply With Quote
11-29-16, 08:34 AM   #67
_Max_Cavalera_
A Fallenroot Satyr
 
_Max_Cavalera_'s Avatar
Join Date: Dec 2007
Posts: 28
Thank you both, I totally get it.

About the . I was actually already using it, learned from the docs I read on tables when researching this issue, what I understood was a . replaces [""], so .whatever is really ["whatever"]

That = {profile = "default"} I never seen so that's a great new "trick" for me 😀

Last edited by _Max_Cavalera_ : 11-29-16 at 08:38 AM.
  Reply With Quote
11-29-16, 09:29 AM   #68
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by _Max_Cavalera_ View Post
And following the same logic, to create a new one I could do

Lua Code:
  1. local name, _ = UnitName("player");
  2. local realm = GetRealmName();
  3.  
  4. addonDB["chars"][realm .. " " .. name]["profile"] = "newprofilename"

So, what am I doing wrong? I get an error saying "attempt to index field '?' (a nil value)" on the "addonDB[......." line
You need to make sure each table in the chain exists, and if it doesn't, you need to create it before you can add values in it:

Code:
if not addonDB then
    addonDB = {}
end
if not addonDB["chars"] then
    addonDB["chars"] = {}
end
if not addonDB["chars"][realm .. " " .. name] then
    addonDB["chars"][realm .. " " .. name] = {}
end
if not addonDB["chars"][realm .. " " .. name]["profile"] then
    addonDB["chars"][realm .. " " .. name]["profile"] = "newprofilename"
end
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
11-29-16, 10:06 AM   #69
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
This is easier to read imo.
Lua Code:
  1. local char = realm .. ' ' .. name
  2.  
  3. addonDB = addonDB or {}
  4. addonDB.chars = addonDB.chars or {}
  5. addonDB.chars[char] = addonDB.chars[char] or {}
  6. addonDB.chars[char].profile = addonDB.chars[char].profile or 'newprofilename'
__________________
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Learning, need help

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