Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XNA: What is the point of Unload()?

XNA games have an Unload() method, where content is supposed to be unloaded. But what is the point of this? If all the content is being unloaded, then the game must be exiting, in which case everything would be garbage collected anyway, right?

like image 354
Nick Heiner Avatar asked Apr 07 '10 23:04

Nick Heiner


3 Answers

As far as I understand it, it's not useful for any standard uses, since as you say the garbage collector deals with things for you.

However, it's useful to have an event called when your game is exiting for many things. For example you could send a message to all clients in a multiplayer game telling them you're exiting, and then you can let the garbage collector kill your network connections.

like image 198
Martin Avatar answered Nov 19 '22 09:11

Martin


It’s always polite to clean up after yourself… otherwise people will stop letting you play with their toys.

My best guess is that it would allow you to nest Game objects into your project and give you a way to clean them up later. This would allow for better reuse of your code. Hopefully an XNA MPV or someone from the XNA team will find this and provide more insight.

like image 2
Matthew Whited Avatar answered Nov 19 '22 07:11

Matthew Whited


Unload is sometimes necessary, especially when working with libraries that don't get unloaded via the Garbage Collector. One example is XACT. I remember having to destroy the object responsible for XACT music in the Unload event in one particular case where the object was not being destroyed by the GC... But I forgot the actual scenario...

More importantly however, the UnloadContent method for GameComponent classes instead of Game classes. Generally, GameComponent and DrawableGameComponent objects can make use of several resources. When the object is destroyed, you have to make sure that all resources related to the object are destroyed, releases or sometimes put in pending state.

I would suggest you leave it for now. As you move forward with XNA development, and for larger scale projects, you might need to make use of the Unload method, and understand the certain cases where you need.

like image 1
SiN Avatar answered Nov 19 '22 09:11

SiN