Hello there,
My game relies on OOP to be fully functional. That being said, I had a few questions about OOP.
QUESTION 1 - METATABLES OR FUNCTION CONTROL FLOW?
TL;DR:
Is it better to use metatables over casually inserting functions into the object table?
Unfold me for details & examples
I wanted to know which technique was better than the other:
Technique 1 - Metatables
local Class = {}
Class.__index = Class
function Class.new()
return setmetatable({}, {__index = Class})
end
function Class:DoSomething()
--code
end
(inheritance):
local Subclass = setmetatable({}, {__index = Class})
Subclass.__index = Subclass
function Subclass.new()
return setmetatable({}, {__index = Class})
end
function Subclass:DoSomething2()
--code
end
function Subclass:DoSomething()
Class.DoSometing(self)
--code
end
Technique 2 - Function control flow
local Class = {}
--In case of concrete class
function Class.new()
local new = {}
new.DoSomething = Class.DoSomething
return new
end
--In case of abstract class
function Class.inherits(new)
new.DoSometing = DoSomething
end
--Static
function Class.DoSomething(self)
--code
end
(inheritance):
local Subclass = {}
--In case of concrete
function Subclass.new()
local new = {} --Or if concrete superclass, then initializes as Class.new()
Class.inherits() --In case of abstract superclass only
new.DoSomething2 = DoSomething2
new.DoSomething = DoSomething
return new
end
function DoSomething2(self)
--code
end
function DoSomething(self)
Class.DoSomething(self)
--code
end
Any difference in performance? One uses metatables, and the other controls which function enters the table.
QUESTION 2 - WRAP THE CUSTOMIZED OBJECT ON A ROBLOX INSTANCE
Ok so, it’s farily simple. My classes are wrapping already-existing instances. I wanted to know how to actually BIND the table to the instance. The current setup I have is that I store objects in a table in this way:
type Object = {
Instance: Instance
}
type StoreTable = {
[Instance]: Object
}
(The Instance
field of the object is equal the Instance
key in the storage table. I bind events so that whenever the instance is destroyed, memory is freed obviously.)
QUESTION 3 - WOULD YOU REPLICATE YOUR OBJECT MOBULES TO CLIENTS
I am honestly just afraid that people try to steal my code, it would be so much better to actually replicate everything to make animations, … What do you think about that?
Thank you for all this, please answer EACH question (even with “idk”), much love,
@Varonex_0