Jump to content
  • Advertisement
Sign in to follow this  
BlueBan007

changing global values

This topic is 3494 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

ok so I have a header file for my game and it has the functions save() and load(). I have many global game varibles, mainly charachter position and level... saving the data to a txt file is working and so is reading it, but the load() function cannot change the global values for some reason... and using reference paramaters would get ridiclous b/c of so many. my question is how can I make a global variable "available for editing" in other files?

Share this post


Link to post
Share on other sites
Advertisement
Unless the globals are "const", then you should be able to write to them. If not, can you make a minimal example that shows this behaviour and post it?

Share this post


Link to post
Share on other sites
Are you defining the variables in a header file and then including that file in multiple .cpp files? Because that will define those variables separately in each cpp file (like a copy) so when you read/write to a variable in one file it won't change the same variable in the other file. It's an easy beginners mistake that i have made a few time.

Share this post


Link to post
Share on other sites
Quote:
Original post by BlueBan007
and using reference paramaters would get ridiclous b/c of so many.

Yeah, passing too much arguments to functions is messy. But leaving them all as global variables is messy, too - it's hard to track down what code accesses what variables.

It's a good idea to organize your variables into classes, and then store these in other classes where that makes sense. For example, you can write a Player class that holds the player position, health, items, and so on. And a Level class that holds level information. And an Enemy class for your enemies. And when you end up with too much instances, wrap them up in a Game class or something, whatever makes sense in the given situation.

Know what? This Game class could have the load() and save() functions as member functions. They get access to the private variables of Game, and it's also more obvious what you're saving. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!