View Single Post
11-18-12, 01:51 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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
On a side note, you should never create global variables with such generic names as "mounts" and "category" and "mount". Such variables should always be declared as local. If you need those values to be accessible from other files, use your addon's private namespace (best) or give them distinct global names if absolutely necessary (last resort), such as "GimbolinoMounts" and "GimbolinoCategory". However, the only reasons to put anything in the global namespace are (1) it's a saved variable, or (2) you expect a need for it to be easily accessed from other addons, or (3) it's your main addon frame/table and you need to be able to easily access it in-game via "/run" commands for now for debugging purposes. If none of those apply, don't make it global.
__________________
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