BaseObject
BaseObject provides interface methods for three core features:
- Object Destruction via adding a :Destroy() method and IsDestroyed flag property,
- Task Management across the objects lifetime by providing a janitor internally,
- and Signal Management by providing interfaces to Register, Get, and Fire signals easily.
Destroy Behavior:
- When a BaseObject instance is destroyed, it's
IsDestroyed
property is set to true, and it'sDestroyed
signal is fired. - It's metatable is set to a metatable that will error when any of it's methods or metamethods are called.
You should check IsDestroyed
before calling any methods on a BaseObject instance if you are not sure if it has been destroyed or not.
Types
BaseObject
Properties
ClassName
This item is read only and cannot be modified. Read OnlyBaseObject.ClassName:
string
Functions
getObjectFromId
staticFetches the object with the given ID if it exists.
local obj = BaseObject.new()
local id = obj:GetId()
print(BaseObject.getObjectFromId(id) == obj) -- true
new
staticConstructs a new BaseObject
local obj = BaseObject.new({
X = 1,
Y = 2,
})
obj.Z = 3
print(obj.X, obj.Y, obj.Z) -- 1, 2, 3
local SuperClass = setmetatable({}, BaseObject)
SuperClass.__index = SuperClass
SuperClass.ClassName = "SuperClass"
function SuperClass.new()
local self = setmetatable(BaseObject.new(), SuperClass)
return self
end
function SuperClass:Destroy() -- Overwrite the BaseObject Destroy method
getmetatable(SuperClass).Destroy(self) -- If you overwrite the BaseObject Destroy method you need to have this line to call the original.
end
function SuperClass:Print()
print("Hello, World!")
end
return SuperClass
Destroy
BaseObject:
Destroy
(
) →
(
)
Marks the Object as Destroyed, fires the Destroyed Signal, cleans up the BaseObject, and sets the metatable to nil/a special locked MT.
GetId
BaseObject:
GetId
(
) →
number
Returns the ID of the BaseObject Can be used to fetch the object with BaseObject.getObjectFromId(id)
IsA
BaseObject:
IsA
(
classOrClassName:
table
|
string
) →
boolean
Returns true if the given object is of a given class. Takes a class name or class object.
GetTask
BaseObject:
GetTask
(
taskId:
any
) →
Task?
Fetches the task with the given ID if it exists.
local obj = BaseObject.new()
local part = Instance.new("Part")
obj:AddTask(part, nil, "Test")
print(obj:GetTask("Test") == part) -- true
AddTask
BaseObject:
AddTask
(
task:
Task
,
taskCleanupMethod:
(
string
|
true
|
nil
)
?
,
--
(if none is given it will try to infer; Passing true tells it to call it as a function)
taskId:
any?
) →
Task
--
The task that was given
Adds a task to the janitor. If a taskId is provided, it will be used as the key for the task in the janitor and can then be fetched later with :GetTask(). If an ID is provided and there already exists a task with that ID, it will clean up the existing task and then replace the index with the new one. It will return the task that was added/given.
local obj = BaseObject.new()
local task = obj:AddTask(function()
print("Hello, World!")
end)
obj:Destroy() -- Prints "Hello, World!"
AddPromise
Adds a promise to the janitor. Similar to :AddTask(). Returns the same Promise that was given to it.
local prom = Promise.delay(math.random(10))
local obj = BaseObject.new()
obj:AddPromise(prom)
task.wait(math.random(10))
obj:Destroy() -- Cancels the promise if it hasn't resolved yet
RemoveTask
BaseObject:
RemoveTask
(
taskId:
any
,
dontClean:
boolean?
) →
(
)
Removes a task from the janitor. Cleans the task as if :DoCleaning was called. If dontClean is true, it will not clean up the task, it will just remove it from the janitor.
local obj = BaseObject.new()
local task = obj:AddTask(function()
print("Hello, World!")
end, nil, "Test")
obj:RemoveTask("Test") -- Prints "Hello, World!"
RemoveTaskNoClean
BaseObject:
RemoveTaskNoClean
(
taskId:
any
) →
(
)
Removes a task from the janitor without cleaning it.
local obj = BaseObject.new()
local task = obj:AddTask(function()
print("Hello, World!")
end, nil, "Test")
obj:RemoveTaskNoClean("Test") -- Does NOT print "Hello, World!"
FireSignal
BaseObject:
FireSignal
(
signalName:
string
,
--
The name of the signal to fire
...:
any
--
Arguments to pass to the signal
) →
(
)
Fires the signal with the given name, if it exists.
Equivalent to calling :GetSignal(signalName):Fire(...)
except this does not require
the signal to exist first.
local obj = BaseObject.new()
local SignalName = "Test"
obj:RegisterSignal(SignalName)
obj:GetSignal(SignalName):Connect(print)
obj:FireSignal(SignalName, "Hello, World!") -- Fires the signal with the argument "Hello, World!"
RegisterSignal
BaseObject:
RegisterSignal
(
signalName:
string
,
--
Name of signal to register
) →
(
)
Marks a signal with the given name as registered. Does not actually build a new signal, it sets the index to a SignalMarker to identify it as registered so that it can be fetched later. Can be given an existing signal to mimic.
local obj = BaseObject.new()
obj:RegisterSignal("Test")
obj:GetSignal("Test"):Connect(function()
print("Hello, World!")
end)
local myPart = Instance.new("Part")
local obj = BaseObject.new()
obj:RegisterSignal("Touched", myPart.Touched)
obj:GetSignal("Touched"):Connect(function(touchedPart)
print("Part Touched!")
end)
ConnectSignal
Connects a signal with the given name to a callback.
Alias for :GetSignal(signalName):Connect(callback)
.
local obj = BaseObject.new()
local SignalName = "Test"
obj:RegisterSignal(SignalName)
obj:ConnectSignal(SignalName, function()
print("Hello, World!")
end)
obj:FireSignal(SignalName) -- Prints "Hello, World!"
PromiseSignal
BaseObject:
PromiseSignal
(
signalName:
string
,
--
The name of the signal to connect to
predicate:
(
(
U...
)
→
boolean
)
?
--
An optional predicate to run before the callback. The promise will not resolve until the predicate returns false.
) →
Promise
<
U...
>
--
A promise that resolves when the signal is fired.
Connects a signal with the given name to a callback that will only run once.
Alias for object:AddPromise(Promise.fromEvent(object:GetSignal(signalName)))
.
local obj = BaseObject.new()
local SignalName = "Test"
obj:RegisterSignal(SignalName)
obj:PromiseSignal(SignalName, function(Letter)
return Letter == "B"
end):andThen(function(Letter)
print("Hello, World!")
end)
obj:FireSignal(SignalName, "A") -- Does not print anything
obj:FireSignal(SignalName, "B") -- Prints "Hello, World!"
obj:FireSignal(SignalName, "B") -- Does not print anything as it was disconnected
HasSignal
BaseObject:
HasSignal
(
signalName:
string
) →
boolean
Checks whether or not a signal with the given name is registered.
local obj = BaseObject.new()
local SignalName = "Test"
print(obj:HasSignal(SignalName)) -- false
obj:RegisterSignal(SignalName)
print(obj:HasSignal(SignalName)) -- true
GetSignal
Fetches a signal with the given name. Creates the Signal JIT.
GetDestroyedSignal
Returns a signal that fires when the object is destroyed. Creates the signal JIT. Kept for backwards compatibility.
local obj = BaseObject.new()
obj:GetDestroyedSignal():Connect(function()
print("Object Destroyed!")
end)
obj:Destroy() -- Prints "Object Destroyed!"
BindToInstance
BaseObject:
BindToInstance
(
destroyOnNilParent:
boolean?
--
Whether or not to destroy the object when the parent is nil'd
) →
function
--
Disconnects the binding
Binds the object to the given instance. When the object is destroyed, it will destroy the instance. When the instance is destroyed, it will destroy the object.
local obj = BaseObject.new()
local part = Instance.new("Part")
obj:BindToInstance(part)
do -- setup prints on destroy
obj:AddTask(function()
print("Object Destroyed!")
end)
part.Destroying:Connect(function()
print("Part Destroyed!")
end)
end
local X = if math.random(1,2) == 1 then obj or part
X:Destroy() -- Prints "Object Destroyed!" and "Part Destroyed!" (Destroying one will destroy the other)