How to save more than 1 variable in GDscript this way

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

First scene script except variables and other unnecessary lines-

 func _on_save_pressed():
	get_node("/root/savesystem").szhr = zhr
	get_node("/root/savesystem").saveValue("Values", "ValueOne")
func _on_Load_pressed():
	get_node("/root/savesystem").loadValue("Values", "ValueOne")
	zhr = get_node("/root/savesystem").szhr

Second scene script-

    extends Node2D

var sves = 0 
var szhr = 0 


var save_path = "res://save-file.cfg"
var config = ConfigFile.new()
var load_response = config.load(save_path)

func _ready():
	pass

func saveValue(section, key):
		config.set_value(section, key, szhr)
		config.save(save_path)

func loadValue(section, key):
		szhr = config.get_value(section, key, szhr)

I already tried to make two “func saveValue(section, key):” methods in 1 scene, and I read documentation about basic savings but it’s not for my way.
I really need to make 10 scenes for 10 variables to save? Or how I can save more than 1 variables for 1 method. #bad english

:bust_in_silhouette: Reply From: Zylann

You can save multiple values. I don’t see where the problem is, except that you somehow created an intermediary variable szhr for some reason.

ConfigFile lets you store multiple variables using multiple names.
If you have 3 variables “A”, “B” and “C”, that’s how you store them:

config.set_value(section, "A", value_of_a)
config.set_value(section, "B", value_of_b)
config.set_value(section, "C", value_of_c)

And this is how you get them back:

value_of_a = config.get_value(section, "A")
value_of_b = config.get_value(section, "B")
value_of_c = config.get_value(section, "C")

If you don’t want to call get_value or set_value multiple times, make your variable a dictionary. A dictionary can contain multiple values. But ConfigFile already is itself a dictionary so it wouldn’t really add much value.