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

Because 'self' is not 'obj'. They're two distinct variables, and you can't modify one with the other (just the table they both point to).
Zephos
Prole
Posts: 18
Joined: Sun Mar 31, 2013 4:53 pm

Re: Problem using the 'SECS' class library

Post by Zephos »

And how can I fix this?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Problem using the 'SECS' class library

Post by Robin »

Like this:

Code: Select all

function love.load()
	require "class"

	object = class:new()
	function object:init()
	end

	obj = object:new()

	obj = nil -- don't forget to clear the other references to the object as well
	print(obj) -- prints nil
end
Help us help you: attach a .love.
Zephos
Prole
Posts: 18
Joined: Sun Mar 31, 2013 4:53 pm

Re: Problem using the 'SECS' class library

Post by Zephos »

It's important for me to have a function for that. And doing it the easiest way I can think of doesn't work either:

Code: Select all

function love.load()
	require "class"

	function object_destroy(o)
		o = nil
		-- note that I don't want to use 'return nil' here and write 'obj = object_destroy()'
		-- because I need to use this argument for other purposes too
	end

	object = class:new()
	function object:init()
	end

	obj = object:new()

	object_destroy(obj)
	print(obj) -- still prints table
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Problem using the 'SECS' class library

Post by Robin »

Zephos wrote:It's important for me to have a function for that.
Why?

It looks like you don't understand how Lua works and how garbage collection works. I want to help you but I don't know where to begin.
Help us help you: attach a .love.
Zephos
Prole
Posts: 18
Joined: Sun Mar 31, 2013 4:53 pm

Re: Problem using the 'SECS' class library

Post by Zephos »

Because I need to use the argument 'o' in the function for other purposes too other than setting it to nil. Take a look at it.
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 »

Well, you simply can't set it to nil.
Zephos
Prole
Posts: 18
Joined: Sun Mar 31, 2013 4:53 pm

Re: Problem using the 'SECS' class library

Post by Zephos »

By the way, I can't set it to anything else either, actually. Are you telling me that there is no fix for this seemingly simple problem?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Problem using the 'SECS' class library

Post by Robin »

Zephos wrote:Are you telling me that there is no fix for this seemingly simple problem?
This is a textbook case of the X-Y problem. Why do you want to destroy objects in the first place? In Lua, you don't have to.
Help us help you: attach a .love.
Zephos
Prole
Posts: 18
Joined: Sun Mar 31, 2013 4:53 pm

Re: Problem using the 'SECS' class library

Post by Zephos »

Let Y be the main problem here now. My X should be irrelevant for you as long as I know exactly what I want, anyway. Just please give me the answer to my problem and let me be happy, especially if the solution is exactly as easy as I imagined it to be:

Code: Select all

function object_destroy(objstr)
	loadstring(objstr .. " = 'whateverthefuckiwant'")()
end

...

object_destroy("obj")
Now, is there anything to complain about this? In the end, that's all I wanted to know.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests