Sign in to follow this  
peter_b

How to store config settings in memory?

Recommended Posts

Hello. Iv just finished my xml parser, its for parsing config files for my games. The problem i have is, how to store the settings parsed from the config files nicely in the application memory. Lets say we have this file: <game> <graphics fullscreen=true resolution=1024x768> <levels> <level1 num_monsters=35> <level2 num_monsters=35> </levels> </game> My first approach was making a map with a string as a key, and some template hacks to store whatever type as value. Accessing the setting from the code would look something like this: bool fullscreen = settings.get_setting("graphics.fullscreen"); However, i don't like this. I don't want to do a map search every time i need a setting. What if a user forgets to store the value and does something like the above every time in the main loop? Not pretty. The second approach is alot simpler but is alot of work to maintain. That would be to just make a global variable with the name of the setting. For instance: extern bool g_graphics_fullscreen; The problem with this approach is that every time i add a new setting i need to add a new variable, and every time i remove one. Remove it too. I don't like it. :-/ But the first approach is not a whole lot better! I don't like to have to do map lookups and hard code c-strings into the code. Anyone got any neat solution to this? Thanks in advance!

Share this post


Link to post
Share on other sites
My personal preference is to encapsulate a map in a singleton. The map allows me to add dynamic settings without having to declare new variables and recompiling anything. The singleton allows me to access the settings from anywhere in a program and encapsulates everything nicely.

It's maybe a little slower than direct access, but what's a few clock cycles for the flexibility gained? ;)

Share this post


Link to post
Share on other sites
I would do both in a sense. Only access the map during initialization. If any value is needed at other times, then copy it into a local variable during initialization. I don't think that having to maintain a variable for some settings is a big deal. This also helps with the problem of missing settings.

However, let me restate your question.

"I have a clean and simple way of handling settings. How do I optimize it?"

  1. Do you need to optimize it? Have you profiled your code and found that accessing the map is slowing down your game? No sense wasting your time optimizing this if it doesn't even make a difference.
  2. Do you need to optimize it now? Are your optimizations going to complicate or interfere with other code? Are your optimizations going to survive to the end of the project? No sense optimizing this now if it slows down development or it is going to be replaced.

Share this post


Link to post
Share on other sites

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

Sign in to follow this