Does this make sense, am I understanding this correctly? ( OOP )

I’m going to write what I know in the comments please correct me if I’m wrong

local o = {} 

function fireball:new(o) -- this is a constructor function
	o = o or {} -- O is the regular table 
	setmetatable(o, self) -- Since self = the regular table, its a regular table
	self.__index = self -- self changes to a metatable 
	return o -- the object is returned 
	
end

self is kinda confusing

1 Like

This also works, but constructors are conventionally stated as .new and not :new, but whatever fits your case best IG.

As per self, think of self as the actual object. For example:

local obj1 = fireball:new(o)
local obj2 = fireball:new(o)

obj1:someFunction(self, blah) --only affects obj1 and not all other objects created by the constructor

--self is automatically passed as a parameter; equivalent of beep.boop(beep)
--self is basically obj1

Self is accurately named to describe the object that the function is running on, so the function only affects it-self, get it?

2 Likes

Thanks this clears up my confusion