Binder
Bind class to Roblox Instance
-- Setup a class!
local MyClass = {}
MyClass.__index = MyClass
function MyClass.new(robloxInstance)
print("New tagged instance of ", robloxInstance)
return setmetatable({}, MyClass)
end
function MyClass:Destroy()
print("Cleaning up")
setmetatable(self, nil)
end
-- bind to every instance with tag of "TagName"!
local binder = Binder.new("TagName", MyClass)
binder:Start() -- listens for new instances and connects events
Types
BinderContructor
type
BinderContructor =
(
...:
any
)
→
T
|
{
new:
(
...:
any
)
}
|
{
Create(self
,
...:
any)
}
Constructor for a binder
Functions
new
Binder.
new
(
tagName:
string
,
--
Name of the tag to bind to. This uses CollectionService's tag system
...:
any
--
Variable arguments that will be passed into the constructor
) →
Binder
<
T
>
Constructs a new binder object.
local binder = Binder.new("Bird", function(inst)
print("Wow, a new bird!", inst)
return {
Destroy = function()
print("Uh oh, the bird is gone!")
end;
}
end)
binder:Start()
isBinder
Binder.
isBinder
(
value:
any
) →
boolean
true
or
false,
whether
or
not
it
is
a
value
Retrieves whether or not the given value is a binder.
Start
Binder:
Start
(
) →
(
)
Listens for new instances and connects to the GetInstanceAddedSignal() and removed signal!
GetTag
Binder:
GetTag
(
) →
string
Returns the tag name that the binder has.
GetConstructor
Returns whatever was set for the construtor. Used for meta-analysis of the binder, such as extracting if parameters are allowed.
ObserveInstance
Fired when added, and then after removal, but before destroy!
GetClassAddedSignal
Returns a new signal that will fire whenever a class is bound to the binder
local birdBinder = Binder.new("Bird", require("Bird")) -- Load bird into binder
birdBinder:GetClassAddedSignal():Connect(function(bird)
bird:Squack() -- Make the bird squack when it's first spawned
end)
-- Load all birds
birdBinder:Start()
GetClassRemovingSignal
Returns a new signal that will fire whenever a class is removing from the binder.
GetClassRemovedSignal
Returns a new signal that will fire whenever a class is removed from the binder.
GetAll
Binder:
GetAll
(
) →
{
T
}
Returns all of the classes in a new table.
local birdBinder = Binder.new("Bird", require("Bird")) -- Load bird into binder
-- Update every bird every frame
RunService.Stepped:Connect(function()
for _, bird in pairs(birdBinder:GetAll()) do
bird:Update()
end
end)
birdBinder:Start()
GetAllSet
Binder:
GetAllSet
(
) →
{
[
T
]
:
boolean
}
Faster method to get all items in a binder
local birdBinder = Binder.new("Bird", require("Bird")) -- Load bird into binder
-- Update every bird every frame
RunService.Stepped:Connect(function()
for bird, _ in pairs(birdBinder:GetAllSet()) do
bird:Update()
end
end)
birdBinder:Start()
warning
Do not mutate this set directly
Bind
This item only works when running on the server. ServerBinds an instance to this binder using collection service and attempts to return it if it's bound properly. See BinderUtils.promiseBoundClass() for a safe way to retrieve it.
warning
Do not assume that a bound object will be retrieved
Unbind
This item only works when running on the server. ServerUnbinds the instance by removing the tag.
BindClient
This item only works when running on the client. ClientSee :Bind(). Acknowledges the risk of doing this on the client.
Using this acknowledges that we're intentionally binding on a safe client object, i.e. one without replication. If another tag is changed on this instance, this tag will be lost/changed.
UnbindClient
This item only works when running on the client. ClientSee Unbind(), acknowledges risk of doing this on the client.
Get
Returns a instance of the class that is bound to the instance given.
Promise
Returns a promise which will resolve when the instance is bound.
Destroy
Binder:
Destroy
(
) →
(
)
Cleans up all bound classes, and disconnects all events.