Best way for Globally Accesible Variables

Started by
15 comments, last by Shaun_30 21 years, 6 months ago

  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)
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Advertisement
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]
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.
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
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.
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]
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.

This topic is closed to new replies.

Advertisement