Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Class = {
- new = function(_constructor)
- local class = {}
- class.length = 0
- class.values = {}
- class.__index = class
- local constructor = function(class, ...)
- local self = setmetatable({}, class)
- class.length = class.length + 1
- table.insert(class.values, self)
- function self:destroy()
- class.length = class.length - 1
- for i, v in next, class.values do
- if v == self then
- table.remove(class.values, i)
- break
- end
- end
- setmetatable(self, {})
- end
- if _constructor then
- _constructor(self, ...)
- end
- return self
- end
- setmetatable(class, {
- __call = function (...)
- return constructor(...)
- end
- })
- return class
- end
- }
- -- Player Class Sample
- local Player = Class.new(function(self, name)
- self.name = name
- self.hp = 100
- end)
- function Player:getName() return self.name end
- function Player:getHp() return self.hp end
- function Player:removeHp(hp) self.hp = self.hp - (hp or 0) end
- local player = Player("Nettoork#0000")
- print(Player.length)
- player:removeHp(1)
- print(player.name .. " [" .. player.hp .. "]")
- player:destroy()
- print(Player.length)
Advertisement
Add Comment
Please, Sign In to add comment