Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Godot_Starter |
As a result of another question I got this code for a saving system with one variable:
https://forum.godotengine.org/70471/shortest-posible-example-for-saving-one-variable
var level = 1
func _ready():
load_level()
func load_level():
var save_file = File.new()
if not save_file.file_exists("user://savefile.save"):
return
save_file.open("user://savefile.save", File.READ)
level = save_file.get_line()
save_file.close()
func save_level():
var save_file = File.new()
save_file.open("user://savefile.save", File.WRITE)
save_file.store_line(level)
save_file.close()
Now I tried to make a saving system with two variables(level,leben) and I changed two things:
var level = 1
var leben = 1
func _ready():
load_level()
func load_level():
var save_file = File.new()
if not save_file.file_exists("user://savefile.save"):
return
save_file.open("user://savefile.save", File.READ)
#Here is the first change
level = int(save_file.get_line())
leben = int(save_file.get_line()) #I think the problem is here
save_file.close()
func save_level():
#Here is the second change
var data = {
"level" : level,
"leben" : leben
}
var save_file = File.new()
save_file.open("user://savefile.save", File.WRITE)
save_file.store_line(to_json(data))
save_file.close()
But it doesnt work. If level was 5 and health was 1 after saving and loading level was 15 and health 0, I think. How can I fix that?