View Single Post
07/21/15, 03:58 AM   #4
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,578
Best way to handle a situation where you create a predictable amount of controls that you don't need all the time is using a ZO_ObjectPool.
You specify a Factory function that returns the object that is used and a Reset function that is called whenever an object is released.

Lua Code:
  1. local function EntryFactory(pool)
  2.   local name = "MyEntry" .. pool:GetNextControlId()
  3.   local container = parentWindow:CreateControl(name, CT_CONTROL)
  4.  
  5.   local labelA = container:CreateControl("$(parent)Level", CT_LABEL)
  6.   labelA :SetFont("ZoFontGameMedium")
  7.   container.labelA = labelA
  8.  
  9.   return container
  10. end
  11.  
  12. local function ResetEntry(entry)
  13.   entry:SetHidden(true)
  14.   entry:ClearAnchors()
  15. end
  16.  
  17. local pool = ZO_ObjectPool:New(EntryFactory, ResetEntry)

When you need a new object you call AquireObject like this:
Lua Code:
  1. local entry, key = pool:AcquireObject()
  2. entry:SetHidden(false)
  3. entry:SetAnchor(...)
  4. ...

And once you are finished you can release it via the key:
Lua Code:
  1. pool:ReleaseObject(key)

If you don't care about specific objects you can also just call ReleaseAllObjects and reset them all at once:
Lua Code:
  1. pool:ReleaseAllObjects()

EDIT: The comment in the zo_objectpool.lua is also quite interesting:
--[[

A generic pool to contain "active" and "free" objects. Active objects
are typically objects which:
1. Have a relatively high construction cost
2. Are not lightweight enough to create many of them at once
3. Tend to be reused as dynamic elements of a larger container.

The pool should "rapidly" reach a high-water mark of contained objects
which should flow between active and free states on a regular basis.

Ideal uses of the ZO_ObjectPool would be to contain objects such as:
1. Scrolling combat text
2. Tracked quests
3. Buff icons

The pools are not intended to be used to track a dynamic set of
contained objects whose membership grows to a predetermined size.
As such, do NOT use the pool to track:
1. Chat filters
2. Inventory slots
3. Action buttons (unless creating something like AutoBar)

A common usage pattern is instantiating templated controls. To facilitate this
without bloating your own code you should use ZO_ObjectPool_CreateControl which has
been written here as a convenience. It creates a control named "template"..id where
id is an arbitrary value that will not conflict with other generated id's.

If your system depends on having well-known names for controls, you should not use the
convenience function.
--]]

Last edited by sirinsidiator : 07/21/15 at 04:01 AM.
  Reply With Quote