Problem using the 'SECS' class library
Posted: Thu Apr 11, 2013 6:08 pm
So I tried using the SECS library most of you are probably familiar with. The snippet I use is the basic version:
Now, I have my love.load() function like this...
My problem is that statements like adding 1 to the global variable 'test' aren't passed down anymore at a certain point while 'self' variables are. Does anyone know a fix? Metatables are a too complicated thing for me to grasp at the moment, so I can't really do anything about it myself right now.
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