+1 vote

Hi all,

I have an issue where when I load a cfg file, it loads the wrong data it seems.

This is the file contents before I run the game:

[Scores]

totalGamesLost=0
totalGamesTied=0
totalGamesWon=0

Inside my globals file I start off like this:

var totalGamesWon   =   0

So within my script if I were to look at globals.totalGamesWon I would get a 0 on immediate startup.

The VERY first thing I do when I load the game is as such:

func _ready():
    configFileLoad()

Here is that function:

func configFileLoad():
    var fileToLoad= "user://stats.cfg"                                          # Path to save file
    var configFile= ConfigFile.new()                                            # Initiate ConfigFile
    configFile.load(fileToLoad)                                                 # Load file
    var err = configFile.load(fileToLoad)
    if err == OK:

        ###########################################
        # This first section for totalGamesWon loading a "2"! BUG BUG BUG Why?
        ###########################################
        if (configFile.has_section_key("Scores", "totalGamesWon")):
            globals.totalGamesWon = configFile.get_value("Scores", "totalGamesWon")
            print("loaded totalGamesWon: ", globals.totalGamesWon)

        if (configFile.has_section_key("Scores", "totalGamesLost")):
            globals.totalGamesLost = configFile.get_value("Scores", "totalGamesLost")
            print("loaded totalGamesLost: ", globals.totalGamesLost)

        if (configFile.has_section_key("Scores", "totalGamesTied")):
            globals.totalGamesTied = configFile.get_value("Scores", "totalGamesTied")
            print("loaded totalGamesTied: ", globals.totalGamesTied)

... and finally, looking at the above 3 x print statements I get this:

loaded totalGamesWon: 2
loaded totalGamesLost: 0
loaded totalGamesTied: 0

I can't for the life of me figure this out. I've checked, double checked, tripple checked. I really hope it's something simple that I'm missing. Any help GREATLY appreciated.

in Engine by (824 points)

I wonder if that print is showing the loaded value, can you try using breakpoints to check values or adding a button to the first scene to trigger a print later?

Thanks for your answer. I am not too familiar with breakpoints as yet but I added print statements to various times after the startup and still get the same result. I tried at steps all the way to where the game starts and still the same. It MUST be something simple but I just can't see it.

I FOUND IT! It's related to this post: https://godotengine.org/qa/10776/saving-config-doesnt-with-user-filename-does-with-filename

The user vs res loading of the file. One is working and the other isn't. As soon as I changed it to var fileToLoad= "res://stats.cfg" then it loaded the config file properly.

I'll put this in the other answers also to help. Thanks again, now I just have to figure out how to solve that other issue (user VS res).

1 Answer

0 votes

Are you sure you can set variables on globals?

The only reason I can think of that your program would be doing this might be because globals.totalGamesWon isn't settable. or you've set the config file previously and forgot about it.

Best solution would be to not use a globals module, if the only thing it's going to be used for is storing values.

If you really do need a globals module, then make sure that globals is the only one that accesses the config file directely. If you want to save, have a save function on the globals module, which other programs can call without having to worry about the underlying config file.

If you seperate the scripts that have access to permanent variable from the ones that might use it in the future, it will be much easier to see where you might have went wrong

by (124 points)

Thanks for your detailed answer. I moved the two functions (save and load) for the cfg files into the global but it's still the same issue. I'm more than a little stumped. I'll keep at it though.

It appears the first issue is it's loading a 2 rather than a 0. If I manually edit the config file to say, a score of 11, it still loads as a 2. Most strange.

I FOUND IT! It's related to this post: https://godotengine.org/qa/10776/saving-config-doesnt-with-user-filename-does-with-filename

The user vs res loading of the file. One is working and the other isn't. As soon as I changed it to var fileToLoad= "res://stats.cfg" then it loaded the config file properly.

I'll put this in the other answers also to help. Thanks again, now I just have to figure out how to solve that other issue (user VS res).

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 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 webmaster@godotengine.org with your username.