Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Robster | |
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