Game Development Community

Proper unloading after a level

by Craig Fortune · in iTorque 2D · 01/14/2010 (8:43 pm) · 5 replies

So now with 1.3 with have a great new system for controlling with datablocks we want to have in memory depending on the level. Using this system is pretty straight forward and easy and appears to work for loading datablocks just as you'd expect it to.

However, can someone explain me the proper procedure for UNloading a level as I swear I'm missing part of the picture...

Scenario:

* I load up a menu screen, all is dandy I see my sprites etc
* I load up a level of my game. Currently the datablocks file for this level is empty, so I expect to see nothing. However what I see is an object which uses an imageMap from a previously loaded datablock from the menu level.

Shouldn't that datablock and related resources have been zapped when I ended the menu screen and loaded up the level of my game? (and hence the game should no longer know about it?) Or am I supposed to remove it? If so, how, when and where is best?

Seems that if resources aren't getting freed up after each level this new system isn't gaining me anything, at least not after the first few loads of various levels. Of course I'm pretty sure I've just overlooked something so I'm hoping someone can point me in the right direction.

#1
01/15/2010 (2:57 pm)
I don't think code was implemented to actually unload the datablocks because it didn't actually free any memory. If you kept the original structure of the level datablocks file, all the datablocks should be in the SimSet $levelDatablocks.

You should be able to delete the current level's datablocks by using the following code:

for (%i = 0; %i < $levelDatablocks.getCount(); %i++)
{
     %datablock = $levelDatablocks.getObject(%i);
     %datablock.delete();
}
#2
01/15/2010 (3:39 pm)
would have expected that unloading was implemented together with the per level loading as it isn't much of per level if it does not unload the stuff :)
#3
01/15/2010 (3:52 pm)
Marc, that's what I thought :)
#4
01/18/2010 (6:04 am)
That is up to you to manage : situations are ALWAYS specific. Some, are generalised not the other way around ;)

For example : tile_00 datablock is used in level 1 - 4, but not level 5. Is my load time determined by :

Unload everything : (level 2) tile_00 deleted.
Reload everything : (level 3) tile_00 loaded.
Run level.

Do this repeatedly?

This is not the best option, obviously. You must explicitly delete what you want to delete, and keep what you want to keep. This saves in between level loading times drastically (for super obvious reasons).

As Joe shows, its really easy to cleanup. Nothing to it ;) The other obvioius way, is to make load/cleanup functions for your game (the way games normally work).

loadMainMenu(); cleanupMainMenu();

These are quick, for cleanup.
mainMenuBgSprite.delete();
mainMenuBgImageDatablock.delete();

keep in mind : image maps will not be removed from memory if *anything* is referencing them. delete all sprites, animation datablocks, or objects that reference the datablocks before trying to delete them (the console warns you).
#5
01/18/2010 (3:18 pm)
Sven,

Totally agree - I just think that it would have been nice if the system could have gone that one step further, and seen what datablocks were required in the next scene and kept them around. (or more importantly, the resources)

I'm thinking more on a resource loading standpoint than the datablocks themselves. Of course this is just unfortunately a side effect of the datablock system as a whole which has plagued Torque for many years :/

Joe,
Just a quick note:
The code snippet mentioned won't work correctly, as you'll be grabbing an increasing index as the $leveldatablocks group gets smaller... you need to grab the 0 object.