Velinquish

OOP Class Library

Sep 24th, 2021
841
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.     A small convenient library I wrote to make OOP easier. Consider it an extra gift ;)
  3. ]]
  4. local RunService = game:GetService("RunService")
  5.  
  6. local Class = {
  7.     classes = {} -- A dictionary of the names of all classes created with class() to the classes
  8. } -- Abstract Base Class for all classes, equivalent of Roblox's Instance
  9. Class.__index = Class
  10.  
  11. function Class.get(className) -- A wrapper
  12.     return Class.classes[className]
  13. end
  14.  
  15. function Class:clientOnly(memberName, member)
  16.     if RunService:IsClient() then
  17.         self[memberName] = member
  18.     end
  19.     return member
  20. end
  21.  
  22. function Class:serverOnly(memberName, member)
  23.     if RunService:IsServer() then
  24.         self[memberName] = member
  25.     end
  26.     return member
  27. end
  28.  
  29. function Class:super(n) -- Takes in how many parents up, returns parent class
  30.     n = n or 1
  31.     local parent = self
  32.     for _ = 1, n do
  33.         parent = parent.__super__
  34.     end
  35.     return parent
  36. end
  37.  
  38. function Class:GetSuperclasses()
  39.     local supers = {}
  40.     local classIterator = self.__super__
  41.     if classIterator == nil then return end
  42.     repeat
  43.         table.insert(supers, classIterator)
  44.         classIterator = classIterator.__super__
  45.     until classIterator == nil
  46.     return supers
  47. end
  48.  
  49. function Class:IsA(class) -- Checks if the classes match; could be class name or object
  50.     class = typeof(class) == "table" and class.__classname__ or class
  51.     local classIterator = self.__class__
  52.     repeat
  53.         if classIterator.__classname__ == class then
  54.             return true
  55.         end
  56.         classIterator = classIterator.__super__
  57.     until classIterator == nil
  58.     return false
  59. end
  60.  
  61. function Class:init(...)
  62.     local className = self.__classname__
  63.     warn(className .. " does not have a " .. className .. ":init() function.")
  64. end
  65.  
  66. setmetatable(Class, {
  67.     __call = function(_, name, parent) -- Returns a newly created class, takes string name and parent class
  68.         parent = typeof(parent) == 'string' and Class.get(parent) or parent
  69.        
  70.         local classInst = {}
  71.         classInst.__index = classInst
  72.         classInst.__classname__ = name -- Class metadata
  73.         classInst.__class__ = classInst
  74.         classInst.__super__ = parent
  75.         classInst.__script__ = getfenv(2).script
  76.         Class.classes[name] = classInst
  77.         setmetatable(classInst, parent or Class)
  78.        
  79.         function classInst.new(...)
  80.             local instance = setmetatable({}, classInst) -- No cyclic table ref in serialization
  81.             classInst['init'](instance, ...)
  82.             return instance
  83.         end
  84.         return classInst
  85.     end
  86. })
  87.  
  88. return Class
RAW Paste Data