DuckRun/code/global.gd

47 lines
1 KiB
GDScript3
Raw Normal View History

extends Node
signal levelup
signal start
var maxspeed = 260
var speed = 100
var score = 0
var gamerunning = false
var hiscore
func _ready() -> void:
load_save()
#write_save()
func load_save():
var data : FileAccess
if (not FileAccess.file_exists("user://save.json")) or (FileAccess.open("user://save.json", FileAccess.READ_WRITE).get_as_text() == ""):
data = FileAccess.open("user://save.json", FileAccess.WRITE)
data.store_string(FileAccess.open("res://code/first_save.json", FileAccess.READ).get_as_text())
data.close()
data = FileAccess.open("user://save.json", FileAccess.READ)
var content_text = JSON.parse_string(data.get_as_text())
if content_text:
print("loaded: ",content_text)
hiscore = content_text.get("hiscore", 0)
else:
print("Failed to parse JSON")
data.close()
func write_save():
var data : FileAccess
data = FileAccess.open("user://save.json",FileAccess.WRITE)
var content = JSON.stringify(
{
"hiscore":$/root/Game/Gui/ScoreContainer.get_score()
}
)
print("saved: ",content)
data.store_string(content)