Thread Tools Display Modes
01-07-11, 08:32 PM   #1
SignOfDoom
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Jul 2010
Posts: 18
problems with tables

Hi
My problem is the following:

In one function i give an table with variable keys a value.
Code:
function test(self, ...)
    ID=2
    Player=Bob
    level=80

    data[ID][Player][level] = "something"
    test2(self,...)
end
Now if i want to use this table in another function like this:

Code:
test2(self,...)
    text = data.2.Bob.80
end
it returns nil everytime. but in my savedvariables this table has its value.
Can someone please explain me what i am doing wrong
  Reply With Quote
01-07-11, 08:46 PM   #2
yssaril
A Warpwood Thunder Caller
 
yssaril's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2007
Posts: 96
first you have to make sure that each subtable is created before using it
Code:
data[ID] = data[ID] or {}
data[ID][Player] = data[ID][Player] or {}
data[ID][Player][level] = data[ID][Player][level] or {}
also you can not use the . table notation for numbers or strings that start with numbers

as such when you want to reference it again you need to use:
Code:
data[2].Bob[80]

or

data[2]["Bob"][80]

Also you seam to have a somewhat inefficient database layout

why not do something like this

Code:
data[ID] = {
     ["name"] =  "Bob",
     ["level"] = 80,
     ["text"] = something,
}
this way you only create one table per ID (saves memory and will probably make it easier to look through your SV file while also making it a little bit faster to load)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » problems with tables


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