iDevGames Forums
Global Objects - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Global Objects (/thread-2063.html)

Global Objects - Simie - Dec 8, 2008 06:50 AM

How would one go about having an object accessible from any other object method?

eg: I have an object which manages textures, and want to be able to access it from the openGL view object. (or any other object).

How do I go about doing this, or is there a better way of doing it?

Thanks =)

Global Objects - AnotherJake - Dec 8, 2008 07:08 AM

You can use what is called a singleton.

Global Objects - ThemsAllTook - Dec 8, 2008 07:29 AM

Here's one approach.

+ (TextureManager *) sharedInstance;

static TextureManager * sharedInstance = nil;

+ (TextureManager *) sharedInstance {
  if (sharedInstance == nil) {
    sharedInstance = [[TextureManager alloc] init];
  return sharedInstance;

In some cases it may be a better design to pass a TextureManager instance to your view rather than having the view reach out and get one. This allows greater flexibility, as a view doesn't always have to use the same TextureManager instance. However, if you know you truly will only ever need one, the singleton approach is simpler and therefore better.

Here's some interesting reading about singletons:

Global Objects - Simie - Dec 8, 2008 08:36 AM

Thank you very much for that, it works!

Thanks for the links to, looks interesting.