config file is changing / overwriting contents rather than appending

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi all, I have this as my save score function:

func configFileSave():

	var file_to_save= "res://stats.cfg"									
	var configFile = ConfigFile.new()									

	if difficulty == "easy":
		configFile.set_value("Scores","totalGamesWonEasy",totalGamesWon)	
		configFile.set_value("Scores","totalGamesLostEasy",totalGamesLost)	
		configFile.set_value("Scores","totalGamesTiedEasy",totalGamesTied)	
		configFile.save(file_to_save)										
		print("TotalgamesTied SaveFile: ", totalGamesTied)
	elif difficulty == "medium":
		configFile.set_value("Scores","totalGamesWonMedium",totalGamesWon)	
		configFile.set_value("Scores","totalGamesLostMedium",totalGamesLost)	
		configFile.set_value("Scores","totalGamesTiedMedium",totalGamesTied)
		configFile.save(file_to_save)											
	elif difficulty == "hard":
		configFile.set_value("Scores","totalGamesWonHard",totalGamesWon)	
		configFile.set_value("Scores","totalGamesLostHard",totalGamesLost)	
		configFile.set_value("Scores","totalGamesTiedHard",totalGamesTied)	
		configFile.save(file_to_save)										

If I’m on hard setting and the score is created at the end of the game, it all writes just fine. It looks something like this:

[Scores]

totalGamesLostHard=1
totalGamesTiedHard=0
totalGamesWonHard=1

Great stuff. I then swap to say, easy, and run the game. Unfortunately though, the score gets changed to this:

[Scores]

totalGamesLostEasy=0
totalGamesTiedEasy=0
totalGamesWonEasy=1

Now I would have thought that if I had played on hard, then played on easy, it would then have both in the file, like below (but it doesn’t):

[Scores]

totalGamesLostHard=1
totalGamesTiedHard=0
totalGamesWonHard=1
totalGamesLostEasy=0
totalGamesTiedEasy=0
totalGamesWonEasy=1

Can anyone offer some advice as to why it’s not appending and why it’s re-creating the file and placing new contents each time?

Thanks so much…

i try your code and the result is same like yours

ruruarchy | 2019-12-06 05:32

:bust_in_silhouette: Reply From: ingo_nikot

im suprised that your 3rd example has 6 entrys. Afaik the config file only overwrites on save and does not append.

solution could be to load and store all 9 entrys every time.

i suggest not useing a config file for saveing your game but json:

func save():
	var savegame = File.new()
	savegame.open("res://savegame.save", File.WRITE)
	savegame.store_string(scores.to_json())
	savegame.close()

func load_savegame():
	var savegame = File.new()
	savegame.open("res://savegame.save", File.READ)
	var file_string = savegame.get_as_text()
	scores.parse_json(file_string)

“scores” is a dictionary that contains your score data:

scores["hard_games_won"]=0

Thanks for your helpful response. I decided to go and rewrite it all with json, but then realised that was a whole new learning experience and I’m SUPER close to finishing the game… so I simply went in and loaded all of the score types (easywon, easy lost, hardwon, hardlost, etc etc) each time, so I had them all at the ready and continued with the config option.

Next game I’ll use the json option though so I can learn a better way to do it.

Thanks again for your input, really appreciated.

Robster | 2017-01-09 00:42

:bust_in_silhouette: Reply From: ruruarchy

hey Robster, try adding this

configFile.load(file_to_save)

before saving,
it will be like this

if difficulty == "easy":
	configFile.load(file_to_save)
	configFile.set_value("Scores","totalGamesWonEasy",totalGamesWon)    
	configFile.set_value("Scores","totalGamesLostEasy",totalGamesLost)  
	configFile.set_value("Scores","totalGamesTiedEasy",totalGamesTied)  
	configFile.save(file_to_save)

add it also to medium and hard difficulty,

the result will be :

[Scores]

totalGamesLostHard=1
totalGamesTiedHard=0
totalGamesWonHard=1
totalGamesLostEasy=0
totalGamesTiedEasy=0
totalGamesWonEasy=1

i just tried it and its works !

configFile.load(file_to_save) will return int 19 if there isn’t any file to load ,looks like its enum error 19 : ERR_CANT_OPEN, and int 0 if that file already exist (has been saved before)

ruruarchy | 2019-12-06 06:16

and also maybe you want to change the section name ,like Scores to ScoresEasy , ScoresMedium , ScoresHard, just for more easier to read

ruruarchy | 2019-12-06 07:04