Recently I have learned how to use OOP, but I still have trouble knowing when it’s appropriate to use OOP. Say, I’m making a gun system with multiple guns. Do I use OOP for that, or do I just hard code the different weapons? I don’t know when to use OOP. Any help is appreciated. Thank You!
It’s always good to use modularity for anything you make. In my case, my relative experience to your post would be making an extremely modular 3PP gun engine. I have a scripts for server side, local, etc, and a their respective modules. I have a config in each gun that determines how it works, implementing OOP.
Overall, for systems that you would possibly want to expand on in the future, it is always good to implement OOP for increased modularity and ease of use in the future.
Let me know if this helps!
I say that the only cases where OOP is not appropriate to implement is when it’s benefits will not exist. I’ve found that emulating OOP in Luau only provides the benefit of inheritance (ie. reusing code) and polymorphism (ie. having reused code be flexible enough to get exactly what you want), so, in general, if the use case you come across can make use of them, that is when you should use OOP.
One example system I wouldn’t use OOP on is an input system. There’s no need for me to come up with different ways to record a player’s inputs. They’re either pressing a key or they’re not. Inheritance and polymorphism will not help me here.
A system where I would use OOP is the one you’ve presented in this post (ie. guns). With inheritance, you can write a rapid-fire firing method and you’ve just scripted part of every single automatic weapon you’ll make. Through polymorphism, you might control the fire rate with a variable. And you can create another class for each individual gun and yet another class for any reskins you’d like to make.
In conclusion, use OOP when you can make use of the 2 pillars that can be used in its emulated form in Luau. The gun system you’ve presented is one of them.
I sort of know how to use OOP, but I don’t enough to make a full out system with it. I made a basic gun system this morning without OOP, and its very easy to add weapons and customize the stuff, but should I quit what I’m doing right now and restart using OOP? Or just keep working on this system and make another one later when I understand OOP more? What would be more beneficial?
If you think adding and customizing stuff without OOP is easier, then go for it. Luau is not designed heavily around OOP, so as long as you’re confident your style can cover all the advantages you want from OOP and you’re more comfortable with it, I see no practical reason to why you should restart using OOP.
As for whether or not you should make another system based around OOP later, I personally would be kind of annoyed about the discrepancy between the different styles of code. It’s kind of like a game with terrible artwork at the beginning of its story that the developers never bothered to update as the team’s artists got better for the later parts. If that doesn’t bother you, continuing with your current system and tacking on some OOP principles later on is perfectly fine.
All in all, since Luau is built off such a flexible programming language (ie. Lua), there are multiple ways to get the same benefits. The more beneficial option, from a practical standpoint, is whichever one can give you everything you need with as little effort as possible.
Well I made my gun system very efficiently(imo) all I have to do is add the new view model into a folder and add its name to a table and it works like every other view model so I see no point in implementing OOP