Stuff with globals and templates and throw in some
289
0
Advertisement
I'm planning on working on a puzzle game for awhile, because...well...I feel like working on a puzzle game. Plus I have a really cool idea for what it's going to play like.
But that isn't exactly why I'm posting this entry, well other than the fact that I like to make it a daily event. I wanted to share my new code for storing global variables. I'm pretty sure this is a common method. Also, I haven't actually compiled this, so it might have some errors. It is in this class:
This class is so simplistic I'm quite sure it has to already be used.
So basically, you can do this:
This system is good because you can load the variables from a file.
I'm trying out STL maps for alot of different tasks and they seem to work well so far.
But that isn't exactly why I'm posting this entry, well other than the fact that I like to make it a daily event. I wanted to share my new code for storing global variables. I'm pretty sure this is a common method. Also, I haven't actually compiled this, so it might have some errors. It is in this class:
template <class T>class Globals{ private: std::map globals; public: void set(std::string key, T value) { globals[key] = value; } T get(std::string key) { return globals[key]; } std::map getList() { return globals; }};This class is so simplistic I'm quite sure it has to already be used.
So basically, you can do this:
Globals<int> numbers;// Create some numbersnumbers.set("x", 5);numbers.set("y", 7);numbers.set("z", 10);// Print the value of xstd::cout << "x is: " << numbers.get("x") << std::endl;// Print all the numbersfor(std::mapint>::iterator i = numbers.getList().begin(); i != numbers.getList().end(); i++) std::cout << i->first << " = " << i->second << endl;This system is good because you can load the variables from a file.
I'm trying out STL maps for alot of different tasks and they seem to work well so far.
Advertisement
Advertisement
Advertisement
Discussion