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!