Skip to main content
GameDev.net gamedev.net
🔒 Locked

Using python as a configuration file

Started by daniel_i_l Sep 29, 2010 at 9:18 AM 8 replies 2.2k views
Original Post
daniel_i_l
daniel_i_l
Hi, I'm writing a program in C++ and want initialize it with a configuration file. I usually use libconfig but in this case I have many similar configuration settings and in many cases it'd be easier to write the configuration file using basic programming constructs like loops.

So I thought about using python scripts for the configuration files. The file would contain simple dictionaries and lists which may be constructed either by hand or using basic python scripting.

My question is, assuming I have a file called config.py, what's the easiest way to read all of the top level variables (integers, lists, dictionaries) from inside a C++ program?

I know that by using the Python C-API I can access dir(config) and then use PyDict_GetItemString to get different variables. But what if I have something like:
list1[{'a':1}, {'a':2}]
in the config.py file? Is there an easy way to get the value of list1[0]['a'] without having to use multiple specialized C-API calls? I was thinking of something like this:
//these line are run oncePyRun_SimpleString("import config");PyObject* module = PyImport_AddModule("__main__");//these three lines can be used for any amount of nestingPyRun_SimpleString("exec('cVar = config.list1[0][\\'a\\'])"); PyObject* dict = PyModule_GetDict(module);PyObject* result = PyDict_GetItemString(dict, "cVar");

The problem with this is that it's just a hack. Is there any better way to approach this problem?
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
SiCrane
SiCrane
One choice would be not to use the Python C API directly, but use a wrapper like boost::python.
Promit
Promit
I'd also suggest that Lua would be a much easier choice than Python.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Steve132
Steve132
I would guess because it's easier to embed (its a lot easier to embed).

However, once you have embedded it you can use it in general for a general-purpose scripting language, and if you are considering doing this, then python is a much better syntax imho.
theOcelot
theOcelot
Lua is a heck of a lot easier to embed, and it's really easy to get data out. If you only need to get a few primitive types in and out, it would just be a few calls to set up the interpreter, run the input file, and grab globals. Extracting specific values from tables, which serve as both lists and dictionaries, is only slightly more work.

In defense of Lua, its syntax is tough to beat for simplicity, which is good for simple scripting tasks. I would only go with Python if Lua's features were really inadequate.
reptor
reptor
I've used Lua scripts as settings files in my application but one thing not quite good about that is that if there is an error in just one line of the file then it rejects the whole file.

So when I have default settings (hard-coded in C++) and run a script to over-ride them and if there is an error in the script then only the default settings are used.

It would be better if it would only reject the setting from the one line that had an error on it. I don't know how to implement this with Lua yet.

And in order to fix the error in that one line I would need to be able to identify exactly which setting was it that was wrong. I wouldn't want to over-write all of the settings the user has created with the default settings. That would fix the error but the user would lose all of his changes to the settings.

I guess using an exception handler of some sort could help? Maybe someone dealt with this problem already?

I can imagine the same problem could happen with Python too.

[Edited by - reptor on September 30, 2010 10:42:47 AM]
daniel_i_l
daniel_i_l
Thanks for the suggestions. I'll take a look at boost and lua.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
y2kiah
y2kiah
&#106avascript hasn't gotten much cred in the gaming world thus far, but I would not ignore it as an option. I believe the Wolfire guys are using Google's engine. I like JSON a lot for data description, prototypal inheritance is ridiculously useful in scripting language, and the C-like syntax feels like home.
Sneftel
Sneftel
The advantage to Lua is that Lua was originally designed as a configuration language. It has various syntactic sugar to help with this, and its API is well-designed for modular readout and querying of hierarchical data.

Incidentally, decide very early on what sort of computation you want going on in your "configuration" file. If you use Lua or Python for data, there's a great temptation to start doing processing in them; it starts with a couple conditionals, then you start adding loops, and before you know it a significant part of your game is housed in user-config.cfg. This is especially likely if you aren't exposing the same language for scripting in the application. Beware. (JSON doesn't suffer from this problem, because you pick your JSON reader based on what execution capabilities you want.)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.