Skip to content
This repository was archived by the owner on Jun 14, 2022. It is now read-only.
LucasMZ edited this page Aug 29, 2021 · 5 revisions

Recycler

.new()

Creates a recycler object!

:AddToGarbage(object)

Adds an object to weak garbage, it can still be garbage collected by Lua, however it will stay on a temporary storage until then.

This method accepts MULTIPLE objects at once, it will insert them for you.

:AddToStrongGarbage(object)

Adds an object to a strong garbage, the object passed will never be garbage collected, it can only be garbage collected if the recycler object gets collected, or if an object was re-used from it.

This method accepts MULTIPLE objects at once, it will insert them for you.

:GetObject()

Returns an object if there is one to reuse.

It can return an object if there if there's a :OnNewObject handler which returns one. You can read about it below.

:GetObjects(objectCount, createNewObjects)

Returns an array with the amount of objects asked for.

createNewObjects defaults to true if unspecified, you don't need to tell it explicitly.

In case if either createNewObjects is false, or there is no :OnNewObject handler, and there's no objects to reuse, the array might not return that amount of objects, instead it will only return the ones it was able to re-use.

:OnNewObject(function()

Sets a handler function to when there is no available object to re-use on :GetObject, whatever it returns will be returned on :GetObject when there is no object to re-use.

:OnDestroyed(function(object)

Sets a handler for when :GetObject is called, and there is an object to reuse, this should be used to prepare an object for when it needs to be reused.

Despite the name, :OnDestroyed like stayed above, doesn't fire when an object is added to garbage, instead it fires on :GetObject if there's an object to re-use. Therefore, if you need to change things when an object is destroyed, change it before sending it to the garbage. Most use-cases won't require this to be a thing, therefore this is an optimization.

> Go to Best Practices

Clone this wiki locally