WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Add table to list (https://www.wowinterface.com/forums/showthread.php?t=58599)

Walkerbo 02-21-21 05:53 PM

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.

Fizzlemizz 02-21-21 07:03 PM

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)

Walkerbo 02-21-21 07:16 PM

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.

Fizzlemizz 02-21-21 07:26 PM

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 02-21-21 07:29 PM

Quote:

Originally Posted by Walkerbo (Post 338510)
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)

Walkerbo 02-21-21 09:02 PM

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?

Fizzlemizz 02-21-21 09:09 PM

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


Walkerbo 02-21-21 09:33 PM

Hi Fizzlemizz

Thanks so much for your help and explanation.

:)

Fizzlemizz 02-21-21 09:38 PM

As someone once told me, tables are just pointers :D

Walkerbo 02-21-21 11:51 PM

lol, I see what you did there


All times are GMT -6. The time now is 02:14 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI