edit prc file variables from program and save changes?

Hello again.
I have a settings GUI app which pops up when you start the game and it allows you to configure some settings like resolution, graphics api, audio api, texture quality, etc.
I know I can have a config text file and use that.

file = open('config.txt', 'r')

for i in file.readlines():
    loadPrcFileData('', i)

and just completely rewrite that file each time the settings are changes with that startup program.
This will overwrite any identical variable setting from the original Config.prc file and will use the rest from Config.prc.

But I would prefer to not have two separate configuration files for the game.
So I would like to know, is there an easy way to modify a value from the original prc file and save the changes?
If not I guess I could read the Config.prc file, split the lines into list, skip lines which start with “#” and use split(" ") on the rest and work on that.

You don’t have to load the prc file one line at a time; you can load it all at once with:

file = open('config.txt', 'r')
loadPrcFileData('', file.read())

Or even better with:

loadPrcFile('config.txt')

If you want to modify and re-write it, you could save the ConfigPage object that corresponds to the file, which is the return value from either loadPrcFileData() or loadPrcFile():

page = loadPrcFile('config.txt')

And you can iterate through the ConfigDeclaration objects in the page via getNumDeclarations()/getDeclaration(n). For each declaration, you can change the value with setStringValue(), or setIntWord()/setBoolWord()/setDoubleWord() and so on. Finally, you can rewrite the file with page.write().

Not sure if this is really the best interface, though. The config file system is really intended for user (or developer) settings, but I guess you could also use it to save runtime-configurable settings.

David

Hi!
I’m trying to make this work but I’m having problems. I wrote this code:

page = loadPrcFile("etc/GameOptions.prc")

for i in range(page.getNumDeclarations()):
    declaration = page.getDeclaration()
    variableName = page.getVariableName(i)
            
    if(variableName == "win-size"):
        sizeX = base.win.getProperties().getXSize()
        sizeY = base.win.getProperties().getYSize()
        declaration.setStringValue(i, "%s" % sizeX + " %s" % sizeY)

And I’m getting this error:

Am I doing something wrong?

Ops… I miss the ‘i’ in the last post. Actually the code is:

page = loadPrcFile("etc/GameOptions.prc") 

for i in range(page.getNumDeclarations()): 
    declaration = page.getDeclaration(i) 
    variableName = page.getVariableName(i) 

I get the same error :frowning:
[/quote]

Ah, my mistake, my apologies. I failed to publish all of the needed functions to actually do this. I’ve just committed the appropriate fixes, which should appear shortly in the next buildbot release, if you’d like to pick it up and try it there.

Note that you will also need to replace “page.getDeclaration(i)” with “page.modifyDeclaration(i)”.

David