Implementing OOP in Roblox

Hello fellow devs, I have a few questions today about overall programming methodologies.

How should you go about structuring your games codebase when using OOP and modularity?

I have been using OOP for a while now, and it is insanely helpful. I find myself having to use it fairly often in all my projects, but there are a few issues that I run into.

When coding games in general, you want to be as efficient as possible. That means not writing duplicate code. But that seems to conflict with one of OOPS core concepts being encapsulation. For example:

Say you want to handle player detection within a boat. Once you make your class, following OOP, you would want to handle player detection within the boats class, and have it abstracted away into said class.

Now say you have another class named NPC, and it handles all the functionality for NPCS in your game. Again, following OOP, you’d handle player detection in the NPC’s class.

But now you’ve opened a runservice loop for every boat, and every npc.

It would be more efficient to have one loop in itself that handles all player detection for all classes in the server. But this goes against the core principals of OOP

And so it does lead to duplicate functions as your doing the exact same thing for each class.

This is just an example, but this goes for other aspects when developing as well.

My other question is, how do you stop your classes code from getting messy? For instance, one of my classes is 200 lines long, and it has many methods and variables. It just looks messy and is hard to read. How abstracted should classes be? Should I create sub modules for certain chunks of the classes code? Or should I leave it how it is now?

Anyways, I would just like to hear what you guys have to say. Currently, I always use one client and one server script, and then everything else is in modules and I use classes where I need them.

Also how do you organize your classes? For instance, I have an airship class, a glider class, a player data class, and then a townmanager class that has an NPC subclass. But then I think that maybe NPC should be its own class, but how would that work as the ai for the npcs that populate each town is vastly different than the ai for a boss or enemy NPC would be. But then inevitably you end up with duplicate code as they all share common methods such as walking.

It’s just all so confusing, if you read all this, congrats! Your amazing! Aight, have a good one guys, I hope this sparks a meaningful discussion!

1 Like

For the looping part I made a module that handles all callbacks binded to each event into one loop for each runservice connection.

RunService binds - Resources / Community Resources - Developer Forum | Roblox

This implies that you should make a playerdetector object that both the npc and boat use internally. Not inherited from, but composed from.

You should abstract a class only when you start needing it more than once imo. Otherwise it might just be a wasted effort. That or it obviously needs the abstraction because it simply wouldn’t make sense any other way.

Absolutely. If something can be self contained in a class, even if it’s only a piece of that class, having sub modules can really help with organization by simplifying the higher level code.

While this may or may not perfectly align with oop principles, not every part of your code needs to be tied to classes. You can use outside functions that are self contained as utility modules. And you should ideally favor composition over inheritance.

very nice, ill check it out. But making an object that handles all detections goes against the core principals of OOP.

Fair enough. I guess that you cannot go entirely on OOP principals. That is actually what I am doing right now for detection client side. I have a “Detections_Class” and then subclasses for each type of detection gets made and then integrated into the runservice loop in the Detections_Class

1 Like