Object removal and destruction

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.
Post Reply
User avatar
verilog
Citizen
Posts: 97
Joined: Thu Nov 03, 2011 3:15 am
Contact:

Object removal and destruction

Post by verilog »

Hello guys, and happy new year! :awesome:

I'm wondering if my method for removing inactive entities (e.g. enemies) is enough for destroying the actual object and deallocating it from memory, I would like any advice on this, if possible.

This is my current approach:

Every object is described as a SECS class, with the usual init(), update() and draw() functions/methods. Every object has an “active” property, defined as a “self.active” boolean variable inside the init block:

Filename: grumpyEnemy.lua

Code: Select all

…
function grumpyEnemy:init(x, y)
	self.x = x
	self.y = y
	self.active = true
	…
end
…
So far so good, once the appropriate conditions are met, the object may be “inactive”, setting self.active = false, and it now must be destroyed. This step is basically the marking of an entity to be soon destroyed.

In the case of my enemies, all my “enemies” instances are stored inside an “enemies” table, managed directly in my level class. I loop through all my level objects in the update cycle. When I dispose of an object, I first check its active status and, if set to false, I proceed to remove it from the corresponding table :

Filename: level.lua

Code: Select all

…
function level:init()
	self.enemies = {}
	--this level currently has only one grumpy enemy:
	self.enemies[1] = grumpyEnemy:new(100,100)
end

function level:update(dt)
	-- I will loop through all the level tables and update each object as necessary:
	   for k,v in pairs(self) do
		     for i = 1, #v do
		        -- update each object:
		        if v[i].update then
			         v[i]:update(dt)
			         -- now, if the object is inactive, remove it from the object table:
			         if (v[i].active == false) then
			            table.remove(v, i)
			         end
		        end
		     end
	   end
end
…
 
And that's all, so far it seems to be working, but I'm not entirely sure if that's a good approach to be removing objects. Do you think I'm doing nonsense?

Thanks for your time!
mathacka
Prole
Posts: 8
Joined: Tue Sep 25, 2012 7:45 pm

Re: Object removal and destruction

Post by mathacka »

This may be a bad idea, but I have an idea how you could test it. Put a large object in, something like:

Code: Select all

-- (10 meg)
a[1024][10]
get a RAM Monitor, remove/add/remove...the object and see if it's in and out. But I wouldn't have my computer running anything special so if it crashes or locks up/requires a reboot, not my fault, but you would know.
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Object removal and destruction

Post by Santos »

The only thing which comes to mind is that the for loop might skip over some objects due to removing table entries while looping through the table. This can be resolved by iterating through the table entries in reverse order. So, by changing...

Code: Select all

for i = 1, #v do
to...

Code: Select all

for i = #v, 1, -1 do
User avatar
verilog
Citizen
Posts: 97
Joined: Thu Nov 03, 2011 3:15 am
Contact:

Re: Object removal and destruction

Post by verilog »

Hi mathacka and Santos,

Thank you guys for your replies.

Santos, I realized the potential problems that a “normal” loop might cause after some experimentation and reading Boolsheet's post in a similar thread. Like you suggested, I ended up implementing a reverse loop and so far it seems to be working as intended. Again, thank you for your time! :megagrin:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 47 guests