changing global values

Started by
3 comments, last by Captain P 14 years, 11 months ago
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?
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?
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.
[Window Detective] - Windows UI spy utility for programmers
I am very sorry!

I just made a silly mistake and found it and thought I deleted this post!

thank you though!
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. :)
Create-ivity - a game development blog Mouseover for more information.

This topic is closed to new replies.

Advertisement