Style/Structuring question: One vs. multiple global objects

Henning Hasemann hhasemann at web.de
Sun Jul 1 06:52:15 PDT 2007


Say I have a game engine in which nearly all parts including the
engines user need to access some global values.
Example for such would be the screen width & height, the current main
character, the currently displayed scene, the jukebox-object which does
sound stuff, etc...

I used to have one single global game object which stores all the other
things mentioned above because deep in my mind there's a "global
variables are evil" burned in.

Now where the stuff is getting more complex, this often leads to some
"forward reference" issues because the game module needs to import a
lot (scene module, character module, etc...) but also lots of modules
need game.
So this leads to circular imports which somehow leads to that forward
reference stuff.

Would it be a better idea to have multiple global objects for the
different things? I.e. a "currentScene" global in the scene module, a
"mainCharacter" global in the character module etc...?

What would you do?

Henning


PS:
I even wrote a small python script which tries to find cycles in the
"import graph" which demonstrated me that not every circular import
leads to a forward reference and that I have much more circular imports
that I'd expected.

-- 
GPG Public Key:
http://keyserver.ganneff.de:11371/pks/lookup?op=get&search=0xDDD6D36D41911851
Fingerprint: 344F 4072 F038 BB9E B35D  E6AB DDD6 D36D 4191 1851


More information about the Digitalmars-d-learn mailing list