0 votes

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...

in Engine by (57 points)

2 Answers

+1 vote
Best answer

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')
by (57 points)
0 votes

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 ?

by (57 points)

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

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

i found out this post: https://godotdevelopers.org/forum/discussion/18056/save-load-variables-help

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

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.