🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Engine config files as python scripts

Started by
0 comments, last by CoffeeMug 21 years, 1 month ago
Here''s the situation: - A C++ program embeds a Python interpreter. - At any point user/programmer/player can bring up a console window and type in commands that will be passed on to Python interpreter to execute. Since many C++ classes will be exposed to python, it is a very flexible realtime debugging/tweaking solution. - A pyton (.py) module is used to define configuration variables. - A C++ class Config is used to load and execute the .py file with configuration symbols in a python interpreter. It is later used to get (and set) these symbols from C++ code. Note, at any time the user/programmer/player can bring up a console and change the values of these symbols. - C++ Config class exposes a simple callback mechanism. Different classes interested in different configuration values sign up for a callback when configuration values are changed (either by the user via a console or by other C++ code). I am running into the following problems: - What is the best way to represent configuration values as python files? I am interested in nested scopes (i.e. Graphics.Terrain.LodBias = 5) and I am unsure how to do this in python. One way is to create a class for each scope. Perhaps someone could suggest a better idea. - Since the user can change the value of any symbol through the console, I am not sure how to implement the callback mechanism provided by C++ Config class. If the user will simply type in LodBias = 5 in the console and will update the value of LodBias in the python interpreter, how can I let the Config class know about the update? Is there a way to overload the assignment operator in python for one module (in this case the module containing the configuration values). P.S. Sorry for the weird post format, listing the prereqs and the problems seemed like the easiest way to describe everything. P.P.S. I am new to python, so some of the questions may seem irrelevant. Please forgive my ignorance.
Advertisement
You could make a class, or more like a struct in Python to get that dot-notation.

class _Graphics:
''Simple Graphics Class.''
class _Terrain:
pass
class _Sky:
pass


def __init__(self):
self.Terrain = _Terrain()
self.Sky = _Sky()


Then just instantiate your class

Graphics = _Graphics()
Graphics.Terrain.LodBias = 5

You can of course make it more complex by defining the possible values and their defaults on __init__ instead of using the pass and just accepting any variable as I did.

Maybe one of the simple things to do in your console is have a set function?

set(''Graphics.Terrain.LodBias'', 5) ?

Interim

This topic is closed to new replies.

Advertisement