Quantcast
Add table to list - WoWInterface
Thread Tools Display Modes
02-21-21, 05:53 PM   #1
Walkerbo
A Flamescale Wyrmkin
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 135
Add table to list

Hi all

I am having an issue saving a table to a global table.

My add function requires a table and an itemlink, (for testing, I have the itemLink fixed as a number).

Here is my code and the button script;
Lua Code:
  1. local tempSpellChatList = {}
  2.  
  3. local function addSpellToClassSpellList(addClassSpellList, addSpellLink)
  4.     print("test function incoming", addClassSpellList, spellLink) -- debug --
  5.     tempSpellChatList = {
  6.         addSpellLink = {
  7.             percentage = 100,
  8.             isEnabled = true,
  9.             spellChatList = {}
  10.         }
  11.     }
  12.     table.insert(addClassSpellList, 1, tempSpellChatList)
  13.     wipe(tempSpellChatList)
  14. end
  15.  
  16. DeathKnightFrame.addItemButton:SetScript(
  17.     "OnClick",
  18.     function()
  19.         local itemLink = 123
  20.         print("test outgoing", DeathKnightSpellList, itemLink)
  21.         addSpellToClassSpellList(DeathKnightSpellList, itemLink)
  22.         updateDeathKnightSpellList()
  23.     end
  24. )

The issue is that when I click the button the itemLink comes across as nil so the table is saved as an empty table.

The table comes across as the empty table is added so it is just the itemLink that is not coming across correctly.

My test prints confirm the itemlink is sent yet it does not seem to reach its destination.

I have stared at this code for ages without working out what I have messed up.

Hopefully, someone here can point out where I have screwed up.
__________________
"As someone once told me, frames are just special types of tables, and tables are special types of pointers."
Fizzlemizz
  Reply With Quote
02-21-21, 07:03 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,424
The second parameter of your addSpellToClassSpellList function is addSpellLink.

In the Incomming test, you're printing spellLink which will always be nil unless it's defined elsewhere.

Code:
print("test function incoming", addClassSpellList, addSpellLink)
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-21-21 at 07:06 PM.
  Reply With Quote
02-21-21, 07:16 PM   #3
Walkerbo
A Flamescale Wyrmkin
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 135
Heya Fizzlemizz

I have fixed the incoming test print, (all my fault as I changed some of my variable names).

The test print now confirms the spellLink does indeed come across, however, the main issue of the table not being saved to the list still is still broken.

Each time I try to write the table to my list it is only saving an empty table.
__________________
"As someone once told me, frames are just special types of tables, and tables are special types of pointers."
Fizzlemizz
  Reply With Quote
02-21-21, 07:26 PM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,424
Lua Code:
  1. tempSpellChatList = {
  2.     addSpellLink = {
  3.         percentage = 100,
  4.         isEnabled = true,
  5.         spellChatList = {}
  6.     }
  7. }

Not sure of your intention but when written like this, addSpellLink becomes the name of the key as in
Lua Code:
  1. tempSpellChatList = {
  2.     ["addSpellLink"] = {
  3.         percentage = 100,
  4.         isEnabled = true,
  5.         spellChatList = {}
  6.     }
  7. }

If you want to use the spell id as the key
Lua Code:
  1. tempSpellChatList = {}
  2. tempSpellChatList[addSpellLink] = {
  3.     percentage = 100,
  4.     isEnabled = true,
  5.     spellChatList = {}
  6. }
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
02-21-21, 07:29 PM   #5
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,424
Originally Posted by Walkerbo View Post
Heya Fizzlemizz

Each time I try to write the table to my list it is only saving an empty table.
Tables are passed as pointers to the table not copies of the table so when you
Code:
wipe(tempSpellChatList)
you are actually wiping the table you inserted at
Code:
table.insert(addClassSpellList, 1, tempSpellChatList)
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-21-21 at 07:31 PM.
  Reply With Quote
02-21-21, 09:02 PM   #6
Walkerbo
A Flamescale Wyrmkin
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 135
Hi Fizzlemizz

Yes you were right, the issue was that I was wiping the temptable, I would never have got this as I thought that
Lua Code:
  1. wipe(temptable)
was the same as
Lua Code:
  1. temptable = {}
Does that mean I can use wipe only on the saved tables in my toc?
__________________
"As someone once told me, frames are just special types of tables, and tables are special types of pointers."
Fizzlemizz
  Reply With Quote
02-21-21, 09:09 PM   #7
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,424
Wipe empties the contents of the table

Lua Code:
  1. table.insert(addClassSpellList, 1, tempSpellChatList)
  2. wipe(addClassSpellList[1])
  3. --is the same as
  4. wipe(tempSpellChatList)

In your code, there is no need to wipe anything as tempSpellChatList is being assigned a brand new table (any time you see = {} means a brand new table has been created.)

tempSpellChatList can be made local in the function as you're not saving anything through reuse.

Run the following and see the table id's (references, memory addresses, pointers...)
Code:
local a={"1"} 
print("A", a, a[1]) 

local b = a 
print("B", b, b[1]) 

wipe(a) 
print("wipe", "A")

print("B", b, b[1])

a = {}
print("A", a) -- a will point to a new table
print("B", b) -- b still holds the old pointer
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 02-21-21 at 09:30 PM.
  Reply With Quote
02-21-21, 09:33 PM   #8
Walkerbo
A Flamescale Wyrmkin
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 135
Hi Fizzlemizz

Thanks so much for your help and explanation.

__________________
"As someone once told me, frames are just special types of tables, and tables are special types of pointers."
Fizzlemizz
  Reply With Quote
02-21-21, 09:38 PM   #9
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,424
As someone once told me, tables are just pointers
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
02-21-21, 11:51 PM   #10
Walkerbo
A Flamescale Wyrmkin
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 135
lol, I see what you did there
__________________
"As someone once told me, frames are just special types of tables, and tables are special types of pointers."
Fizzlemizz
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Add table to list

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