freettrpg/scripts/createStats.gd

46 lines
1 KiB
GDScript3
Raw Normal View History

2024-06-14 09:44:34 +02:00
extends Node
var save_path = "user://player_data.json"
func saveJSON():
var data := {
2024-06-14 10:34:06 +02:00
"name": $input_name.text,
"hp": $input_hp.text,
"level": $input_level.text
2024-06-14 09:44:34 +02:00
}
var json_string = JSON.stringify(data)
var file_access = FileAccess.open(save_path, FileAccess.WRITE)
if not file_access:
print("An error happened while saving data: ", FileAccess.get_open_error())
return
file_access.store_line(json_string)
file_access.close()
func loadJSON():
if not FileAccess.file_exists(save_path):
return
var file_access = FileAccess.open(save_path, FileAccess.READ)
var json_string = file_access.get_line()
file_access.close()
var json = JSON.new()
var error = json.parse(json_string)
if error:
print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
return
var data:Dictionary = json.data
2024-06-14 10:34:06 +02:00
var charName = data.get("name")
var charHP = data.get("hp")
var charLevel = data.get("level")
print(charName)
print(charHP)
print(charLevel)
2024-06-14 09:44:34 +02:00
2024-06-14 10:34:06 +02:00
func _on_button_pressed():
2024-06-14 09:44:34 +02:00
saveJSON()
loadJSON()