Code: Select all
__HAS_SECS_COMPATIBLE_CLASSES__ = true
local class_mt = {}
function class_mt:__index(key)
return self.__baseclass[key]
end
class = setmetatable({ __baseclass = {} }, class_mt)
function class:new(...)
local c = {}
c.__baseclass = self
setmetatable(c, getmetatable(self))
if c.init then
c:init(...)
end
return c
end
Code: Select all
function love.load()
require "class" -- the lua file containing the above snippet
test = 0
object = class:new()
function object:init()
self.a, self.b, self.c = 1, 2, 3
test = test + 1
end
print(test) -- 0, as expected
object_sub = object:new()
print(test) -- 1, as expected
function object_sub:init()
self.d = 4
end
object_sub_sub = object_sub:new()
print(test) -- still 1, not as expected
print(object_sub_sub.a, object_sub_sub.b, object_sub_sub.c, object_sub_sub.d) -- however, variables work fine
end