0 votes

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 _settings = {
"Levels": Level,
"Coins": Coins

func _process(delta):

var Savepath = "res://Save folder/savefile.cfg"
var _config
file = ConfigFile.new()

func ready():

func savesettings():
for section in _settings.keys():
for key in _settings[section].keys():
file.setvalue(section, key, _settings[section][key])

func loadsettings():
var error = _config
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)
asked Jul 19, 2019 in Engine by XxcringekidxX (27 points)

2 Answers

0 votes

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.

answered Jul 19, 2019 by johnygames (1,841 points)
0 votes

Hey there,

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

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!")
    # Open existing file
    if file.open("user://saved_game.sav", File.READ) != 0:
        print("Error opening file")
    # 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")
    # Save the dictionary as JSON (or whatever you want, JSON is convenient here because it's built-in)
    # Here are two debug lines:
    print("Saved config:")
    print (to_json(config))
answered Jul 19, 2019 by h0rn (14 points)
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.