0

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!

mac_55
  • 4,130
  • 7
  • 31
  • 40

2 Answers2

1

You could perhaps use a singleton.

Community
  • 1
  • 1
Mac
  • 14,615
  • 9
  • 62
  • 80
  • A singleton or shared instance is the way to go here. Mac is correct. – Jordan Mar 29 '10 at 00:49
  • 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 Mar 29 '10 at 00:57
  • No, because said singleton would be retained by your app's delegate throughout the entire app's life – Hellektor Mar 29 '10 at 01:09
0

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

Hellektor
  • 479
  • 3
  • 5