Christian Boman
Christian Boman

Reputation: 43

How to load textures after startup in XNA using vb.net

I've encountered a problem with my gameproject lately. When I load more than a certain number of textures (around 1000) at startup I recieve an error regarding memory (because I have to use 32-bit in XNA). I am self-taught and so have only basic knowledge of the "correct" ways to program a game. The project itself is getting very big and although I try to compress images together etc, I will have to use more than 1000 textures throughout the project.

My question, then, is: How can I load textures at other points in time than at startup in XNA, using vb.net? (I'm not using classes at all that I'm aware of, so I hope to stay away from that if possible)

Thank's for any feedback on this! /Christian

Upvotes: 1

Views: 118

Answers (2)

Christian Boman
Christian Boman

Reputation: 43

So the answer seems a lot easier than I expected, if I'm doing it right at the moment(?).

I simply Load the content needed for all "Worlds" in the beginning of the game (in the Protected Overrides Sub LoadContent()) and then Dispose() and Load.Content() depending on what World is loaded later (in any Sub I choose):

TextureName = Content.Load(Of Texture2D)("")
TextureName.Dispose()

If there isn't any problem I'm not yet aware of, this seem to do the trick and does not leave me with the memory error in the start.

Thank you Davor Mlinaric and Monset for helping me along

Upvotes: 1

Monset
Monset

Reputation: 657

I cannot comment, so I'll just put my idea here. Declaration time: I am also self-taught developer, so the algorithm that I'm using isn't proven to be the standard or anything like that. Hope it helps!

I am using a single class called GContent witch is "instanciated" (more like loaded) the first thing when game starts. This class is static, and it has lists for all textures, sounds and spritefonts in game. So, anywhere in my code I can put GContent.Texture("texture_folder\\texture_name"); (similar for sound and spritefont). Now, before this function loads Texture2D, it checks it's list of textures, and tries to return a texture with the name that is asked for in parameter. If it finds the right texture, it returns the texture from the list. If not, it uses Content.Load(textureFullPath); (by full path I don't mean "C:\Users\....") to load the texture, give it a name (Texture2D.Name) equal to the parameter textureFullPath, adds that texture to the list, and then returns the new texture.

So, the more you play my game, the more textures will be loaded, if I don't load all the assets at the start of the game. Now, I imagine you can simply have an array of strings that represent all textures that are used by a single level, or a map, or main menu... This way, you could easaly create function that will take List< string > and try to load or unload all textures of one map/level/menu...

So, my answer is pretty much: have a static class with lists of assets and load/unload asset(s) from where ever in game you want!

Also, if my answer helped you, please, check it as an answer :)

Upvotes: 1

Related Questions