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?


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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