0 votes

I need to load the value of a variable from a JSON file, but it only loads the text. I can not load the value "true" from the file, no matter how I try to change the code. I looked at a few examples that work, but I did not see any information on how to load the value of the variable false \ true

    extends Node2D

var fps = false

func load_save() -> Dictionary:
    var f := File.new()
    f.open("res://save.json", File.READ)
    var result := JSON.parse(f.get_as_text())
    f.close()

    if result.error:
        printerr("Failed to parse save file: ", f.error_string)
    return result.result as Dictionary

func save(data: Dictionary):
    var f := File.new()
    f.open("res://save.json", File.WRITE)
    prints("Saving to", f.get_path_absolute())
    f.store_string(JSON.print(data))
    f.close()

func _process(delta):
    if fps == true:
        get_node("Polygon2D").visible = true
    if fps == false:
        get_node("Polygon2D").visible = false

func _ready():
    load_save()
    save({
        "inventory": {
            "ingredients": ["apple", "pear"],
            "items": ["spoon", "fork"]
        },
        "stats": {
            "fps": fps,
            "experience": 20,
        }
    })


    print(load_save())




func _on_Button_pressed():
    fps = !fps
    print(fps)


func _on_Button2_pressed():
    save({
        "inventory": {
            "ingredients": ["apple", "pear"],
            "items": ["spoon", "fork"]
        },
        "stats": {
            "fps": fps,
            "experience": 20,
        }
    })


func _on_Button3_pressed():
    load_save()
    print(load_save())

However, the code saves and loads everything correctly, as the console shows. But its essence lies in loading the previously saved value "true".
That is, when loading the selected polygon - should become visible, but this does not happen when loading

I would be grateful if you explain what the problem is and how to solve it

in Engine by (71 points)

IIRC, JSON files load text or numbers, and no other data type. So you could have it save the string "true", or you could have it save the values 0 or 1.

1 Answer

0 votes
Best answer

In your _ready function, you are loading the data, and saving it. Saving works because you are creating the dictionary to save using the variables it needs to contain.

load_save returns the data from the file as another dictionary, but you aren't doing anything with it. So it's dropped. fps is never getting assigned any new value, so it remains false forever.

You should do this if you want to update your variables:

var data = load_save()
fps = data.stats.fps
# And same for other variables you might have
by (28,791 points)
selected by
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 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 webmaster@godotengine.org with your username.