Hi! On begin, i want greet all Just now, I sorry for my english - i learn it.
I’m writing to you, because I have problem with manage memory in MonoGame. I’m beginner programmer and I know MonoGame since one week ;P.
Going to the case. I have my project, what I divided on classes. I have one MainClassGame, and other classes, what are scenes. This look like this:
MainClass:
public class MainGame : Game
{
protected override void Initialize()
{
}
protected override void LoadContent()
{
}
protected override void UnloadContent()
{}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
if (MenuState.IsShowMainMenuScene)
mainMenuScene.Update();
if (MenuState.IsShowAboutScene)
aboutScene.Update();
if (MenuState.IsShowSettingsScene)
settingsScene.Update();
//
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
if (MenuState.IsShowMainMenuScene)
mainMenuScene.Draw(spriteBatch, gameTime);
if (MenuState.IsShowAboutScene)
aboutScene.Draw(spriteBatch, gameTime);
if (MenuState.IsShowSettingsScene)
settingsScene.Draw(spriteBatch, gameTime);
if(MenuState.IsShowGameScene)
//
base.Draw(gameTime);
}
}
And in other class, it’s methods Update and Draw.
This is:
public class SettingsScene : DrawableGameComponent
{
Game game;
ContentManager Content;
public SettingsScene(Game game) : base(game) { this.game = game; LoadContent(); }
protected override void LoadContent() { Content = new ContentManager(game.Services); Content.RootDirectory = game.Content.RootDirectory; }
public void Draw(SpriteBatch spriteBatch, GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(); // spriteBatch.End(); }
public void Update() { } }
I find solved my problem with destruct unusable classes and unload unusable content:
I write destructor in “other class”:
~MyScene(){
Content.Unload();
}
And call it, in my MainClass:
if (MenuState.IsShowSettingsScene)
{
if(settingsScene !=null)
settingsScene.Update();
else{
settingsScene = new SettingsScene(this);
}
}
else
{
settingsScene = null;
GC.Collect();
}
This solved, called my destructor in “other class” and unload my content, but usable memory is for all time does not change.
My question:
- How to do unload my content, when i don’t use my “other class” ?