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 :
- How can i save a level with his own number of sprites, their events and objects ?
- How can i load/unload these levels ?
Give each level its own
ContentManager, and use that instead ofGame.Content(for per-level content).(Create new
ContentManagerinstances by passingGame.Servicesto the constructor.)A single
ContentManagerwill 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()).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
ContentManagerdon’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).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.If this still doesn’t provide enough control, you can derive a class from
ContentManagerand implement your own load/unload policies (example in this blog post). Although this is rarely worth the effort.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
ContentManagerworks.