Topic was automatically imported from the old Question2Answer platform.
Asked By
XxcringekidxX
I am trying to make a file that I can use to save the players coins and what level that they are on I have got the variables changing but it won’t save them plz help:
Heres my code:
extends Node
var Coins = 0
var Level = “res://Scenes/Levels/World2.tscn”
var Save_path = “res://Save folder/savefile.cfg”
var _config_file = ConfigFile.new()
func _ready():
save_settings()
load_settings()
func save_settings():
for section in _settings.keys():
for key in _settings[section].keys():
_config_file.set_value(section, key, _settings[section][key])
_config_file.save(Save_path)
func load_settings():
var error = _config_file.load(Save_path)
if error != OK:
print(“failed loading settings file.”)
for section in _settings.keys():
for key in _settings[section].keys():
_config_file.get_value(section, key)
pass
I’m new to the forum so please excuse formatting issues.
You are trying to save parts of your data to parts of the file. May I suggest you drop that, because the file won’t be very large if you “just” save a small amount of values. Just load and save the whole config-dictionary.
So let’s say you have the root node, put in the _ready function:
func _ready():
var config = load_config();
# If no config is present, initialize default values
# this is just an example
if config == null:
config = {
Levels = [
{
Name = "Forrest_01",
Result = "completed",
Percent_Complete = "78"
},
{
Name = "Desert_03",
Result = "failed",
Percent_Complete = "8"
}
],
Coins = "12700",
LivesLeft = "3"
}
# and save that default to a new file
save_config(config);
For this to work, you need the two functions load and save config:
func load_config():
# Check if there is a saved file
var file = File.new()
if not file.file_exists("user://saved_game.sav"):
print("No file saved!")
return
# Open existing file
if file.open("user://saved_game.sav", File.READ) != 0:
print("Error opening file")
return
# Get the config
var config = {}
config = parse_json(file.get_line())
return config
func save_config(config):
# Open a file
var file = File.new()
if file.open("user://saved_game.sav", File.WRITE) != 0:
print("Error opening file")
return
# Save the dictionary as JSON (or whatever you want, JSON is convenient here because it's built-in)
file.store_line(to_json(config))
# Here are two debug lines:
print("Saved config:")
print (to_json(config))
file.close()
I see that you do the following in the _process() function:
var Savepath = "res://Save folder/savefile.cfg
var _configfile = ConfigFile.new()
So, maybe you are creating a new file on every tick of the game? Try to put those lines outside the scope of the _process() function, most preferably in the beginning where you initialize the other variables.
Also, I know there is a thing with Python, where, if you do a for loop like:
for section in _settings.keys():
instead of:
for section in range(len(_settings.keys())):
then you cannot override the variables, meaning you cannot change their value directly. Maybe it’s the same with GDscript.