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:
local function EntryFactory(pool)
local name = "MyEntry" .. pool:GetNextControlId()
local container = parentWindow:CreateControl(name, CT_CONTROL)
local labelA = container:CreateControl("$(parent)Level", CT_LABEL)
labelA :SetFont("ZoFontGameMedium")
container.labelA = labelA
return container
end
local function ResetEntry(entry)
entry:SetHidden(true)
entry:ClearAnchors()
end
local pool = ZO_ObjectPool:New(EntryFactory, ResetEntry)
When you need a new object you call AquireObject like this:
Lua Code:
local entry, key = pool:AcquireObject()
entry:SetHidden(false)
entry:SetAnchor(...)
...
And once you are finished you can release it via the key:
If you don't care about specific objects you can also just call ReleaseAllObjects and reset them all at once:
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.
--]]
|