Where is OOP useful and how is it used?

Hi there,

Recently I learned to use metatables and metamethods and how you can implement a system of inheritance. However I just can’t seem to identify where it should be used.

I like how OOP keeps code organized and structured in a neat manner. I can’t always seem to convert my logic into OOP but I can do it easily without it. For example, I made a Tycoon framework without using any OOP but when I try to do an OOP version I end up doing things how I do them usually.

Can someone give me some advice? Examples of where OOP is useful are also appreciated!

1 Like

There is a good article on how it is good: https://www.roberthalf.com/blog/salaries-and-skills/4-advantages-of-object-oriented-programming

Here’s how you make an object (called table in lau):

local object = {
    a = {
        b = 1,
        c = “nice”
    }
}

So, object.a.b will be 1, and object.a.c will be “nice”

Typing in markdown on an iPad is a pain in the butt lol

1 Like

I just learned OOP yesterday, and I don’t have a really good explanation of what it is, but basically, if you wanted a special bunny making code piece, you would code:

local Bunny = { -- this is going to be the default bunny thing
 Fur = "Pink",
 EyeColor = "Blue",
 Hostile = false

}

function Bunny:New(Fur, EyeColor, Hostile)
 local Object = {}

 Bunny.Fur = Fur
 Bunny.EyeColor = Eyecolor
 Bunny.Hostile = Hostile

 self.__index = self
 setmetatable(Object, self)
 return Object
end

function Bunny.DyeFur(newFurColor, self) -- self means the Bunny variable
 self.Fur = newFurColor
end

If you want to learn from a person with an actual knowledge of OOP, check out Lua Learning Lua Learning - Roblox and search Intro to OOP.

1 Like

If you have use for encapsulation, abstraction, inheritance and polymorphism go for it. If you’re developing a component of a game, you can consider it. Making a system out of OOP is probably not the way to go. Check out ECS if you’re interested in making a good system.

1 Like

Thank you all.

@AE_TG Mhm I did go over that yesterday as well, I will check out the tutorials on the game now. Thank you :slight_smile:

@GUNSGOBRIBRl Yes, I think there isn’t a way of doing everything using OOP I guess you have to mix it up with other paradigms as well.

glad i could help :slightly_smiling_face: