Created
January 22, 2019 20:43
-
-
Save adrian-alberto/990d4b2cd060341d1cec4ec815d392e1 to your computer and use it in GitHub Desktop.
Simple OOP for Roblox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
Classes are simple modules placed in the SoopHierarchy folder. | |
Methods, functions, and properties are handled the same as in standard metatable- | |
based oop frameworks. | |
Class hierarchy follows the hierarchy of module objects in the hierarchy folder. | |
Single-inheritence, default constructors(:init()), etc. are handled automatically on start. | |
--]] | |
local hierarchy = script.Parent.SoopHierarchy | |
local soop = {} | |
function initClass(module, superclass) | |
if soop[module.Name] then | |
error("Duplicate class name in hierarchy: " .. module.Name) | |
end | |
local class = require(module) | |
local c_mt = {__index = class, __tostring = function(obj) | |
if obj.tostring then | |
return obj:tostring() | |
else | |
return obj._classname | |
end | |
end} | |
class._classname = module.Name | |
class._module = module | |
--Inherit parent class properties | |
if superclass then | |
setmetatable(class, { | |
__index = superclass | |
}) | |
end | |
--Automatic object instantiation. | |
--Automatically applies Class.init(obj, ...) | |
--e.g. dogObj = soop.Dog.new("Porkchop") | |
-- print(dogObj.name) -> Porkchop | |
--(see next section for implementation of init function) | |
function class.new(...) | |
local obj = setmetatable({}, c_mt) | |
if obj.init then obj:init(...) end | |
return obj | |
end | |
--Java style superclass | |
--e.g | |
--[[ | |
function Dog:init(dogName) | |
Dog.super().init(self, dogName) --Runs parent class initialization function | |
--NOTE: initializing using the parent class init function is optional. | |
self.name = dogName | |
end | |
--]] | |
function class.super() | |
return superclass | |
end | |
--Java style class checking | |
--Takes a class table as argument | |
--e.g. print(soop.Dog.instanceOf(soop.Animal)) -> true | |
-- print(dogObj.instanceOf(soop.Dog)) -> true | |
-- print(dogObj.instanceOf(soop.Animal)) -> true | |
function class.instanceOf(c) | |
return c == class or superclass and superclass.instanceOf(c) | |
end | |
--Roblox style class checking | |
--Takes string class name as argument. | |
--e.g. print(dogObj:IsA("Dog")) -> true | |
-- print(dogObj:IsA("Animal")) -> true | |
function class.IsA(self, cname) | |
return cname == class._classname or (superclass and superclass.IsA(self, cname)) | |
end | |
soop[module.Name] = class | |
for i, v in pairs(module:GetChildren()) do | |
if v:IsA("ModuleScript") then | |
initClass(v, class) | |
end | |
end | |
end | |
for i, v in pairs(hierarchy:GetChildren()) do | |
if v:IsA("ModuleScript") then | |
initClass(v) | |
end | |
end | |
return soop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment