Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's the best way to handle game states / screens? My problem is this:

PlayGameScreen adds a new InventoryGameScreen to the game during it's update. This immediately adds InventoryGameScreen to the array of GameScreens. That's throwing an exception when iterating over the array that the contents of the array have changed.

Should I have two more arrays, like screensToBeAdded and screensToBeRemoved and do all the processing for them at the end of the game loop after drawing all the other screens?

share|improve this question

There are several equally valid approaches, and enqueuing pending changes for later processing like you have suggested is but one of them. It will work just fine; without more details it's hard to provide much more advice.

That said, this concrete example is for game state management in XNA, but the concepts should apply to any language/platform/technology/et cetera and it might help demostrate a working solution you can learn from.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.