5 Replies - 3211 Views - Last Post: 22 December 2013 - 07:33 PM

#1 xBrandon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-December 13

XNA Game State tutorial?

Posted 20 December 2013 - 09:41 AM

I've searched around and found numerous GameState tutorials (Menu tutorials, screen manager tutorials, etc), but none of them are that efficient. I've created my own, but it's also inefficient. What I use to control the screens is an enum, however, the way I load my screens and handle them is inefficient to what other game makers use. I want to read/learn a better way to handle this. If you don't mind, could you provide me with any tutorials?

Is This A Good Question/Topic? 0
  • +

Replies To: XNA Game State tutorial?

#2 Rhino1111  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 99
  • View blog
  • Posts: 229
  • Joined: 28-August 13

Re: XNA Game State tutorial?

Posted 20 December 2013 - 07:42 PM

What exactly is inefficient about it? Do you load/dispose of thousands of resources on state changes? Even that should be relatively fast with the optimizations the content pipeline makes.

Performance optimizations are very specific, which is why nobody will be able to answer such a vague question. I'd suggest using the stopwatch class to identify which code segments in your routines are bogging down to cause these delays. Then post those specific segments of code and ask how they could be made more efficient.

As for the tutorial part: You could try CodingMadeEasy's Monogame RPG tutorial series. The first 10 or so parts are mainly about a screenmanager/menu system.

Link: http://www.youtube.c...yFHPfrSNhqFoA0h

This post has been edited by Rhino1111: 20 December 2013 - 07:46 PM

Was This Post Helpful? 2
  • +
  • -

#3 xBrandon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-December 13

Re: XNA Game State tutorial?

Posted 20 December 2013 - 08:25 PM

View PostRhino1111, on 20 December 2013 - 07:42 PM, said:

What exactly is inefficient about it? Do you load/dispose of thousands of resources on state changes? Even that should be relatively fast with the optimizations the content pipeline makes.

Performance optimizations are very specific, which is why nobody will be able to answer such a vague question. I'd suggest using the stopwatch class to identify which code segments in your routines are bogging down to cause these delays. Then post those specific segments of code and ask how they could be made more efficient.

As for the tutorial part: You could try CodingMadeEasy's Monogame RPG tutorial series. The first 10 or so parts are mainly about a screenmanager/menu system.

Link: http://www.youtube.c...yFHPfrSNhqFoA0h


Well, the thing is: I load all my resources when the game starts, and once screen changes, I just leave them there. I have no idea how to unload them properly, but yeah. I've seen some of his tutorials, it's alright...but eh.
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 591
  • View blog
  • Posts: 1,317
  • Joined: 24-April 12

Re: XNA Game State tutorial?

Posted 21 December 2013 - 08:14 AM

View PostxBrandon, on 20 December 2013 - 09:25 PM, said:

Well, the thing is: I load all my resources when the game starts, and once screen changes, I just leave them there. I have no idea how to unload them properly, but yeah. I've seen some of his tutorials, it's alright...but eh.



I think I vaguely know what you're talking about. When the Game class starts up it runs the LoadContent() method, which is controlled by the ContentManager.

Content.RootDirectory = "Content";



At one point, when I was studying state management, I remember coming across a discussion about UnLoadContent(). I believe they were talking about instantiating multiple ContentManagers. My understanding of it was that they were talking about loading all of the content for a level, for example, in one ContentManger and then killing that ContentManager to release the resources and creating a second ContentManager to load the next level. I imagine the UnloadContent () method would be called when you are destroying the ContentManager. Otherwise, I think both only get called once.

That might at least get you pointed in the right direction. I've never messed with that, although it intrigued me a bit. I've never done a project where the efficient management of content was an issue. So I've never done anything other then let it load and leave it loaded. If you do find a more efficient way, I would like to hear about it. They obviously included UnloadContent() for a reason, but I'm not sure I've ever seen anyone actually use it. I'd still like a better understanding of what it's actually good for.
Was This Post Helpful? 2
  • +
  • -

#5 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,641
  • Joined: 24-September 10

Re: XNA Game State tutorial?

Posted 22 December 2013 - 04:56 PM

You can have more than 1 ContentManager.

Note, if 1 content manager loads the same texture as another content manager... there'll be 2 of the same textures in actual memory, each independently accessible from the ContentManager that loaded the unique one.

The ContentManager owned by the Game object should really only be used for stuff that is required on a global level (probably not a whole lot really...).

First create yourself a GameStateManager, I usually make it inherit from DrawableGameComponent because the GameStates are going to be drawable and DrawableGameComponent has the methods that are automatically called for this by the main processing pipe. Then create some abstract class/interface for a generic GameState. All your game states will inherit/implement this class/interface. Then each GameState will have it's own ContentManager (I'd go the abstract class route and have the abstract class create this on construction). Then the GameState will load the stuff it needs.

Note you will want to code your GameStateManager/GameState framework to allow for layering game states (or make it a sub member of the game state, like a gamescreen), so that you can have for instance a 'options menu' that opens over the 'level' with out unloading the level every time.




You may also want to consider adding an ability to register other ContentManagers to some pool. Let's say it's called a "ResourceManager", which is a GameComponent attached to the Game. You can register ContentManagers to it with an "owner" (basically a dictionary of object -> ContentManager relationships). So this way if you follow an Entity design pattern (where objects in your game are considered 'Entities' that have components added to them [not GameComponents] that change the way the Entity acts) you can have some shared ContentManager that is accessible to specific types of Entities. That sharing is specific to how you set up the 'owner' relationship. This way you could for instance register some model to be used by multiple game objects to some shared ContentManager. The some group of entities are told which content manager to get the model from. And then they all share the same specific model, which can then be modified in memory, and you get a model that can be rendered over and over efficiently under the precept that all Entities with this model look identical (good for say trees or something that duplicate a lot).

Anyways, point is, this ResourceManager would have the content managers for even the GameStates and other things registered to it. Allowing you a nice easy place to get to those ContentManagers, and also identify where those ContentManagers are being used. So if you have to clean up the game you can easily purge all memory by specific criteria.

This post has been edited by lordofduct: 22 December 2013 - 04:58 PM

Was This Post Helpful? 2
  • +
  • -

#6 xBrandon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 14-December 13

Re: XNA Game State tutorial?

Posted 22 December 2013 - 07:33 PM

Thank you all! Especially BBeck and lordofduct. I think I have enough info to re-design my ScreenManager to a more efficient manner. Thanks once again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1