Global/Singleton or Local?

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

Post Reply
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Global/Singleton or Local?

Post by XianForce »

Okay, so I made a sort of 'framework' using SDL that simplifies things for me (at least that's what it's aimed to do, seems to complicate things haha).

Anyways, graphically it looks something like this:

Image

And any of the game systems I have, hold a pointer to the ApplicationSystem. But I'm finding other classes (which I'll be making multiple instances of), need access to some of the different components of the ApplicationSystem. If I give them pointers to the ApplicationSystem, I'd think that a singleton would likely be more efficient, though.

So should I make a global/singleton ApplicationSystem? And also, is it a good practice to have a structure like this? This is what logically made sense to me at first, but now I'm thinking I should separate the VideoSystem, AudioSystem, and InputSystem from the StateMachine.

So I'm just looking for a few opinions, before I do anything drastic.

Thanks in advance for any replies!
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Global/Singleton or Local?

Post by eatcomics »

The general consensus is no singletons (except for debug for some) and globals are frowned upon in object oriented programming, but you could very well use globals if you want... I'd say a pointer is a good way to go, it would be more efficient and safer than a singleton I would imagine
Image
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Global/Singleton or Local?

Post by XianForce »

eatcomics wrote:The general consensus is no singletons (except for debug for some) and globals are frowned upon in object oriented programming, but you could very well use globals if you want... I'd say a pointer is a good way to go, it would be more efficient and safer than a singleton I would imagine
Well I was thinking a global. Because if every, let's say, entity had a pointer to it, then I would think globals would be more efficient...
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Global/Singleton or Local?

Post by eatcomics »

you mean like a global pointer to it? that would be more efficient than all of them having it. but only a little, really if you're using pointers right there won't be much worry about efficiency... I'd say having a pointer to it in each class would be safer than a global that you could actually call somewhere you didn't mean to, but that's just what I would think, I still don't exactly know what you're talking about :lol:
Image
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Global/Singleton or Local?

Post by XianForce »

eatcomics wrote:you mean like a global pointer to it? that would be more efficient than all of them having it. but only a little, really if you're using pointers right there won't be much worry about efficiency... I'd say having a pointer to it in each class would be safer than a global that you could actually call somewhere you didn't mean to, but that's just what I would think, I still don't exactly know what you're talking about :lol:
Yeah I have a problem with explaining things >.>.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Global/Singleton or Local?

Post by eatcomics »

its okay, I just sometimes can't grasp other peoples concepts, anywho...
Image
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Global/Singleton or Local?

Post by qpHalcy0n »

I'd say for those objects that are persistent (eg: xxxxxMachine, xxxxEngine, xxxxFactory), then there's absolutely no problem whatsoever having a global pointer to the instance. A good and safe bet is to just define an externally linked pointer at the end of the class definition. Then you need a consistent application entry handler which will create all the objects and perform any initialization. You also need a consistent application exit handler which will clean up all the objects and perform any cleanup, so you only have two places to look for persistent objects really.

The whole "no globals" thing is just really psychotic. I never understood that whole deal, myself. The only compelling arguments against such a practice involve code readability, persistence, and namespace pollution. All of which are fixed with a little simple management, like above.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Global/Singleton or Local?

Post by XianForce »

qpHalcy0n wrote:I'd say for those objects that are persistent (eg: xxxxxMachine, xxxxEngine, xxxxFactory), then there's absolutely no problem whatsoever having a global pointer to the instance. A good and safe bet is to just define an externally linked pointer at the end of the class definition. Then you need a consistent application entry handler which will create all the objects and perform any initialization. You also need a consistent application exit handler which will clean up all the objects and perform any cleanup, so you only have two places to look for persistent objects really.

The whole "no globals" thing is just really psychotic. I never understood that whole deal, myself. The only compelling arguments against such a practice involve code readability, persistence, and namespace pollution. All of which are fixed with a little simple management, like above.
Yeah, that's what I had before, and I changed it for some reason... Can't really remember why though...

But thanks, that cleared things up. I'll make the ApplicationSystem global then =D
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Global/Singleton or Local?

Post by eatcomics »

I currently have 16 globals in my new project...
Image
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Global/Singleton or Local?

Post by XianForce »

eatcomics wrote:I currently have 16 globals in my new project...
Well, the way I see it, I'll only need to have the ApplicationSystem global... Because everything will be running via the State... Once it has access to audio, input, and video, every other system can be passed by reference =D... And it would be silly to make play time systems global I would think... Because other game states wouldn't use them...

but then again, I'm a noob, so maybe not haha
Post Reply