Thread Tools Display Modes
11-04-11, 05:14 PM   #1
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
Strange tinsert behavior

I had a block of code that basically went like this:

lua Code:
  1. local Tables = {
  2.     [1] = CharacterTable,
  3.     [2] = GlobalTable,
  4. }
  5. local TableEntry = {
  6.     name = MyName
  7. }
  8.  
  9. for k, v in ipairs(Tables) do
  10.     table.insert(v, 1, TableEntry)
  11. end

Now, what happened was TableEntry was getting inserted twice, into both Tables, if both Tables contained the same data. So I'd end up with:

Code:
CharacterTable = {
    {
        ["name"] = "Me",
    }
    {
        ["name"] = "Me",
    }
}

GlobalTable = {
    {
        ["name"] = "Me",
    }
    {
        ["name"] = "Me",
    }
}
(If CharacterTable and GlobalTable contained a slightly different set of data before I inserted, then inserting would work fine.)

However, if I hotwire my code to this, it works fine:
lua Code:
  1. local Tables = {
  2.     [1] = CharacterTable,
  3.     [2] = GlobalTable,
  4. }
  5. local TableEntry1 = {
  6.     name = MyName
  7. }
  8. local TableEntry2 = {
  9.     name = MyName
  10. }
  11.  
  12.  
  13. for k, v in ipairs(Tables) do
  14.     if k == 1 then
  15.         table.insert(v, 1, TableEntry1)
  16.     else
  17.         table.insert(v, 1, TableEntry2)
  18.     end
  19. end

Am I missing something obvious?

Last edited by Nibelheim : 11-04-11 at 05:17 PM.
  Reply With Quote
11-04-11, 07:21 PM   #2
Saiket
A Chromatic Dragonspawn
 
Saiket's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 154
In your first example code, were CharacterTable and GlobalTable references to the same table? That's the most likely explanation I can think of.
  Reply With Quote
11-04-11, 07:28 PM   #3
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
Originally Posted by Saiket View Post
In your first example code, were CharacterTable and GlobalTable references to the same table? That's the most likely explanation I can think of.
They're two separate tables.
  Reply With Quote
11-04-11, 07:35 PM   #4
Saiket
A Chromatic Dragonspawn
 
Saiket's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 154
Assuming that first code block was your entire code, the only other things I can think of are a modified table.insert function, or metatables. It runs fine for me both in a standalone Lua interpreter and in-game.
  Reply With Quote
11-04-11, 07:41 PM   #5
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
Originally Posted by Saiket View Post
Assuming that first code block was your entire code, the only other things I can think of are a modified table.insert function, or metatables. It runs fine for me both in a standalone Lua interpreter and in-game.
Hmm, definitely is odd behavior. Oh well, I shall stash it in it's own little box of strangeness.
  Reply With Quote
11-07-11, 07:33 PM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Where are the CharacterTable and GlobalTable tables defined? I don't see that being done in your code.
  Reply With Quote
11-07-11, 11:52 PM   #7
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
Originally Posted by Phanx View Post
Where are the CharacterTable and GlobalTable tables defined? I don't see that being done in your code.
They're defined in a default table registry via AceDB. Just a simple char and global space. It's possible the way Ace deals with it's saved settings, and char/global tables, is interfering with the process.
  Reply With Quote
11-08-11, 12:27 AM   #8
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
More than possible. AceDB is a wrapper around the actual SavedVariables. If you want to set a value on the raw table, you'll have to do something like this:

lua Code:
  1. function MyAddOn:OnInitialize()
  2.     local db = LibStub("AceDB-3.0"):New("MyDB", DATABASE_DEFAULTS, "Default")
  3.     DATABASE_DEFAULTS = nil
  4.     private.db = db
  5.  
  6.     local raw_db = _G["MyDB"]
  7.  
  8.     if not raw_db.version or raw_db.version < private.database_version then
  9.         raw_db.version = private.database_version
  10.     end
  11. end

Variable names have been changed to protect the innocent.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Strange tinsert behavior

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