Steffanp

Hi
I know how to load a texture when the game starts, but how to load a texture when the game has already startede ex. 10 min in the game


Re: XNA Game Studio Express loading textures "in game"

Gregory English

You can load a texture at any time during the game.

If you're using the content manager it would be

content.Load<Texture2D>(AssetName);

and that can be anywhere in the code if you wish.

If you want to do it simply from a file(PC only), it would be

Texture2D myTexture = Texture2D.FromFile(FileName);

also can be put anywhere in the code. on this one, make sure you get rid of it when you don't need it anymore.





Re: XNA Game Studio Express loading textures "in game"

Joel Martinez

I think perhaps he was asking about guidance about loading assets outside of the LoadGraphicsContent method. This is actually a good question that I've been wondering myself.





Re: XNA Game Studio Express loading textures "in game"

dczraptor

You can call content.Load<>() anytime, and anywhere, in your code. Say you want to load a texture after the player has passed a certain position:

if(player.Position.Y > 1000) content.Load<Texture2d>("JustAnotherWall");

You aren't required to load all your assets inside LoadGraphicsContent; it's just good design/habit.

There are two types of loading resources - automatic and manual. Automatic is through content.Load<>(), and whenever you call content.Unload(), it will dispose of everything properly for you. Manual texture loading is a bit more complicated, but still nothing fancy:

Texture tex = Texture2D.FromFile();

However, when you are done using the texture, make sure you call tex.Dispose(); Otherwise, the GC might not come and dispose of that memory, and you'd have a memory leak problem.






Re: XNA Game Studio Express loading textures "in game"

Joel Martinez

No no ... I don't think that calling content.Load anywhere in the code is what's under discussion here. I always thought that the point of the LoadGraphicsContent had something to do with the device resetting ... that you would have to reload some content if the device resets, and doing it in the LoadGraphicsContent method would ensure it got reloaded right.

Honestly, I'm not sure that the concept of losing the device, why it happens, and what you have to do when it does happen has been covered very well anywhere. And if it has, I missed it :-)





Re: XNA Game Studio Express loading textures "in game"

dczraptor

Losing the device happens when you switch over to another application, minimize the window, change monitor resolution, etc. XNA automatically handles these device resets with its content manager. You don't need to do anything special to reload the resources you've loaded with the content manager. I've tried all sorts of things, even locking the computer, and when I come back, it's always still there. However, with manual resources, you will need to dispose of them when you lose the device, under UnloadGraphicsContent i believe, and then reload them when you regain the device, inside LoadGraphicsContent. It's either that, you can hook the device events yourself under graphics.GraphicsDevice. I'm not exactly sure which one is the correct method since I haven't done much manual loading, but it's one or both of those methods.




Re: XNA Game Studio Express loading textures "in game"

Joel Martinez

ahhhh ... that's good to know :-) thanks for the explanation