Conflicted on the idea of a flat Instance API. What is your stance on the existence of components anyway?

A lot of Luduvo’s Instance API is flat. There are exceptions (namely fields from “custom” components like instance.Tool.gripOffset) but almost all properties and methods from components of all shapes and sizes live directly on the Instance.

Some things seem fine, like color:

self.Color = Vector3.new(x, y, z)

But things get strange and pretty wordy when there starts to be overlap between components:

self.OnColor3 -- Without context, some of these names feel nonsensical
self.BoxColor3
self.DotColor3
self.FogColor
self.OffColor3
self.TabColor3
self.BodyColorHead
self.BodyColorLeftArm
self.BodyColorLeftLeg
self.BodyColorRightArm
self.BodyColorRightLeg
self.BillboardColor -- They also don't have consistent naming
self.HighlightColor3
self.SelectionColor3
self.BackgroundColor
self.BackgroundColor3 -- And this seems identical to the one before it
self.TextStrokeColor3
self.DirectionalColor
self.MultiSelectColor3
self.SelectedTabColor3
self.AmbientLightColor
self.ProgressTrackColor3
--... and this isn't even all of it!

Methods have the same problem, as not even autocomplete will be able to help you make heads and tails of what API corresponds to which component(s). For example, could you tell me what self:Pause, self:Play, and self:Resume operate on? If you guessed timers, you’d be “correct”. They don’t seem very obvious, right? Except I lied, and those 3 APIs are actually for controlling sound. But what if that’s also a lie? It’s not (it really does control sound), but the point is that you wouldn’t be able to tell if I was telling the truth just by looking at it. Another example of unclear APIs is IsAlive, which might seem like it checks whether a character is dead via its health component. Nope, it doesn’t care about the health component at all. Instead, it just generally checks whether the entity exists, regardless of if they are a character with a health component or a random block from one of your builds.

Now, whether the API names are coherent isn’t the problem I have with the flat APIs. Most of the API has appropriate naming, and the ones that don’t can be fixed without actually unflattening anything. Having everything directly available like this can be convenient too, assuming that you have an LSP capable of giving hints and autocorrect. My bigger problem is that this flat API doesn’t help teach one of the most important parts of Luduvo’s API: components.

At some point in your scripting career, you will probably use AddComponent, RemoveComponent, HasComponent, game.World.Each, game.World.Query, or game.World.System. If you are reading this nerdy Help & Feedback > Engine API post, you probably already have. But regardless, these APIs need component names. How do you learn which component owns a property when you’ve only ever accessed it directly on the Instance? You might not even realize that instance properties use components in the first place!

Queries make that gap more noticeable. Let’s say that you wanted to make a weather system and make everything that’s not a player slippery or something by changing their Friction property all at once. This is a job that game.World.Query was made for!

Ok, but which component owns Friction? You need to know which component to query so you can access and change it. Perhaps Physics? Nope. The more refined gentlepeople in the crowd who know their way around the block might guess something like RigidBody or SurfaceMaterial. Also no. Maybe SurfaceOverride since that’s the name friction falls under in the properties tab? Close, but no. You should know by now that the proeprty tab lies about everything and makes things up whenever it wants to (duh-doi!). It’s actually this:

local parts = game.World.Query("PhysicalSurfaceOverride"):Without("Character")

function Update(dt)
    parts:Refresh()
    for i = 1, parts.count do
        local friction = parts.PhysicalSurfaceOverride.Friction[i]
        -- Then do something with that stuff here
    end
end

You would never know this, because you never even once had to use it since you could always just do self.Friction instead. I bet without me telling you this, you also wouldn’t immediately know that this PhysicalSurfaceOverride also contained the values needed for changing Density and Resitution. Since the API is flat, you won’t have any idea how data is actually structured in Luduvo!

Queries are also used in systems, which are another tool that you might find yourself needing:

local query = game.World.Query("PhysicalSurfaceOverride"):Without("Character")

game.World.System("Weather Nonsense", query, function(parts)
    parts:Refresh()
    for i = 1, parts.count do
        local friction = parts.PhysicalSurfaceOverride.Friction[i]
        -- Then do something with that stuff here
    end
end)

And instance:AddComponent will also return similarly formatted components after you use them:

local surfaceSettings = self:AddComponent("PhysicalSurfaceOverride")
surfaceSettings.Friciton = 0.123

None of this is impossible to learn with how things are right now, but now you need to know the component name, which fields it exposes, and how you are supposed to use the component when the time comes (field access like this is only one of many ways component values can be formatted). None of your knowledge from using the flat instance API would transfer when it comes time to using said component in any other API.

But with all that said, there’s something I left out, which is why I’m “conflicted” and not “spiteful” about this flat API. Even with all this said, you could probably make any game you want (though maybe with a bit of added difficulty) without even touching systems, or queries, or anything that’s not instance component management or game.World.Each.

The systems/queries example shown earlier is somewhat contrived for 2 reasons:

  1. You can access Instances and continue using the flat API all you want because queries also provide the specific escape hatch that gives the instance it match with via query.Instance[i]
  2. Many components will error if you try to index them in a query by saying: 'ComponentName' has no column value; read properties through the 'Entity' column instead (them saying to use the “Entity” column instead of “Instance” column is a bug). So that really just means that in some cases, query.Instance[i] it is actually the intended way to manage a component and it’s not just an escape hatch

So with those two things alone, it’s a very real possibility that you aren’t really supposed to be using components like that anymore and you should just use the flat API for mostly everything.

And that’s fine, but you have to commit to one or the other. Right now it’s just kind of a weird mix of both that makes it the worst of both worlds. Learning the convenient Instance properties doesn’t help you when you need to know the component’s name and how to use its specific component API, and learning to use the component APIs doesn’t help because sometimes you are just forced to use the instance properties instead. By some means or another, there needs to be greater continuity across APIs.

Personally, I’d prefer more properties to be organized like tool, where things follow a instance.ComponentName.Property format and the same field names are available when queries or AddComponent returns a component proxy. Methods specific to specific components like TakeDamage() should join them inside the component proxy too (i.e instance.Health:TakeDamage()).

Though not everything should be nested just for the sake of purism. Things that manage things specific to the Instance like AddComponent and GetSignal, or Parent and IsDescendantOf should stay directly on the Instance. Methods like GetMass() that don’t have a clear home about where they belong because it reads/modifies multiple components could also stay in Instance until someone figures out a better solution :p

If anyone else has thoughts on this, I’d like to hear them too

i don’t HATE the flat instance api… it’s kind of annoying how Position and other stuff have multiple types depending on the instance itself (e.g. a part has a Vector3, a ui object has UDim2)

it would be so much better if:

  1. the autocomplete wasn’t GENUINELY horrible and only checks to see if it matches the start of the word
  2. there was autocomplete for components, for AddComponent, HasComponent, and Query i guess

This is like the time where i emptied 5 boxes into 1 and lost track of everything :joy: :joy: :joy: