freettrpg/scripts/getStats.gd

47 lines
1.3 KiB
GDScript3
Raw Normal View History

extends Node
var field = preload("res://scenes/menu/inputOutput.tscn")
var data:Dictionary
var amount
func loadJSON(savePath):
if not FileAccess.file_exists(savePath):
return false
var fileAccess = FileAccess.open(savePath, FileAccess.READ)
var json_string = fileAccess.get_line()
fileAccess.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 false
data = json.data
return true
func _ready():
if loadJSON("res://content/stats.json"):
for i in range(int(data.get("amount"))):
var fieldInstance = field.instantiate()
add_child(fieldInstance)
get_child(i+1).position = Vector2(16,(16+i*88))
get_child(i+1).placeholder_text = data.get(str(i))
if i > 0:
get_child(i+1).editable = false
amount = data.get("amount")
func _on_button_pressed():
var savePath = str("user://player_data"+get_child(1).text+".json")
if loadJSON(savePath):
for i in range(int(amount)-1):
get_child(i+2).text = data.get(str(get_child(i+2).placeholder_text))
func _process(delta):
if Input.is_action_pressed("escape"):
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
func hideGet():
$button.visible = false
$button.disabled = true