Hello! ive been coding in OOP for a couple months now and its honestly a life changer. It changed my perspective and i use it very very often. But i have one problem, usually when i write in modules using oop. I would make a new class then write some methods specific to that object.
I want to do the same but differentiate between it kind of. For example I have 1 class called “fruits” and i want both of them 2 use the same method but do different things. Example: I make 2 classes and pass the string of the fruit name. I then called the method called eat, if the fruit was an apple i would spin the apple then destroy. But if its a banana i would teleport the banana up a million studs.
So basically 2 different functions but attached to the same module. This would help me alot since im diving into my desired topic of combat and it requires something like this but way more complex. Thanks!
Edit: I know it can be as simple as a if else statement but i want to future proof it for hundreds of fruits which means hundreds of different functions.
It wasnt really what i was looking for but thanks!
You could compnentalize the functions. Store them in a table then loop through all of them call a general :Eat() method. Then you can add and remove methods in this table.
But in the end its all if statements, the only difference is that instead of one long ifelse chain its in modules instead.
An example can be my OOP component attempt uses a generalized function like :Update(dt, data)
1 Like
You should look into inheritance/polymorphism with OOP. This allows you to create many sub classes which belong to a single super class, and the sub class can inherit methods from the super class through using setmetatable(). You can then create overrides for sub classes so that they run a different method to the super class. This is how it could work using the fruit example:
-- Super class: Fruit
Fruit = {}
Fruit.__index = Fruit
function Fruit.new(name)
local self = setmetatable({}, Fruit)
self.name = name or "Unknown Fruit"
return self
end
function Fruit:eat() -- This method will be overridden by sub classes
print("Default eat method (will run if not defined for a sub class)")
end
Apple = setmetatable({}, Fruit) -- creates a sub class inheriting from Fruit
Apple.__index = Apple
function Apple.new()
local self = setmetatable(Fruit.new("Apple"), Apple)
return self
end
function Apple:eat() -- override method :eat()
print("Specific method for eating apple")
end
Banana = setmetatable({}, Fruit) -- inheriting from Fruit again
Banana.__index = Banana
function Banana.new()
local self = setmetatable(Fruit.new("Banana"), Banana)
return self
end
function Banana:eat() -- banana eat override
print("Banana eat method")
end
-- Example usage:
local myApple = Apple.new()
local myBanana = Banana.new()
local unknownFruit = Fruit.new()
myApple:eat()
myBanana:eat()
unknownFruit:eat()
2 Likes
Hello, Im the creator of the post.
This is a bad example of inheritance, I lacked experience when I made that post ._.
1 Like
You could do it with a callback.
local Fruit = {}
Fruit.__index = Fruit
Fruit.new = function(name, eatCallback)
return setmetatable({name = name, eatCallback = eatCallback},Fruit)
end
function Fruit:eat()
print('Eating ' .. string.lower(self.name) .. '!')
self.eatCallback()
end
local apple = Fruit.new('Apple',
function()
print('yummy apple')
end)
local banana = Fruit.new('Banana',
function()
print('Yuck, banana!')
end)
apple:eat() banana:eat()
--[[
13:06:30.302 Eating apple! - Edit
13:06:30.302 yummy apple - Edit
13:06:30.302 Eating banana! - Edit
13:06:30.302 Yuck, banana! - Edit
]]
1 Like
Thats actually very helpful thanks alot
You have any new examples or a better way to do this?