Components is a brand new plugin that allows you to extend existing objects in your experience with custom behavior or functionality.
Those who have used other engines may be familiar with the concept already, but it’s a great way to create and re-use common components that make up your experience and share them with other developers.
Each component is represented by a BindableEvent which allows you to view them in the explorer window and interact with them from other components or systems via the Roblox API.
You can install the plugin by following this link or you can add it in Studio by searching for “Components” under the plugins section of the toolbox.
Features
The plugin currently supports the following:
- Add custom behavior to instances in your project
- Insert and view components in the explorer
- Communicate directly between components and other systems
- Custom icons to tell components apart
In the future, these features will be added:
- Component benchmarking
- ComponentService API for adding/removing and fetching different component types
- Running components in Studio
Examples
Lava
The Lava component has a damage attribute which allows you customize how many times the player can touch the brick before they die.
local Component = {}
function Component:Initialize()
self:SetAttribute("Damage", 50)
end
function Component:OnStart()
self.touchedConnection = self.Parent.Touched:Connect(function (hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid and not humanoid:GetAttribute("Damaging") then
humanoid:SetAttribute("Damaging", true)
humanoid:TakeDamage(self:GetAttribute("Damage"))
task.wait(1)
humanoid:SetAttribute("Damaging", false)
end
end)
end
function Component:OnStop()
self.touchedConnection:Disconnect()
end
return Component
Spinner
The Spinner components makes the part its parented to spin around a set number of times per second.
local Component = {}
local RunService = game:GetService("RunService")
function Component:Initialize()
self:SetAttribute("Speed", 1)
end
function Component:OnStart()
self.renderConnection = RunService.RenderStepped:Connect(function (step)
local theta = math.pi * 2 * self:GetAttribute("Speed") * step
self.Parent.CFrame *= CFrame.Angles(0, theta, 0)
end)
end
function Component:OnStop()
self.renderConnection:Disconnect()
end
return Component
Documentation
Component
Initialize()
Called the very first time a component is created. You should use this to setup the initial state of your component.
function Component:Initialize()
self:SetAttribute("Example", true)
end
Lifecycle
Each component has a number of methods which are called throughout its lifecycle. Typically the lifecycle of a component looks like so:
OnAwake → OnEnable → OnStart → OnStop → OnDisable → OnDestroy
Generally speaking, all your code should go into OnStart and OnStop unless you want to run some code when the game isn’t running or before the component is enabled.
function Component:OnStart()
print(self, "is running")
end
function Component:OnStop()
print(self, "has stopped running")
end
OnAwake()
Called when the component has just been created
OnEnable()
Called when the component has been enabled but is not yet running
OnStart()
Called when the component starts running
OnStop()
Called when the component stops running
OnDisable()
Called when the component is no longer enabled
OnDestroy()
Called when the component is being removed
OnMessage(message, …)
Called when another script calls ‘Fire’ on the BindaleEvent instance.
function Component:OnMessage(message, ...)
if message == "Shout" then
print("HELLO WORLD!")
end
end
Parent
Returns the parent instance of the component
self.Parent
Variables
Variables can be assigned to components directly, allowing you to maintain runtime variables.
function Component:OnStart()
self.startTime = os.clock()
end
function Component:OnStop()
print("Component was alive for", os.clock() - self.startTime)
end
GetAttribute(name)
Gets the attribute of the given name and returns its value
self:GetAttribute("Example")
SetAttribute(name, value)
Sets the attribute of the given name to the given value
self:SetAttribute("Example", true)
GetAttributes()
Returns a table of all of the components attributes
self:GetAttributes()
GetAttributeChangedSignal(name)
Returns an RBXScriptSignal which can be connected to, to listen for attribute changes.
self:GetAttributeChangedSignal("Example"):Connect(function ()
-- do something
end)
ComponentService
Coming soon
Changelog
- Added dev-forum link for documentation