+1 vote

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...

in Engine by (837 points)

i try your code and the result is same like yours

2 Answers

+2 votes

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
by (295 points)

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.

0 votes

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 !

by (71 points)

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.