NOTE: The following instructions are for ActorPool_v3.
- Create a new
Actor
. - Add a
Script
to saidActor
. The code inside of theScript
should follow the boilerplate below. - Add a
BindableFunction
calledRunEvent
to theActor
, - (Optional) If you want to return data from your Actor then add a
BindableFunction
calledReturnEvent
to theActor
. IfReturnEvent
exists then you need to fire it at the end of your actors code.
Script Code Boilerplate
local RunEvent, ReturnEvent = script.Parent.RunEvent, script.Parent.ReturnEvent
RunEvent.Event:ConnectParallel(function(...)
-- Your code here
-- To return data from an actor do:
-- ReturnEvent:Fire(...)
end)
local ActorPool = require(game:GetService("ReplicatedStorage").ActorPool)
local myPool = ActorPool(baseActor:Actor, actorsFolder:Folder, amount:number)
baseActor
= The actor of which all actors in your pool will be a clone of. This actor will not be included in your pool.
actorsFolder
= The folder where all of your actors that are in your pool will be parented to.
amount
= The amount of actors to initially create.
When using the method below you get returned a connection
object.
local myActorFromPool = myPool:take(autoPutBack:boolean?)
autoPutBack
= If this is true
then after :run
is called on the actor, said actor will automatically be returned to the pool.
myActorFromPool:run(...)
...
= the arguements to send to the Script
inside of the actor from the pool.
myActorFromPool:runAsync(...)
...
= the arguements to send to the Script
inside of the actor from the pool.
myActorFromPool:putBack()
The below method waits until a specified Actor has finished with whatever work/task they were doing.
myActorFromPool:waitUntilFree()
The below method waits until a specified Actor has finished with whatever work/task they were doing.
myActorFromPool:waitUntilFreeAsync()
Example
myPool:take(true):waitUntilFreeAsync():andThen(function(self)
self:runAsync(1):andThen(print)
end)
Please note that in most circumstances using a different connection (actor) from the pool is preferred over using the same connection.
If you are not using promises then reusing actors is simple.
local conn = myPool:take()
print(conn:run(1))
print(conn:run(2))
However if you are using promises then you need to make sure that you use the :waitUntilFree()
method to make sure that the actor/connection is available to do more work.
local conn = myPool:take()
conn:runAsync(1):andThen(print)
conn:waitUntilFree()
conn:runAsync(2):andThen(print)
A more sophisticated approach would use the :waitUntilFreeAsync()
method instead.
local conn = myPool:take()
conn:runAsync(1):andThen(print)
conn:waitUntilFreeAsync():andThen(function()
conn:runAsync(2):andThen(print)
end)