Random beginner-question: How to save (and load) a checkbox-state

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

Hi everyone,

I’m unsuccessfully trying to set this up for a couple of days by now, so I finally decided to ask for help. I’d like to save the states of four checkboxes (red on/off, blue on/off, green on/off, yellow on/off) and load that latest state at restart.

From all I know I certainly need to create a file for that, one that should be accessible from any node/script. I find suggestions for *.txt-files but also for *.tres, (like “res://saves/my_checkboxes.tres”; I know I should opt for “user://” later…). I feel that “json” often is not recommended.
(Having a directory created when none exists yet also seems to be easily possible by something like
if not dir.dir_exists(“res://saves/”):
dir.make_dir(“res://saves/”) )

Then there is the “File.WRITE” and “File.READ” approach to be found but also something like “RecourceSaver.save(…)” which I understand as part of some func load_everything() and func save_everything().

As I understand I have to “export”-mark the state-variables of my checkboxes in order to make them save-/loadable in the first place. I guess that in this case this means I need to somehow set the boolean in the checkbox-scene’s script variables (I have a separately instanced scene for every checkbox).

A simple “save button” below my checkboxes emits a signal when pressed which my root-node receives, then performing the saving in the “signal-received” function (“save_everything()”).
However, what I can’t quite wrap my head around is how to make the bool-state being “registered” and “ready-made” for the save-function to acknowledge (and how to send such a state to another node/script in the first place – in fact I guess this is one of the issues particularly confusing me because this seems to work so differnetly from signals, which I can somewhat manage by now). But I have a feeling that this might lean towads the exporting in the first place (“export var checkboxes…”?). Actually, this really might be the biggest question mark of them all…

All in all, despite seemingly being such a little thing to accomplish, this turns out much more complex (and confusing) than I hoped for, so my humble question is if I may ask for some short step by step guidance (or maybe a link to an already existing one).
As I feel like having reached my wits’ end here I’d very much appreciate any help.

export is just to make them show up nicely in the inspector in the editor in edit mode only
can still use them and save them without

rakkarage | 2020-09-11 16:26

:bust_in_silhouette: Reply From: rakkarage

here is a file i use to save a checkbox and some stuff (thanks gdquest https://www.youtube.com/watch?v=IrUhyf-g5hU)

can set it up as autoLoad then: Store.data.all.remember = myBool when bool changes
and then: Store.save() when wanna save
it loads in _init or whenever you want

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])
	Utility.ok(_file.save(_path))

func clear() -> void:
	data = _dataDefault.duplicate()

Thanks rakkarage, I’ll carefully work this through.
Gdquest can be pretty helpful! I might have to advance a little more to fully grasp this topic though, but your insight fills some gaps in this jigsaw.
(Now I really have to read up on this - > void… what’s the advantage of using it?)

pferft | 2020-09-12 08:34

Static typing in GDScript — Godot Engine (stable) documentation in English

it is just the return type of the function
it is not needed but without it you can return anything from any function without warning or error

rakkarage | 2020-09-12 14:53

I only had a quick glance over that page yet the other day and I remember I filed it under “I’ll come back here later”…

Just for the → void:
It says

func _process(delta: float) -> void:
    pass
The type void means the function does not return anything.

“Does not return anything” means that I can “mark” a function with → void to make it completely ignored? Say, like # at each line when hitting “ctrl k” on the whole part?

And then following

You can use any type, as with variables:

func hit(damage: float) -> bool:
    health_points -= damage
    return health_points <= 0

So here the “defined return type” is a bool… but, asks my beginner mind, what does that mean? And could I just change that “bool” to e. g. “string” or “int”? Wouldn’t that be a completely differnet thing then and probably not working at all?

(I mean, I can see how useful it would be if I could have the state of an “on/off” bool being returned from a function by explicitly telling it to do so via → bool, but that function has to deal with such a theme in the first place for that, so I guess I can’t just choose anything behin the → wherever I like… Well, I believe that’s a quite obvious thought. Beginner mind… ; )

Thanks for the help!
(Oh, and I realise I’m going off-topic here quite a bit!)

pferft | 2020-09-13 08:35