How to load and Unload a level

How to load and Unload a level



I'm coding my first game with XNA and i'm a bit confused.



The game is a 2D platform game, in pixel-perfect, NOT Tiles based.



Currently, my code looks like this


public class Game1 : Microsoft.Xna.Framework.Game
{
//some variables
Level currentLevel, level1, level2;

protected override void Initialize()

base.Initialize();


protected override void LoadContent()

spriteBatch = new SpriteBatch(GraphicsDevice);

//a level contains 3 sprites (background, foreground, collisions)
//and the start position of the player
level1 = new Level(
new Sprite(Content.Load<Texture2D>("level1/intro-1er-plan"), Vector2.Zero),
new Sprite(Content.Load<Texture2D>("level1/intro-collisions"), Vector2.Zero),
new Sprite(Content.Load<Texture2D>("level1/intro-decors-fond"), Vector2.Zero),
new Vector2(280, 441));

level2 = new Level(
new Sprite(Content.Load<Texture2D>("level2/intro-1er-plan"), Vector2.Zero),
new Sprite(Content.Load<Texture2D>("level2/intro-collisions"), Vector2.Zero),
new Sprite(Content.Load<Texture2D>("level2/intro-decors-fond"), Vector2.Zero),
new Vector2(500, 250));

...//etc



protected override void UnloadContent()


protected override void Update(GameTime gameTime)


if(the_character_entered_zone1())

ChangeLevel(level2);

//other stuff



protected override void Draw(GameTime gameTime)

//drawing code


private void ChangeLevel(Level _theLevel)

currentLevel = _theLevel;
//other stuff



Every sprites are loaded since the beginning, so it's not a good idea for the computer's RAM.



Well, Here are my questions :




1 Answer
1



Give each level its own ContentManager, and use that instead of Game.Content (for per-level content).


ContentManager


Game.Content



(Create new ContentManager instances by passing Game.Services to the constructor.)


ContentManager


Game.Services



A single ContentManager will share all instances of content that it loads (so if you load "MyTexture" twice, you will get the same instance of it both times). Due to this fact, the only way to unload content is to unload the entire content manager (with .Unload()).


ContentManager


.Unload()



By using multiple content managers, you can get more granularity with unloading (so you can unload the content for just a single level).



Just note that different instances of ContentManager don't know about each other and won't share content. For example if you load "MyTexture" on two different content managers, you get two separate instances (so you use twice the memory).


ContentManager



The simplest way to deal with this is to load all the "shared" stuff with Game.Content, and all the per-level stuff with the separate level content managers.


Game.Content



If this still doesn't provide enough control, you can derive a class from ContentManager and implement your own load/unload policies (example in this blog post). Although this is rarely worth the effort.


ContentManager



Remember that this is an optimisation (for memory) - so don't spend too much time on it until it becomes an actual issue.



I recommend reading this answer (on the gamedev site) that provides some tips and links to further answers that explain in more depth how ContentManager works.


ContentManager






You're right, i shouldn't waste too much time on it. But it's always good to know. Thx a lot for all these informations.

– Sharpnel
Jul 21 '12 at 12:25






bonus: in a game level file you should enfore confidentiality and integrity. The former to avoid users peeking inside the file to know the level before actually playing it, the latter to avoid users changing the level data in order to gain some benefit while playing it

– Gianluca Ghettini
Apr 17 '14 at 21:17




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ