Problem using the 'SECS' class library

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Zephos
Prole
Posts: 18
Joined: Sun Mar 31, 2013 4:53 pm

Problem using the 'SECS' class library

Post 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.
Attachments
classproblem.love
(1.38 KiB) Downloaded 228 times
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Problem using the 'SECS' class library

Post by Zeliarden »

run the parents init

Code: Select all

	function object_sub:init()
		object:init()
		self.d = 4
	end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Problem using the 'SECS' class library

Post 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
Zephos
Prole
Posts: 18
Joined: Sun Mar 31, 2013 4:53 pm

Re: Problem using the 'SECS' class library

Post by Zephos »

Is there a way to let it do automatically instead of me having to insert this line again and again?
Zephos
Prole
Posts: 18
Joined: Sun Mar 31, 2013 4:53 pm

Re: Problem using the 'SECS' class library

Post by Zephos »

*bump*

Can someone at least briefly explain the SECS code?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Problem using the 'SECS' class library

Post 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?
Zephos
Prole
Posts: 18
Joined: Sun Mar 31, 2013 4:53 pm

Re: Problem using the 'SECS' class library

Post by Zephos »

What exactly the code basically does.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Problem using the 'SECS' class library

Post 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.
Zephos
Prole
Posts: 18
Joined: Sun Mar 31, 2013 4:53 pm

Re: Problem using the 'SECS' class library

Post by Zephos »

That'll do, thanks.
Zephos
Prole
Posts: 18
Joined: Sun Mar 31, 2013 4:53 pm

Re: Problem using the 'SECS' class library

Post 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
Attachments
deleteproblem.love
(1.35 KiB) Downloaded 182 times
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Google [Bot] and 7 guests