WoWInterface (
-   Lua/XML Help (
-   -   Dynamically adding and removing frames? (

hux4 06-29-20 09:27 PM

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.


Fizzlemizz 06-30-20 01:25 AM

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)

SDPhantom 06-30-20 06:03 AM

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.

All times are GMT -6. The time now is 01:07 AM.

vBulletin © 2021, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI