GoodPool
A flexible and lightweight generic object pool.
GoodPool allows you to reuse objects instead of repeatedly creating and destroying them. You provide a function to create the object, another to initialize it when acquired, and an optional one to destroy it when the pool is cleaned.
local tblPool = GoodPool.new(
function()
return { value = 10 }
end,
function(tbl)
tbl.value = 10
end
)
local obj = tblPool:Get()
obj.value += 10
print(obj.value) -- 20
tblPool:Return(obj)
print(tblPool:Get().value) -- 10
Functions
new
GoodPool.new(createFn: () → T,--
Called when the pool is empty and a new object is needed.
initFn: (T) → (),--
Called every time an object is acquired. Use this to reset its state.
destroyFn: ((T) → ())?--
Called on each object when Clean is invoked. Use this to free resources.
) → GoodPool<T>Creates a new GoodPool.
forInstance
GoodPool.forInstance(templateOrClassName: Instance | string,--
An existing Instance to clone, or a class name string to create one from.
) → GoodPool<Instance>,InstanceCreates a GoodPool that clones Instances from a template.
If a class name string is provided, a new Instance of that class is created and used as the template. The template is returned alongside the pool so you can reference it later regardless of how it was created.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local partPool, template = GoodPool.forInstance(
ReplicatedStorage.Part,
function(part)
part.Transparency = 0
part.Position = Vector3.new(0, 0, 0)
end
)
print(template == ReplicatedStorage.Part) -- true
local part = partPool:Get()
part.Parent = workspace
partPool:Return(part)
Get
GoodPool:Get() → TRetrieves an object from the pool, passing it through initFn before returning.
If the pool is empty, a new object is created via createFn.
Preload
GoodPool:Preload(amount: number--
Number of objects to pre-fill the pool with.
) → ()Pre-fills the pool with a given number of objects created via createFn.
Useful for avoiding lag spikes on first use. Does nothing if amount is <= 0.
Resize
GoodPool:Resize(newAmount: number--
The desired pool size.
) → ()Resizes the pool to the given amount.
If newAmount is greater than the current size, new objects are created via Preload.
If newAmount is less, excess objects are destroyed via Clean.
Does nothing if newAmount equals the current size.
Clean
GoodPool:Clean(amount: number?--
Number of objects to destroy. Defaults to all objects in the pool.
) → ()Destroys objects in the pool via destroyFn.
If amount is provided, only that many objects are destroyed.
Otherwise, the entire pool is cleared.
Objects that are currently in use (i.e. not yet returned) are not affected.
GetSize
GoodPool:GetSize() → numberReturns the number of objects currently sitting idle in the pool.
IsEmpty
GoodPool:IsEmpty() → booleanReturns true if the pool has no objects available, false otherwise.
Return
GoodPool:Return(item: T--
The object to return.
) → ()Returns an object back to the pool so it can be reused later.
CAUTION
This does not reset the object's state. State reset is handled by
initFn the next time the object is acquired via Get.
ReturnList
GoodPool:ReturnList(list: {T}--
The objects to return to the pool.
) → ()Returns a list of objects back to the pool so they can be reused later.
CAUTION
This does not reset the objects' state. State reset is handled by
initFn the next time each object is acquired via Get.