Page 1 of 3

Problem using the 'SECS' class library

Posted: Thu Apr 11, 2013 6:08 pm
by Zephos
So I tried using the SECS library most of you are probably familiar with. The snippet I use is the basic version:

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
Now, I have my love.load() function like this...

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
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.

Re: Problem using the 'SECS' class library

Posted: Thu Apr 11, 2013 8:25 pm
by Zeliarden
run the parents init

Code: Select all

	function object_sub:init()
		object:init()
		self.d = 4
	end

Re: Problem using the 'SECS' class library

Posted: Thu Apr 11, 2013 9:35 pm
by bartbes
Zeliarden wrote:run the parents init
The correct code is:

Code: Select all

	function object_sub:init()
		object.init(self)
		self.d = 4
	end

Re: Problem using the 'SECS' class library

Posted: Fri Apr 12, 2013 4:25 pm
by Zephos
Is there a way to let it do automatically instead of me having to insert this line again and again?

Re: Problem using the 'SECS' class library

Posted: Sun Apr 14, 2013 1:47 pm
by Zephos
*bump*

Can someone at least briefly explain the SECS code?

Re: Problem using the 'SECS' class library

Posted: Sun Apr 14, 2013 3:27 pm
by bartbes
Zephos wrote:Is there a way to let it do automatically instead of me having to insert this line again and again?
No.
Zephos wrote:Can someone at least briefly explain the SECS code?
What, specifically, do you want to know?

Re: Problem using the 'SECS' class library

Posted: Sun Apr 14, 2013 5:10 pm
by Zephos
What exactly the code basically does.

Re: Problem using the 'SECS' class library

Posted: Sun Apr 14, 2013 7:16 pm
by Roland_Yonaba
You might want to google for object orientation first (here is a nice intro applied on game development).
SECS just offers to the Lua programmer the core mechanisms to perform object orientention with Lua. The online book Programming In Lua has a dedicated chapter on how to write your own OO framework. Once you understand what is said in there, you will be able to fully understand the sourrce code of SECS.
Hope it helps.

Re: Problem using the 'SECS' class library

Posted: Sun Apr 14, 2013 7:40 pm
by Zephos
That'll do, thanks.

Re: Problem using the 'SECS' class library

Posted: Fri Apr 19, 2013 10:50 am
by Zephos
*bump*

When deleting objects, you simply set the object to nil, right? Can someone explain why this code won't work?

Code: Select all

function love.load()
	require "class"

	object = class:new()
	function object:init()
		function self:destroy()
			print(self) -- returns table when obj:destroy() is called
			self = nil
			print(self) -- returns nil when obj:destroy() is called
		end
	end

	obj = object:new()

	obj:destroy()
	print(obj) -- return the table again? wtf
end