How should I use OOP?

Hey, so I know about to use OOP and all, but just curios how to use it. \/

  • When should I use it?

  • Inheritance or Composition?

  • What is it most useful for?

  • What do most people use it for?

  • Some in game examples

  • Practical usage


1 Like

This article can answer your questions I think ‘All about Object Oriented Programming’. I could answer each question, but I think that learning about OOP for ROBLOX will teach you much more.

1 Like

I know how to use it, I just want to know the following. \/

  • When should I use it?
  • Inheritance or Composition?
  • What is it most useful for?
  • What do most people use it for?
  • Some in game examples
  • Practical usage

Pretty much whenever possible I guess.

I personally prefer composition in Lua, eventhough I like the idea of inheritance.

For making entire systems that can consist of classes.

For writing readable, logical and maintainable code.

Here is the structure of something that I am working on (based on OOP):

afbeelding

1 Like

What would be the benefit of making an oop car system rather than say making values inside the car that know the speed or drift etc. then making a script for that car that that handles all of that stuff?

Logical code, maintainability and readability.

Actually, I do this myself too sometimes. This is not directly related to OOP, but rather a design choice.

Yeah, I’d like to know this. Right now I’m super dependent on Values and BindableEvents. It works well enough, but I heard some people say it’s a bad practice.

I know this could be a stupid question, but I haven’t looked into OOP at all because I’m lazy. Does that mean that the only drawbacks of my current solution are the things you listed? If I already have systems that don’t use OOP, would it be worth the effort to convert everything into OOP once I get the hang of it?

1 Like

That is completely up to you. OOP mainly prevents you from writing scripts that are bigger than 100 lines of code. You can test your knowledge of OOP this way that is for sure.

2 Likes

Only use OOP if you need multiple objects to share similar functionality but also need to share their own states. This can be done without OOP but storing physical value objects has some limitations such as not being able to use tables or functions in each object’s state.

Composition is the more ideal method because it lets you take two classes that are vastly different in overall purpose and have them share code for the things that they do share. Which follows DRY (Don’t repeat yourself). An example is a car and a boat. They both need an engine but the car will use it to move the wheels while the boat will use it to move the propeller. So it would be best if they both sourced their engine from an engine class so they can share functions.

Similar answer to my response to the first question, but it allows you to create multiple objects that share similar functionality with other objects but allows each to have it’s own states.

Managing large amounts of objects that share a similar code base, going off of the “Don’t Repeat Yourself” principle.

Just know OOP isn’t useful for all scenarios and you could make games without it. Lua doesn’t really support OOP as compared to other languages and I don’t think Roblox is planning on improving it in LuaU because it’s really not important when things such as Attributes and CollectionService exist. Some programmers will want to use it and others probably thing it’s over hyped.

5 Likes

Alright thank you for the informative response. Btw I literally just got hacked not even joking. Lucky I reset my pass fast enough! :smile: