The ObjectPool
module provides a mechanism for efficiently managing objects without causing FPS drops. It allows objects to be reused rather than recreated, optimizing performance and resource usage.
-
Initialization:
- Create an instance of
ObjectPool
usingObjectPool.new()
with specified object template, creation, and reset functions. - The
create
function should create new objects based on the specified template. - The
reset
function should reset the object's state to be ready for reuse.
- Create an instance of
-
Object Retrieval:
- Retrieve objects from the pool using
:GetObject()
method. - New objects are created if the pool is empty, up to the maximum pool size limit.
- Retrieve objects from the pool using
-
Object Return:
- Return objects to the pool after use with
:ReturnObject(obj)
. - Objects are reset and made available for reuse.
- Return objects to the pool after use with
local ObjectPool = require(script.ObjectPool)
-- Define object creation and reset functions
local function createObject(template: Model)
local newObj = template:Clone()
return newObj
end
local function resetObject(obj)
obj.Position = Vector3.new(0, 0, 0)
obj.Parent = nil
end
local objectTemplate = Instance.new("Part")
local minSize, maxSize = 10, 50
local pool = ObjectPool.new(objectTemplate, createObject, resetObject, minSize, maxSize)
-- Get an object from the pool and then return it
local obj1 = pool:GetObject()
wait(1)
pool:ReturnObject(obj1)
local obj2 = pool:GetObject()
wait(1)
pool:ReturnObject(obj2)
- More information, about functions you can read in module.
-- Programmed by notqaltx (@qaltx)