Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use OOP in actual game logic?

Asked by
QWJKZXF 88
2 years ago

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?

1 answer

Log in to vote
1
Answered by
Speedmask 661 Moderation Voter
2 years ago
Edited 2 years ago

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

1
I am still a bit confused, you mentioned your Animation Controller and how you used a custom function to deal with the animations by using OOP. But what is the difference between using just a module script and creating a function that does the exact same thing rather than having to create a new class? QWJKZXF 88 — 2y
0
this is because every enemy has their own animations. by keeping an object, it stores the animations and the model etc. inside of itself. that way I can call Play multiple times on the same animations until it dies or something. using a simple function on a modulescript, it doesn't really "belong" to anything and cannot store data within itself, it would have to be a parameter. Speedmask 661 — 2y
0
technically you could just store the functions and give the object as one of the parameters, but that's just OOP split up and complicated further. it does not respect encapsulation. like this, the data and functions are neatly tied together and using Class.new() the table structure is always the same (e. g. it is guaranteed to contain animations and some other stuff). Speedmask 661 — 2y
0
I can tell why you are confused though, that's because what you're describing is pretty much already half-baked OOP :) Speedmask 661 — 2y
Ad

Answer this question