Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | JerryHayat |
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
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.
Ertain | 2020-03-14 17:42