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!
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.
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.