Archived

This topic is now archived and is closed to further replies.

Shaun_30

Best way for Globally Accesible Variables

Recommended Posts

What''s the best way to do have globally accessible variables in C++? Endless amounts of externs in a header file making every little change to the header a recompile of every document using it, or is there a better way through the use of OOP (which im new to)?

Share this post


Link to post
Share on other sites
The best way is to not have them at all...



The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites
The main reason I need them is for the sake of orginzation... for example I have a function that draws a mouse icon onto the engine im making. It needs the mX (mouse X) and mY (mouse Y) to draw it correctly, I have this moved into its own *.cpp file. Now you''d think for a solution I could have this local in said *.cpp file, but what about when I implement mouse click detection, will it not need these points (actually I know it needs these because its already implemented), not to mention mX and mY''s dependenices in the Main.cpp file. Now I could lump everything into one big file and everything would be ok right? Well, besides for the added compile time, looking time for specific areas, general organization, and the confusing necessary use of protoype declarations im gonna need, but those are onjly little things, right?

Share this post


Link to post
Share on other sites
It''s not theory for me...



The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites
quote:
Original post by Shaun_30
...


You know that functions take parameters, yes...?





The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
You know that functions take parameters, yes...?


Yes, but can you tell me how this can help me?

EDIT: How I can have a typo in such few words I'll never know...

EDIT #2: Oh, I bet you intend for me to make a function take the mX and mY params. pseudo function being...
void MouseMove(float *MouseX, float *MouseY);
...correct?
well if so then thats fine and dandy, but what I was implying was I have many instances where I need global variables, but if what above is the kind of trick that game programmers use to overcome this problem then I guess this is how I could do it...answer please...

[edited by - Shaun_30 on October 20, 2002 7:03:28 PM]

[edited by - Shaun_30 on October 20, 2002 7:12:13 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Shaun_30
EDIT #2: Oh, I bet you intend for me to make a function take the mX and mY params. pseudo function being...
void MouseMove(float *MouseX, float *MouseY);
...correct?


Yeah. Or you could use references instead of pointers. Or even make the point an aggregate structure, and pass a pointer/reference to that.

quote:

well if so then thats fine and dandy, but what I was implying was I have many instances where I need global variables,


No, you don''t. You have many instances where you think you need global variables. Globals are never necessary,although I guess there could be circumstances in which their use was preferrable to the alternative.

Personally, I can''t remember the last time I used a global in something I wrote.
quote:

but if what above is the kind of trick that game programmers use to overcome this problem then I guess this is how I could do it.


Trick??? Parameter passing is a very fundamental aspect of almost any language.



The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites
I''m more comfortable with pointers although I''ll take a look at what you suggested...by trick I didn''t mean something used cheaply to get something done, but as something those who have more experience use to get something done (which is pretty much what you said)...time for a complete background overhaul for my engine-in-progress...thank you for your input .

Share this post


Link to post
Share on other sites
Actually do you think that you could show me an example of utilizing an aggregate structure and passing a pointer or reference in this type of a situation? My quick-search on google has yielded no fruitful results.

Share this post


Link to post
Share on other sites

  
struct Point
{
float x;
float y;
};

//...


void mouseMove( Point& pt )
{
//...

pt.x = <something>
pt.y = <somethingelse>
}







The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites
I'm assuming that you know of classes, but probably don't use them. Using classes is the best way to get around having global variables, because the class members act like global variables within the class itself. Thus you could make a mouse class and give it Mouse X and Mouse Y members, as well as all the update code you need. It REALLY makes things easier if you take the time to learn how to use them, otherwise, it does get tricky to not have to use globals.

[edited by - Mulligan on October 20, 2002 8:00:21 PM]

Share this post


Link to post
Share on other sites
Thank you so much! The thing I''ve always forgotten was the "&" sign at the declaration of the structure, making the usefulness of a structure limited to that of the scope of the function where it is declared. Now my project can move into a direction of organization I never thought possible. Thanks again.

Share this post


Link to post
Share on other sites
You shouldn''t have all global variables and you shouldn''t have all member, local, or referenced, or whatever else I''m forgetting. There''s a limit. I prefer globals because they''re fast and easy to access. However they don''t work for everything. Too many global variables makes things confusing so there becomes a time when you need to store them in structures and classes. Also passing variables through function is helpful when you don''t know exactly what data should come in and out and need pointers to help you out.

---
Brent Gunning | My Site

Share this post


Link to post
Share on other sites
Thank you Mulligan, again we go back to the fact I always forgot the "address of" sign when declaring structs (or in this case, classes) eliminating any usefulness a class would have for this situation. I am now considering a hybrid between yours and Arild Fines''s posts.

Share this post


Link to post
Share on other sites
I think you need to go over the basics of C++ again before you continue with your engine. You're forgetting really fundamental things, and if you don't remember basic C++ the structure and organization of your engine is really going to suffer.

EDIT: Arild Fines was not using an "address of" sign. It was a reference, which is somewhat different from a pointer.

[edited by - micepick on October 20, 2002 8:16:26 PM]

Share this post


Link to post
Share on other sites
Its really more an intro to OOP than it is a serious engine, again a lack of proper terminology. Its not like im writing an 0GL particle engine. Right now it loads in a map from a *.txt file, makes tiles interpereted from the map, and draws them onto the screen. I have recently set up mouse movement, and mouse clicking collision in attempt to see HOW it works, and was looking for ways to refine what I have done so far by fixing my extern/global problem when I first posted.

Share this post


Link to post
Share on other sites