Pooler
An instance pooler.
Types
GetMethod
type
GetMethod =
"sequential"
|
"random"
The method that this Pooler should use to retrieve instances. sequential
will retrieve them in an ascending order (item 1 -> item n
)
whilst random
will retrieve instances randomly.
ReturnMethod
type
ReturnMethod =
"nilParent"
|
"cframe"
The method that this Pooler should use to return instances. nilParent
will set the instance's parent to nil
and leave all other
properties intact whilst cframe
will set the CFrame of the instance to a very long distance away, which is faster for BaseParts
and Models with PrimaryPart set. cframe
, as such, is only supported if you're pooling BaseParts or Models.
Options
interface
Options {
size:
number?
--
The number of objects to initialise this pool with. Defaults to 100.
unsafe:
boolean?
--
Whether or not the Pooler should skip safety checks. This pretty much means that you're on your own when it comes to doing things properly, but it increases performance. Defaults to false.
exhaustion:
boolean?
--
Whether or not the pool can be exhausted. When enabled, instances will be removed from the internal pool table and you will have to return them yourself to add them back. Defaults to false.
}
An object that describes any extra options you want to pass when creating a Pooler. All options are not required.
Functions
new
ConstructorPooler.
new
(
instanceTemplate:
T
,
--
The instance that this Pooler should base its pooled objects on.
) →
Pooler
<
T
>
--
A Pooler instance.
Creates a new Pooler instance.
local pool = Pooler.new(Instance.new("Part"))
Errors
Type | Description |
---|---|
"cannot create Pooler without an instance template" | Occurs when you're attempting to create a Pooler without a template. |
"cannot create Pooler with cframe return method if instance template is not a BasePart or Model" | Occurs when trying to use the `cframe` ReturnMethod when your template isn't a BasePart or a Model. |
"cannot create Pooler with cframe return method if instance template is a Model and it has no PrimaryPart" | Occurs when trying to use the `cframe` ReturnMethod when your template is a Model and it has no PrimaryPart. |
"cannot use random getMethod with exhaustion enabled" | Occurs when you're trying to use the `random` GetMethod with `exhaustion` enabled. `random` is not a valid GetMethod since it might retrieve a nil value. |
Get
Pooler:
Get
(
) →
T
--
An instance.
Gets an instance from the pool.
local pool = Pooler.new(Instance.new("Part"))
local instance = pool:Get()
Errors
Type | Description |
---|---|
"attempt to use destroyed Pooler instance" | Occurs when this Pooler has been destroyed and you are trying to call methods on it. |
"pool exhausted" | Occurs when the pool has been exhausted of all of it's instances and exhaustion is enabled. |
Wait
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsPooler:
Wait
(
) →
T
--
An instance.
Gets an instance from the pool. If the pool is exhausted, it will wait until an instance is available before returning.
local pool = Pooler.new(Instance.new("Part"))
local instance = pool:Wait()
Errors
Type | Description |
---|---|
"attempt to use destroyed Pooler instance" | Occurs when this Pooler has been destroyed and you are trying to call methods on it. |
Return
Pooler:
Return
(
instance:
T
--
The instance to return.
) →
(
)
Returns an instance to the pool.
local pool = Pooler.new(Instance.new("Part"))
local instance = pool:Get()
pool:Return(instance)
Errors
Type | Description |
---|---|
"attempt to use destroyed Pooler instance" | Occurs when this Pooler has been destroyed and you are trying to call methods on it. |
"attempt to return instance not part of this pool" | Occurs when you attempt to return an instance that wasn't a part of this pool to begin with. |
"instance is not a BasePart or Model but it's using the cframe return method" | Occurs when the instance you're returning is not a BasePart or a Model, but you're somehow using the CFrame return method. If safety is enabled and you haven't been tampering with the internals, this is almost always a bug and should be reported. |
Size
Pooler:
Size
(
) →
number
--
The size of the pool.
Get the current size of the pool.
local pool = Pooler.new(Instance.new("Part"))
local size = pool:Size()
Resize
Pooler:
Resize
(
newSize:
number
--
The new size the pool should be.
) →
(
)
Resizes the pool.
local pool = Pooler.new(Instance.new("Part"))
pool:Resize(1000)
Errors
Type | Description |
---|---|
"attempt to use destroyed Pooler instance" | Occurs when this Pooler has been destroyed and you are trying to call methods on it. |
Destroy
Pooler:
Destroy
(
) →
(
)
Destroys this Pooler and destroys all instances associated with the pool.
local pool = Pooler.new(Instance.new("Part"))
pool:Destroy()