1
\$\begingroup\$

Considering game-development?

I have this question in a gamedev quiz and I wonder what to answer.

Any clues?

\$\endgroup\$
2
  • \$\begingroup\$ Can you clarify programming language? \$\endgroup\$ Jan 9 '15 at 12:26
  • \$\begingroup\$ Language is C++ \$\endgroup\$
    – PaulD
    Jan 9 '15 at 13:27
1
\$\begingroup\$

Main problem with global variables in C++ that compilers don't guarantee initialization order. Let's you have 2 global variables with constructors:

global_A g_a;
...
global_B g_b;

...

global_A::global_A()
{
    this->test = 5; // test is int
}

...

global_B::global_B()
{
    printf( "%d", g_a.test );
}

If initialization order of g_a and g_b is undefined, then sometimes you can get situation when constructor of g_b would be called before g_a and variable g_a.test would be undefined. The worst thing - sometimes it works well.

Note, if global variables in same source file then you may be sure that order will be same as global variables are defined. But using global variables with dependencies is bad practice because other people who will work with this code may not know about dependency between global variables and got troubles if move one of global variable to other file.

\$\endgroup\$
5
  • \$\begingroup\$ I think I recall that Scott Meyers proposed a solution to this problem, it is using global function "getters" fro global objects. So when you call it, you guarantee will have global objct initialized. \$\endgroup\$
    – PaulD
    Jan 9 '15 at 14:22
  • \$\begingroup\$ Yes, it works but it's not global variables. You can make this "global" object as static object in "getter" function. And in this case you'll have same functionality without any global variables. \$\endgroup\$ Jan 9 '15 at 14:26
  • \$\begingroup\$ Maybe your question should be something like "Why people don't like Singleton pattern?". In this case you'll get other answers, because global variables is one of many ways to implement Singleton. \$\endgroup\$ Jan 9 '15 at 14:44
  • \$\begingroup\$ It answers only a half of my question. The second is about class constrcutors. \$\endgroup\$
    – PaulD
    Jan 9 '15 at 15:46
  • \$\begingroup\$ I don't know what "very simple" means for constructors. If you mean less code or less dependencies, then I don't know any restrictions or common problems with big constructors with many dependencies (except readability). RAII technique uses constructors and destructors that make a lot of work and has no problem with it. \$\endgroup\$ Jan 9 '15 at 16:30
0
\$\begingroup\$

Global's initialization happens before main() gets called and the UI setup stuff happens if that takes too long then the game window may take a long time to show up.

Common rule in UX (user experience) is to react to user input as fast as possible even if the result is not yet available.

This is a common setup for games as they will show a semi-static screen for some time as they load all the needed assets before showing the main menu.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.