View Single Post
11-18-12, 05:08 PM   #3
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
Originally Posted by Phanx View Post
That's because you're doing it wrong. Here's what your code is currently doing:

Code:
mounts = {}
category="new_category"
mount="Swift Moonsaber"
  1. The first line creates the table { } and assigns it to the variable "mounts".
  2. The second line creates the string "new_category" and assigns it to the variable "category".
  3. The third line creates the string "Swift Moonsaber" and assigns it to the variable "mount".

Now when you do:

Code:
mounts[category][mount]=true
This is what's happening:
  1. The code looks for a table under the variable name "mounts". This returns the table, {}.
  2. The code looks for the value under the variable name "category" as a table key. This returns nil, because the "mounts" table is empty.
  3. The code looks for the value under the table returned by "mounts[category]". This fails and throws an error, because "mounts[category]" is nil, not a table.

What you need to do is:

Code:
local mounts = {}
local category = "new_category"
local mount = "Swift Moonsaber"

mounts[category] = {}
Then you can add values to the category the way you were trying to:

Code:
mounts[category][mount] = true
Thanks Phanx,
I think I have finally understood :-)


On a side note, you should never create global variables with such generic names as "mounts" and "category" and "mount"...
Yes I know,
the above code was only a pseudo code I wrote to show the problem.
In the addon I surely use local for my names. :-)

Btw thanks for the great explanation on how the global var impact in the addon dataspace.

Now I an continue the rewrote :-)
  Reply With Quote