Dynamically adding and removing frames? - WoWInterface
Thread Tools Display Modes
06-29-20, 09:27 PM   #1
A Kobold Labourer
Join Date: Jun 2020
Posts: 1
Dynamically adding and removing frames?

Hello! I'm new to creating addons and playing around with my first one. The bulk of my addon is a queue'ing system where players will be added to a queue and then a frame with their name / location / spot in queue will show up. As more people queue, more frames are added dynamically under the other frames.

I am wondering what the best way to approach this is? Ideally it would be an array of frames than are added one after another and then removed from the first spot onward. I know you cant "remove" a frame, so thats another thing I have to tackle.

Anyway, would appreciate any steering or ideas on how should I approach this.

  Reply With Quote
06-30-20, 01:25 AM   #2
I did that?
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,322
At it's simplest, you have a table of "active" frames. As they become no-longer "active" you move the frame to a "pool" table of inactive frames (delete the frame reference from the active table and add it to the inactive table).

If you need a new frame and the pool is empty, you create a new frame and add it to the active table otherwise take one from the pool table(delete the frame reference from the inactive table and add it to the active table).

This becomes more involved if you want to have frame types for different purposes and how far you want to abstract the pool concept but the general idea is the same.

To add a new active frame:
Lua Code:
  1. local frame
  2. if #MyInactiveFrameTable > 0 then -- 1 or more frames in the inactive table
  3.       frame = MyInactiveFrameTable[1] -- grab the 1st one
  4.       tremove(MyInactiveFrameTable, 1) -- and remove it from the table
  5. else -- otherwise
  6.       -- create a new frame
  7.       frame = CreateFream(...)
  8.        -- add frame elements
  9. end
  10. tinsert(MyActiveFrameTable, frame)  -- add the frame to the active table
  11. -- update "frame" with new information to display

To "deactivate" active frame:
Lua Code:
  1. tinsert(MyInactiveFrameTable, MyActiveFrameTable[1])
  2. tremove(MyActiveFrameTable, 1)
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 06-30-20 at 03:01 AM.
  Reply With Quote
06-30-20, 06:03 AM   #3
A Pyroguard Emberseer
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,993
To answer a question that shows up a lot here, frames cannot be "removed" from memory. They stay persistent throughout an entire session. The best you can do is recycle frames by use of "Frame Pools".

Here's a simpler way and keeping a separate table just for active frames in a frame pool is unnecessary if you're already keeping track of them in other parts of your code.

Lua Code:
  1. local FramePool={};
  3. local function FramePool_Acquire()
  4.     return table.remove(FramePool) or CreateFrame("Frame");--   Returns a frame if there is one in the pool or creates one if empty
  5. end
  7. local function FramePool_Release(frame)
  8.     table.insert(frame);--  Stores frame in pool
  9. end

Note: You should create a different pool for each frame template you wish to use in order to keep them from getting mixed up.

If you're feeling adventurous, you might want to take a look at Blizzard's implementation of frame and object pools in SharedXML\Pools.lua.
You'll want to pay attention to CreateFramePool() for a singilar frame pool and CreatePoolCollection() to manage different templates.
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 06-30-20 at 06:09 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Dynamically adding and removing frames?

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