Beginners guide to freeing resources

Blitz3D Forums/Blitz3D Tutorials/Beginners guide to freeing resources

Matty(Posted 2005) [#1]
Freeing resources and managing your media

A common enough question is "how do I make my game restart?"

Design your code around the fact that sometimes the user will want to "restart" the game.
Not too unreasonable really, how many games have you played where you actually had to
remove the CD, reboot the machine in order to start a new game (aside from Silent Hill 4:The Room on Xbox - I was never able to work out how to get back to the main menu once the game started)

While my code is pretty messy, I do try to have some kind of structure to it which works for
me. I am not saying it is the best method or the only method but is one method that, for
simple games works fine.

repeat

MainMenu()

InitialiseGame()  

CoreGameLoop()

DeInitialiseGame()


forever




Ignoring the function "MainMenu()" for now, look at the other three.

InitialiseGame() is where you load all of your entities, sounds, images and other media.
Store the handles for each of these in a list, usually using 'types'. Whether you use a single
'type' list to store all of your media or you break it down into components like 'particletextures','creaturemeshes','levelmeshes','soundeffects',"cameras"
is up to you - but you do want to store the handles so that you can free them later.

CoreGameLoop() is where you have your regular game code - all the usual stuff you put in there. As an example, many of
mine look like this (or similar):


repeat

HandleNetwork()
GetPlayerInput()
GetAIInput()
UpdateItems()
UpdateCreatures()
UpdateBullets()
UpdateParticles()
UpdateGraphics()

until GameIsOver<>0



Now, with most games things like particles, creatures and bullets come and go quite often so you want
to use a 'type' list to store these as well - or at least the handles to them, so that when
the bullet has reached the end of its life, or the smoke particle has faded away you can free
those entities easily.

Finally the function :

DeInitialiseGame() is where all the media which is still present in those type lists and has
not been removed gets removed. If you have managed your resources properly in the coreGameloop, and have freed
everything that was still in memory then the statement 'clearworld' will most likely be totally unnecessary.

DeInitialiseGame() for me usually looks like this:

for sounds.soundobj=each soundobj
freesound sounds\soundchannel
next 

for levelmesh.levelobj=each levelobj
if levelmesh\meshhandle<>0 then freeentity levelmesh\meshhandle
delete levelmesh
next 

and so on with all the other bits and pieces that you have in your game.

Earlier on I said "Ignoring the function MainMenu for now". That is because I usually treat
that differently. The same principles can be applied to the MainMenu function however, it is in effect
no different than the CoreGameLoop - it consists of a section where I load the gui elements, interact with the gui elements,
and free the gui elements.

Others will do this procedure differently than I do, perhaps in much more elegant ways but
I hope you find it useful.


AdrianT(Posted 2005) [#2]
cool :) I'm at the point where this is pretty useful :)


Damien Sturdy(Posted 2005) [#3]
LOL.

Ahem.. (okay thats bad of me)

Actually, this is a great idea. I may be quite advanced with my normal coding (with some weak points. like tweening) but i do sometimes have problems re-organising code to get it to be "restartable".


Nice work, Many will find it useful :D