Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Stuff with globals and templates and throw in some

Sign in to follow this  


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:

template <class T>
class Globals
std::map globals;
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 numbers
numbers.set("x", 5);
numbers.set("y", 7);
numbers.set("z", 10);
// Print the value of x
std::cout << "x is: " << numbers.get("x") << std::endl;
// Print all the numbers
for(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.
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 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!