views:

33

answers:

2

I'm creating my first game, and I've currently set up a 'GameState' class, to store player health etc. inside. This class is currently instantiated from the AppDelegate as I need to access it from all over my game.

This is fine. For each class I'm working in, I can access the app delegate, and then find the GameState object... however, it seems very messy. I'm tempted to find a way (I'm still a newbie) to define the GameState instance as being some sort of global variable so that I can access it from all over with ease... but my little bit of reading on variable scope makes me uneasy about doing this, even if I knew how.

Any ideas of the best way to define and access this class? It'll be used for everything from player health, to items they've found, any personalisation etc.

Thanks!

+1  A: 

You could perhaps use a singleton.

Mac
A singleton or shared instance is the way to go here. Mac is correct.
Jordan
Hrm, that's slightly confusing. Won't the contents of the variables contained in the class be destroyed or re-initialised every time the instance is used? *confused*
mac_55
No, because said singleton would be retained by your app's delegate throughout the entire app's life
Good reference here: http://videos.71squared.com/6378572
mac_55
A: 

Perhaps Key-Value Observing or Notifications are a better choice.