Skip to main content
GameDev.net gamedev.net
🔒 Locked

About Global Variables

Started by reaptide Aug 24, 2000 at 7:07 PM 10 replies 850+ views
Original Post
reaptide
reaptide
Hello everyone, I have a question about using global variables in a game. Usually what I do to store my globals is to create a source file with all my globals declared and initialized within it along with a header file with the same globals defined as externals. I then just include the header file wherever I want to use the globals. // Example.cpp int Global1 = 0; int Global2 = 0; int Global3 = 0; // Example.h extern int Global1; extern int Global2; extern int Global3; I was wondering if this is the most effective way to do this. I've asked a friend of mine and he thinks it's a perfectly acceptable way to handle globals. Any other opinions or alternate methods? Thanks, Reaptide Edited by - Reaptide on 8/24/00 7:10:56 PM
Quantum
Quantum
well thats the way i do it, and it works fine for me
Great Milenko
Great Milenko
heh well what I did was put them as extern in the header and stuff.. then decaler the vars at the top of the files that used them... ummm the most like...

Gfx.cpp

int ScreenWidth;
int ScreenHeight;
.
.
.

but I don''t do this any more... heh... I''m going total OOP =)

but anyways for now eather would work well =)

Great Milenko

Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."


http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech

The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
JD
JD
You could also put your global variables into a structure like so:

class MY_GLOBALS_TAG
{
public:
INT iNum1;
INT iNum2;
}g;

g.iNum1 = 10;

Or something like that


my homepage
E-Mail: BlueOrbSoftware@mailcity.com

Edited by - JD on August 25, 2000 3:23:01 AM
Knarkles
Knarkles
quote:
Original post by reaptide

I was wondering if this is the most effective way to do this. I''ve asked a friend of mine and he thinks it''s a perfectly acceptable way to handle globals.


Actually, it''s the only way.

-Jussi
todderod
todderod
You don''t need to give them the value 0, since globals are supposed to be of zero value anyway

And once more, if you ain''t doing it all OOP, then it is thw way to go, hehe
Kensai
Kensai
I could be missing something but what would be the point of a glabal if it had to be zero? Globals can be anything you want them to be.
Quantum
Quantum
yes you are missing something
they''re talking about the initialising of a global
Magicdude
Magicdude
you could also use a namespace, so you dont pollute the global namespace, its basically a structure with things dumped in it.

    
namespace gamespace
{
int global1;
int global2;
int global3;
};
[/source]

then you can use gamespace::global1 to refer to global1, or use the using keyword

[source]
using namespace gamespace;

global1 = 0;
Doctor Gonzo
Doctor Gonzo
I''ve always been told it''s bad to use globals, but sometimes it''s the easiest and best way to do things. Putting globals in a structure is probably the best thing to do too. One thing I do sometimes to make sure my globals don''t get changed in other files is to extern it as const.
Also, isn''t it better to access a local variable rather than a global one?
A man on the move, and just sick enough to be totally confident.Damn Right!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.