Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » ...of mice and globals

This thread is locked; no one can reply to it. rss feed Print
...of mice and globals
Dizzy Egg
Member #10,824
March 2009
avatar

Hello

I am intruiged to find out a bit about how people approach declerations of their classes within their C++ games/projects.

I usually declare global instances of my classes within my main.cpp file that I then extern into functions.cpp (etc). A bit sloppy maybe but hey-ho I'm impatient. But I hear (on the grapevine) that globals really ARE frowned upon, and I want to improve my behaviour in the 'best' way possible.

Do people generally have a singleton class, such as 'game' that contains instances of all the other required classes? Or do they create instances within, say, a 'game()' function and then pass them to the required functions?

Just looking for opinions really :)

Dizzy

----------------------------------------------------

www.facebook.com/dontrobthemachina

X-G
Member #856
December 2000
avatar

Mainly, namespaced systems.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

imaxcs
Member #4,036
November 2003
avatar

In C#, I always use an dependency injection framework which handles the whole singleton issue for me. The object itself has no clue if it's a singleton or not.

For C++, I'm afraid it's not quite that easy. Googling IoC for C++ only reveals PocoCapsule which I have no experience with. :(

In general, I would try to stay away from global state as much as you can. But in some cases it can't be avoided without producing even worse code.

Dizzy Egg
Member #10,824
March 2009
avatar

imaxcs said:

In general, I would try to stay away from global state as much as you can. But in some cases it can't be avoided without producing even worse code.

Good point well made, and also the reason I'm interested in how the C++'ers here approach it.

I'll look into namespace usage.

----------------------------------------------------

www.facebook.com/dontrobthemachina

Elverion
Member #6,239
September 2005
avatar

I dislike using singletons myself. You still end up with globals doing that. You could, I suppose, have your Game and GameEntity classes hold a parent pointer (NULL for Game, pointer to parent for everything else), and travel up the list till you hit NULL, then return the this pointer. Sometimes, you'll want to access an object's parent anyways, so this could be useful. If you want speed, you could copy the Game pointer to each GameEntity when it's created instead of having to traverse parents.

--
SolarStrike Software - MicroMacro home - Automation software.

Tobias Dammers
Member #2,604
August 2002
avatar

Elverion said:

If you want speed, you could copy the Game pointer to each GameEntity when it's created instead of having to traverse parents.

I doubt this would make any notable difference at all.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Indeterminatus
Member #737
November 2000
avatar

I second the "Inversion of Control" idiom mentioned by imaxcs. Haven't been doing any C++ coding as of late, so I too can't tell you if/how well it works.

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

relay01
Member #6,988
March 2006
avatar

singletons are nice, but it's often used as a global instantiation anyway. so if your going to use a singleton as a global, then why not just use a global? At least of you are using a global instantiation all over the place, you'll easily be able to find all the references to it within your code via search.
there is a trade off to either choice.

I've never setup a namespace before... I should look into that.

_____________________________________

My Website
"I got so fed up with this mind numbing experience that one day, I just went outside. That is how bad it was. And let me tell you, I do not want to have to go outside again. It looks nothing like Minecraft."-Billybob

Tobias Dammers
Member #2,604
August 2002
avatar

relay01 said:

singletons are nice, but it's often used as a global instantiation anyway.

A singleton is nothing but a wrapper class around a bunch of globals. The only thing you gain by using a singleton is that you can lazy-init your globals when needed, and that you can refactor it into a non-singleton class later, in case you have to. Another useful property of singletons is that you can have an actual constructor, but then, this also gives room to a lot of nasty "worst practices", such as constructors with side effects, etc. etc. yadda yadda.
Finally, there's the threading issue, which basically means that you need to make sure your singleton instantiation is properly protected, otherwise you might end up with more than one instance after all. And, depending on the situation, the term "single instance" is relatively vague; consider a web application of which several copies are installed (say, to provide some sort of load balancing or whatever) - do you want one copy per request, per session, per user, per application instance, per server, or one copy period?

Quote:

so if your going to use a singleton as a global, then why not just use a global?

Because it still encapsulates somewhat better than a plain bunch of globals. In most cases, however, there isn't a reason to enforce singleton behaviour. Even with the classic example of a data access layer that uses a singleton to represent the database connection, there is little reason to disallow a second connection, as long as you make sure all connections are closed under all circumstances. A singleton-like getDefaultInstance() method is still useful though.

Oh, and I have absolutely no clue at all how namespaces are supposed to be related. Namespaces avoid identifier name clashes while still allowing concise names, but I don't see how they can make globals any safer.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Thomas Fjellstrom
Member #476
June 2000
avatar

Oh, and I have absolutely no clue at all how namespaces are supposed to be related. Namespaces avoid identifier name clashes while still allowing concise names, but I don't see how they can make globals any safer.

Just helps keep stuff more organized.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro SVN Snapshots] - [Allegro TODO] - [Web Hosting]
"God Bless Joe Pesci" -- George Carlin
"Goto is the buldozer of coding. Sometimes, the buldozer is just the right tool for the job. Not often, but sometimes." -- LordBob

Tobias Dammers
Member #2,604
August 2002
avatar

Yeah, I know, and I love namespaces for this, but globals in a namespace are just as global, with just as little control over which part of your (or someone else's) code uses them.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Go to: