• Create Account

## Structure of classes in good Game Engine?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

46 replies to this topic

158
Like
0Likes
Like

Posted 21 June 2012 - 09:45 AM

I am not a huge fan of globals but when I am forced to use them, I usually do. The way I indicate if I should use them or not, is if they are used by the whole application and if they are dependencies. If they depend on a class, or a class depends on it, then I will not use it. A good example would be a logger. It does not require anything from other classes, it just holds a few functions and outputs information to a file/console. I most likely wouldn't even make it a class, it would just be a few static functions that I would call. At most, it would probably be two or three global variables.
There is a time where using globals too much can make it a pain to debug, but for a simple logger, it won't be too difficult to debug, especially when it's purpose is to help you debug

### #42Narf the Mouse  Members

318
Like
0Likes
Like

Posted 21 June 2012 - 10:05 AM

I found the service locator pattern (as described here http://gameprogrammi...ce-locator.html) a good alternative to the singleton. Its still very global but not that restrictive anymore.

Looks rather useful, and not just as a static variable.

### #43Nanook  Members

533
Like
0Likes
Like

Posted 21 June 2012 - 05:01 PM

Cygon mentioned it as a anti-pattern though..

• Passing some semi-global application class around is just hugely increasing dependencies since now you don't know which objects a class will look up through the application class (the service locator anti-pattern).

### #44alnite  Members

3271
Like
0Likes
Like

Posted 21 June 2012 - 05:21 PM

Seriously, it's just for logging. Don't overengineer and overthink the solution. A thread-safe and simple function is generally sufficient. If you want to log to different outputs (file, network), use FILE structure or std::ostream.

MyLogger::log(std::ostream& stream, const std::string& what);

Done.

### #45Nanook  Members

533
Like
0Likes
Like

Posted 22 June 2012 - 03:00 AM

yes logging is one thing, but the discussion is also valid for other stuff..

### #46web383  Members

804
Like
1Likes
Like

Posted 22 June 2012 - 04:51 PM

For higher level objects or systems, dependency injection is always an option.

http://martinfowler.com/articles/injection.html

### #47Cygon  Members

1215
Like
0Likes
Like

Posted 24 June 2012 - 11:30 AM

I found the service locator pattern (as described here http://gameprogrammi...ce-locator.html) a good alternative to the singleton. Its still very global but not that restrictive anymore.

Cygon mentioned it as a anti-pattern though..

• Passing some semi-global application class around is just hugely increasing dependencies since now you don't know which objects a class will look up through the application class (the service locator anti-pattern).