Jump to content
  • Advertisement

Archived

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

Chryzmo

Global Variable Concept Questions...

This topic is 5484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was poking around on these forums and it seems people discourage the use of global variables. I was wondering how come? I used some in a project of mine. I am still very new to C++, so this project was just to see if I coudl put what I had just read together into something useful. My project was a text-based RPG battle type game. I had seperate .cpp fikes for the battle and the shop. There were a number of values I felt needed to be carried over to each section of the program. Basically, they were money, health, and power(how much damage the player could do per turn). So, I used global variables. Is this bad or is it just a situation where global variables are necessary? I am not sure whether of not the source code would help, but I do not have it on this computer. If you think it may help I can get it tommarrow and post it. Thanks, Chris

Share this post


Link to post
Share on other sites
Advertisement
Go back and try to add a second character to the game, keeping track of each character''s seperate money, health, and power. Then, you''ll begin to see one of the problems with global variables.

Share this post


Link to post
Share on other sites
quote:
Original post by kdogg
Go back and try to add a second character to the game, keeping track of each character''s seperate money, health, and power. Then, you''ll begin to see one of the problems with global variables.


hehe that''s a lot of stuff to keep track of isn''t it?

If you haven''t done so already I would recommend that you at least group all those stats/atributes/hp/whatnots into a struct and then you can just create as many chars as you want using that struct. Perhaps even better you can create a class just for the character.

Share this post


Link to post
Share on other sites
Sometimes global variables are neccesary. for example I have to have all my SDL surfaces global as they are referenced from different bits, and youy can''t have one type inside another, according to the error messages I got when I tried.
There''s some things that need it, but don''t store stuff like player stats etc global, and if you do, put it in a class or struct.

Share this post


Link to post
Share on other sites
Global variables are one way of getting things to work, but there is supposedly always a non-global alternative.

If you''re not careful with it, you could design yourself into a corner and have to redo some things. That''s true for using globals or not.

Share this post


Link to post
Share on other sites
Global variables aren''t necessarily "bad". There a few reasons why they''re discouraged. First because you don''t know who else is messing with your data. By "who else" I mean other functions. With global data that''s used a lot, tracking down sporadic values can be a pain. Global variables are also bad when you''re working on a project, since you don''t know what global variables they are using, and vica versa. That''s not really much of an issue any more.
I prefer to stay away from them because they make code harder to read.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!