Why is this loading as a 2 when it should load as a 0?

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

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?

eons | 2016-12-18 21:40

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.

Robster | 2016-12-18 23:51

I FOUND IT! It’s related to this post: Saving a config file doesn't work with "user://filename.cfg" but it does with "res://filename.cfg" - Archive - Godot Forum

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

Robster | 2016-12-18 23:55

:bust_in_silhouette: Reply From: Jatz

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

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.

Robster | 2016-12-18 23:48

I FOUND IT! It’s related to this post: Saving a config file doesn't work with "user://filename.cfg" but it does with "res://filename.cfg" - Archive - Godot Forum

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

Robster | 2016-12-18 23:55