0 votes

I have a singleton with 5 variables, how can I make their changes permanent? Like a save and load script, and where should I put that script? (on the player's script?)

in Engine by (55 points)
edited by

Not sure if I can follow... You want to save (the variables in your singleton script to a file?) but don't load them when the game is started back? Why save them then?

changed the question, I wrote it badly

2 Answers

0 votes

I also don't understand why saving but not loading but look here.

by (381 points)
0 votes
extends Node

const _path := "user://Store.cfg"
var _file := ConfigFile.new()
var _dataDefault := {
    "all": {
        "remember": true,
    },
    "f": {
        "token": "",
        "email": "",
        "refresh": "",
        "id": ""
    },
    "n": {
        "token": "",
        "email": ""
    }
}
var data := _dataDefault.duplicate()

func _init() -> void:
    read()

func read() -> void:
    if _file.load(_path) == OK:
        for section in data.keys():
            for key in data[section]:
                data[section][key] = _file.get_value(section, key)

func write() -> void:
    for section in data.keys():
        for key in data[section]:
            _file.set_value(section, key, data[section][key])
    _file.save(_path)

func clear() -> void:
    data = _dataDefault.duplicate()
by (1,883 points)
edited by

Store.data.n.token = "test"

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 Frequently asked questions and 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 [email protected] with your username.