I am trying to learn OOP (Object-Oriented-Programming) for a long time, but never understood its purpose or how to use it at all. Examples I have seen in tutorials mostly create an imaginary class, whether it be a person or a type of food, and set properties and methods for it.
What I don't understand is, what is the difference between using OOP like how I had mentioned above versus using a module script and just putting a bunch of functions that can be referenced in other scripts?
Can someone please provide me a practical example of how OOP is applied to an actual game?
well why do you think modulescripts exist? that's already half of it, organizing what you can into tables. here are the often heralded four principles, I won't explain them in detail but this is where you should start.
the only thing behind it, the holy grail of programming is: scalability. in roblox and game development especially, this is huge. what's scalability? it means that the bigger your game gets, it doesn't get any harder to write. without scalability your game is just gonna get so complicated that you will give up on it.
code reuse
okay, so that's already pretty obvious. using metatables, at least, you can just use the exact same function over and over without wasting space. in the same context, using inheritance you can just make one thing build off of another and use the same values. in the long run you'll be writing way less lines than you would have without it.
abstraction
abstraction means making underlying stuff simple. imagine you have like, 1000 lines of something super complicated. without obfuscation you will have a ton of functions and won't even remember what the hell you wrote. with obfuscation you still don't remember what the hell you wrote but that doesn't matter because you have a shiny, easy object:Calculate()
function.
encapsulation
encapsulation means keeping the objects as less related as possible. when everything is related to each other then editing one thing becomes a mess. going back to that 1000 line calculation, you will have to go all over the place adding maybe two words. however, making it broad allows it to be used flexibly and you will only have to edit a few places. using Class.new(x, y, z)
you are basically opening up a ton of functions to data derived from three of the same arguments which can also constantly change. that's pretty flexible if you ask me.
example
I'll give you an example of an animation controller I made. it's common knowledge that any animations should happen on the client, but I had enemies that need to be animated from the server. it would get complicated to constantly fire remotes every time I wanted to switch animations. however, using a special animator object I made, all I have to do is Enemy.AnimationPlayer:Play("Walk")
and my object automatically shoots remotes and replicates it to all clients easily. I don't have to worry about that at all anymore!
if there's one thing to take away from this essay it is this: LESS THINGS TO KEEP TRACK OF = GOOD