Thread Tools Display Modes
09-19-19, 07:48 PM   #1
Nibs
A Defias Bandit
AddOn Compiler - Click to view compilations
Join Date: Oct 2012
Posts: 2
Setting (and changing) anchors

I have a very simple add-on that displays four pieces of information textually instead of with a visual representation - each on their own frame. Each one can be turned on an off at the discretion of the user. I would like these frames to all be anchored to each other horizontally. Easy enough right? Anchor left edge of Frame2 to the right edge of Frame 1, Frame3 to Frame2 in the same manner, etc. But what if Frame2 is disabled? Then Frame3 needs to be anchored to Frame1.

I tried to run Frame:GetChildren() on an anchor frame to count the children and anchor them to the anchor frame itself instead of to each other, but Frame:GetChildren() returns a table of tables, and the "#" operator doesn't count tables.

As a bonus, I would like the user to be able to change the order of the frames - 3, 1, 4, 2; 4, 2, 3, 1; etc.

The question of how to do this has consumed me all day today. Perhaps it's the lack of sleep, or perhaps the lack of Lua experience. Either way, any help would be much appreciated.
  Reply With Quote
09-19-19, 10:12 PM   #2
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
GetChildren returns a table of tables because frames are tables.

Lua Code:
  1. local parent = CreateFrame("frame","Foo")
  2. local child1 = CreateFrame("frame","Bar1",parent) -- can also use Foo instead of parent, cannot be a string
  3. local child2 = CreateFrame("frame","Bar2",parent)
  4.  
  5. local children = parent:GetChildren()
  6.  
  7. print( children[1] == child1 ) -- true
  8. print( children[2] == child2 ) -- true
  9. print( children[1] == Bar1 ) -- true
  10. print( children[2] == Bar2 ) -- true
  11.  
  12. local count = 0
  13. for k in ipairs(children) do
  14.     count = count + 1
  15. end

Last edited by Kanegasi : 09-19-19 at 10:14 PM.
  Reply With Quote
09-20-19, 06:31 AM   #3
Nibs
A Defias Bandit
AddOn Compiler - Click to view compilations
Join Date: Oct 2012
Posts: 2
Yes, I knew that the frames themselves were tables. I come from a web-dev background and I would tackle this problem a little differently in JS. Your reply helps a lot. Thanks a bunch.
  Reply With Quote
09-20-19, 07:08 AM   #4
Sharparam
A Flamescale Wyrmkin
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 102
Originally Posted by Nibs View Post
[...] the "#" operator doesn't count tables.
Yes it does. see for example:

Lua Code:
  1. local tables = {
  2.     {1, 2, 3},
  3.     {4, 5, 6},
  4.     {7, 8, 9}
  5. }
  6.  
  7. print(#tables) -- prints "3"

As long as the indices start at 1 and are contiguous it will count just fine. But if you have an index missing somewhere in the chain, it will stop counting there. For example:

Lua Code:
  1. local tables = {}
  2. tables[1] = {1, 2, 3}
  3. tables[3] = {4, 5, 6} -- Notice we skipped index 2
  4. tables[4] = {7, 8, 9}
  5.  
  6. print(#tables) -- prints "1"

Edit: Actually, the behaviour of the "#" operator on a non-contiguous table is undefined, so you cannot rely on the output if the table doesn't have indices in sequence.

Last edited by Sharparam : 09-20-19 at 07:13 AM. Reason: Clarify behaviour on non-contiguous indices
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Setting (and changing) anchors

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