What any constructor
function does is, create an empty table, because objects in oop are most commonly expressed as tables, then put properties in that empty table, specified along with the constructor’s parameter (Vector3.new(x, y, z), here for example I mean the x and the y and the z, which are technically a property of vector3) represented as keys inside of the empty table. Then for the methods, they aren’t created in the empty table, that’s kind of inefficient, instead using __index
the methods can be created inside of the class
table which is the class, and can be indexed from that empty table without actually having it inside of the empty table.
local class = {__index = {}} --all methods would be stored inside of this table, the __index one
function class.new(x, y) --the object will have two properties, and x and y coordinates
local object = {} --let's just say this is our object, the empty table that we're gonna fill
setmetatable(object, class) --setting the object's metatable, so if a method is called upon it, it will check if the class has that method
object.X = x
object.Y = y
return object --return the object, otherwise just a table
end
local obj = class.new(2,5) --an object, represented as a table
And remember that for methods they have to be inside of __index
’s table
function class.__index:Init(x, y)
self.x = x
self.y = y
end
And a very good point is, it seems that you create the object first, without any properties, then set the properties using :Init()
, so the code would be something like this
local class = {}
function class.new()
local object = {}
setmetatable(object, class)
return object
end
function class:Init(x, y)
self.x = x
self.y = y
end
local class_A = class.new()
class_A:Init(45, 80)
local class_B = class.new()
class_B:Init(99, 4)
Here is a really good article, read the first part of it to understand more about oop.