The Engine Lacks the Ability to Add New Behavior to Instances

As a Roblox developer, it is currently too hard to create individual objects that we can assign to an instance to give it extra functionality (can be considered as DOP or Data-Oriented Programming).


Why Components?

Components can help developers organize their frameworks, and make it easier for them to understand what a specific object does. Components also allow developers to edit variables within the components during runtime that alter what the component does. This system will give developers an extra way to design their frameworks, similar to how many of them already use OOP as their framework of choice.
Popular game engines such as Unity already have this feature implemented:

A topic about this feature:

https://devforum.roblox.com/t/would-it-be-possible-to-use-data-oriented-programming-dop-in-roblox/1440581


How Does it Work?

Components will work exactly like Roblox’s classes system; an ImageButton inherits from the classes: GuiButton, GuiObject, GuiBase2D, and Instance. Thus these classes add on to what the ImageButton actually does (displays an image on a frame).

The interface for inserting different components would be similar to adding attributes:

When the “Add Component” button is clicked, it will bring up a menu with the same design as the Insert Object menu but instead of every instance, it contains every component. Component names derive from the name of the ComponentScript that the component runs on.

To create a component, insert a ComponentScript into a new service: ComponentService:

--// In the ComponentScript named "Hitblox"

component = { -- "component" is a new global used specifically in component scripts
    -- This is where the component variables are stored
    Damage = 10
}

reference.SwordBlade.Touched:Connect(function(hit) -- "reference" is a variable of the instance that the component is attatched to
    if hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid:TakeDamage(component.Damage)
    end
end

In runtime, you can edit the component like this:

local Sword = workspace.Sword
local SwordBlade = workspace.SwordBlade

SwordBlade.Damage = 100 -- Just like how you would edit an instance's property

Why not CollectionService?

Though CollectionService allows you to assign “tags” which you can access the tagged instances and assign functions to them, it doesn’t provide a visual representation and it’s uneditable from other scripts.


If Roblox would add this feature it would improve my development workflow drastically as I’d prefer using small script chunks that I can add to my objects than full-fledged frameworks that can be hard to determine what it does and where it runs.

31 Likes

Not sure I exactly understand why ModuleScripts aren’t good enough? They’re perfectly fine with returning simple tables and requires are 1 line of code.

What exactly is the difference between the two?

5 Likes

The link I provided explains more about components:

The difference is that you cant add a module script to an instance to add functionality to them. But adding a component gives it functionality immediately.

3 Likes

You should title the thread after the problem rather than the proposed solution. Someone browsing feature requests sees “Add a Components Feature” and has no idea about what problem the thread is addressing. It makes your topic harder to surface and find back via search.

ref: How to post a Feature Request

3 Likes

This is very true, the current practice to extend instances adopted by most devs I come across is using master tables keyed by some instance and the value being the component data. But you end up with numerous of these modules all over the place and the data for an instance becomes decentralized across multiple modules creating a mess.

Currently I use a global module of sorts to store components of each instance and each instance has a Components script that has a table of component instances and registers the components into the global module to be accessible globally, which helps to keep components centralized.

If this were an engine feature, life in terms of being able to write maintainable and extensible code would be so much easier. Many engines nowadays (Take the likes of Unity or Godot) encourage a component-based approach and it has proven to work well.

6 Likes

I would even go as far as to say it’s more natural to have these components as sort of custom instances rather than a section under the properties panel. Just as one would add BodyVelocity instance to a part to give it additional behavior, you’d be able to create your own custom instances such as EquipableComponent, HitInteractableComponent, DroppableComponent etc.

3 Likes

Big support for this. I don’t like cooking up my own solutions to a problem that should’ve been solved by the game engine, which would provide much better experience.

However, I do not think that this will ever be added since Roblox can’t make major changes to the engine as there is no going back after something is added, and removal isn’t an option either. At most what you can do is implement this yourself, which is not pleasant but this is what we got. Another option is to move to another game engine, which probably won’t work for majority of the developers that develop on Roblox, including myself.

3 Likes