How to save and load Hslider value ?

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

Hello everyone,

I create an option menu with 3 HSliders to define speed and sentivity of mouse.
I would like to store value before launch a level, i try this :

extends Spatial

onready var GreyScale = get_node("./Control/GreyScale")
onready var niveau1 = get_node("./Control/VBoxContainer/niveau1")

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	set_process_input(true)
	pass

func _unhandled_input(event):
    if event is InputEventKey:
        if event.pressed and event.scancode == KEY_ESCAPE:
            get_tree().quit()

func _on_niveau1_pressed():
	var HS1 = get_node("./Control/GreyScale/HSlider").get("value")
	var HS2 = get_node("./Control/GreyScale/HSlider2").get("value")
	var HS3 = get_node("./Control/GreyScale/HSlider3").get("value")
	script_save_load.save_game({"MAX_SPEED":HS1, "JUMP_SPEED":HS2, "MOUSE_SENSITIVITY":HS3})
	get_tree().change_scene("res://_Scene/Scene2.tscn")
	pass # replace with function body

func _on_Options_pressed():
	if GreyScale.is_visible():
		GreyScale.hide()
		niveau1.show()
	else:
		niveau1.hide()
		GreyScale.show()
	pass

I created so an script to save and load value :

extends Node

var ContaSet = 0
var savegame = File.new()
var path = "user://savegame.save"

var Dict = {
	MAX_SPEED = 10,
	JUMP_SPEED = 10,
	MOUSE_SENSITIVITY = 0.02
}

func save_game(Dictnew):
	savegame.open(path, File.READ)
	savegame.store_line(to_json(Dictnew))
	savegame.close()

func load_game(name):
	if savegame.file_exists(path):
		savegame.open(path, File.READ)
		Dict = parse_json(savegame.get_line())
		savegame.close()
	else:
		savegame.open(path, File.WRITE)
		savegame.store_line(to_json(Dict))
		savegame.close()
	return Dict[name]

And when I launch a scene i try to do this :

var MAX_SPEED = script_save_load.load_game("MAX_SPEED")
var JUMP_SPEED = script_save_load.load_game("JUMP_SPEED")
var MOUSE_SENSITIVITY = script_save_load.load_game("MOUSE_SENSITIVITY")

But I don’t know why my velue it isn’t store and load in scene in game.
And I would like when i launch game that Value of HSlider is define by the File savegame.

I need help…

:bust_in_silhouette: Reply From: Paul WOISARD

I found a solution, to save parameters.

func save_game(Dictnew):
	savegame.open(path, File.WRITE)
	savegame.store_line(to_json(Dictnew))
	savegame.close()

But i would like load last parameter in hslider. how to ?

i dont know which kind of file you are using to store the data, if it was a .txt the solution would be to get the text and put it in a string:

var file=File.New
file.open(path, File.READ)
var text_content=file.get_as_text()
file.close

Andrea | 2018-10-31 13:28

I would like save two ints and a float in a dictionnary file.

Paul WOISARD | 2018-10-31 15:33

i found out this post: Loading...

to save: f.store_string(my_dict.to_json())
to load: my_dict.parse_json(f.get_string())

however the post is old and I didn´t test it to check if still valid

Andrea | 2018-10-31 15:43

:bust_in_silhouette: Reply From: Paul WOISARD

Now all works like i expected, the code are :
In code to save and load :

extends Node
var ContaSet = 0
var savegame = File.new()
var path = "user://savegame.save"

var Dict = {
    MAX_SPEED = 10,
    JUMP_SPEED = 10,
    MOUSE_SENSITIVITY = 0.02
}

func save_game(Dictnew):
    savegame.open(path, File.WRITE)
    savegame.store_line(to_json(Dictnew))
    savegame.close()

func load_game(name):
    if savegame.file_exists(path):
        savegame.open(path, File.READ)
        Dict = parse_json(savegame.get_line())
        savegame.close()
    else:
        savegame.open(path, File.WRITE)
        savegame.store_line(to_json(Dict))
        savegame.close()
    return Dict[name]

In Option button :

func _on_Options_pressed():
	if GreyScale.is_visible():
		script_save_load.save_game({MAX_SPEED = get_node("./Control/GreyScale/HSlider").get("value"), JUMP_SPEED = get_node("./Control/GreyScale/HSlider2").get("value"), MOUSE_SENSITIVITY = get_node("./Control/GreyScale/HSlider3").get("value")})
		GreyScale.hide()
		niveau1.show()
	else:
		get_node("./Control/GreyScale/HSlider").value = script_save_load.load_game('MAX_SPEED')
		get_node("./Control/GreyScale/HSlider2").value = script_save_load.load_game('JUMP_SPEED')
		get_node("./Control/GreyScale/HSlider3").value = script_save_load.load_game('MOUSE_SENSITIVITY')
		niveau1.hide()
		GreyScale.show()
	pass

When in new scene was load :

var MAX_SPEED = script_save_load.load_game('MAX_SPEED')
var JUMP_SPEED = script_save_load.load_game('JUMP_SPEED')
var MOUSE_SENSITIVITY = script_save_load.load_game('MOUSE_SENSITIVITY')