OOP Format
Follow this format for writing OOP code:
local Object = {}
Object.__index = Object
-- Types
type Object = typeof(setmetatable({}, Object)) & {
Foo : number,
Bar : string,
}
function Object.new(foo : number, bar : string)
local self : Object = setmetatable({}, Object) -- UNSEALED TABLE!!!
self.Foo = foo
self.Bar = bar
self:Init()
return self
end
-- Called as Object:Init()
function Object.Init(self : Object)
-- Initialization
self:Method()
end
-- Called as Object:Method()
function Object.Method(self : Object)
-- ...
end
return Object
Things to note:
- OOP is done best with metatables
__indexis declared at the top, right alongside the class declaration.newmakes a variable of the top of that scope calledself, which is an unsealed metatable- Using
.newcalls:Init()before returningself selfis typechecked- Methods are defined as
Object.Method(self : Object, ...)but called using:notation (Object:Method(...))
We write OOP in this way because of readability and ease of use. It's more faux-OOP than anything, but it gets the same job done.
When to use OOP
Object-oriented programming is used when you can best represent functionality as an instance of an object. For example, a weapon is an object with different existing instances, thus OOP makes the most sense for a gun class. Other examples of OOP being used is for tools, NPCs, cleanup groups, and more.