How do I make a strong OOP system?

Hello. I am trying to put together an object oriented framework. My main question currently is how I can have attributes and methods pass down throughout different objects.

Right now, I have a main class, “entity”, which is comparable to Roblox’s Instance class. "entity’ contains attributes such as ClassName, and also contains a destroy method. If I were to call entity.new(“subclass”), how could I have roblox autocomplete fill in the attributes for the subclass and of the main entity class?
image

2 Likes

For me, I would do subclass.new() and that inherits from entity which would allow for autocomplete. I’m not sure if there are other ways but I’d love to see them if you find them.

Also if you’re interested in making strong frameworks/systems, have you looked into entity component systems? There are multiple open sourced ones already on Roblox that make it incredibly easy to make your own. There are also videos explaining the problems that ECS solves that OOP cannot.

To put it simply:
OOP → “IsA” type framework (strong but basically immutable entities with possible inheritance-hell)
ECS → “HasA” type framework (flexible and mutable entities)

3 Likes

I won’t go into detail but, if I understand the question then:

Inheritance can be done by

--in subclass module, require superclass and set it as metatable
local subclass = setmetatable({}, require(script.Parent))
subclass.__index = subclass

--TODO : Make class-constructor etc

If you are referring to having methods automatically be suggested as you type, this will sometimes partially happen automatically. It can be made more reliable by learning how to use export, type and typeof keywords, and maybe the --!strict command, although they have limitations and little documentation.

3 Likes

Ok I may have misunderstood, it seems you are using Entity as a factory for creating subclasses. I do not know how to make it have autocomplete for multiple subclass types from this method, it might not be possible honestly. I shall perhaps experiment tomorrow to see how smart the type-system is, but any solution would involve setting up types, which would have to be done manually whenever you add a new subclass.

2 Likes

thanks! your example might work well. i know i’m supposed to go composition over inheritance and blah blah all that, but i shouldn’t suffer any issues in this framework. also, thank you @debugMage for the ECS recommendation. i’ll check it out before i get going

3 Likes

Hey, thanks for the recommendation. I know it’s been a little while, but is this how it would work out?

I have an “entity” superclass, but all of the subclasses can use each other as components. The entity superclass has a :Destroy() method. In each subclass, if it wants to inherit from another, then one of the attributes would be e.g. “.Tires = Tires.new()”

Sorry for the late reply, I didn’t see the notification.

I honestly suggest avoiding inheritance and favoring composition instead. Creating a tire in an entity is very close to entity component systems already, so I’d give that a look if you’re curious.

Really this depends on your game. If you want a flexible and modular game, because it’d suit your game better, then favor composition. If you want to make a standard game that isn’t really flexible but strong then I suggest inheritance.

Flexible modular games: Sandboxes, high customization, easy mix and match capabilities, easy random elements.
Strong standard games: Having to predefine a lot of things. Having a very concise idea of what your classes are and how they will be used.

1 Like

I think I misspoke a tiny bit. Do you think it is fine if I have my class inherit from one superclass, but each class then uses each other for composition?

I think you may run into circular dependency issues.

Like A requires B while B requires A. There are hacky ways around this but I don’t recommend them.

1 Like

I have not had that issue so far and everything seems to run fine thankfully!