2024-06-14 09:44:34 +02:00
|
|
|
extends Node
|
|
|
|
|
2024-06-16 19:32:19 +02:00
|
|
|
var field = preload("res://scenes/menu/inputOutput.tscn")
|
2024-06-15 21:56:18 +02:00
|
|
|
var data:Dictionary
|
|
|
|
|
2024-06-16 19:32:19 +02:00
|
|
|
func loadJSON(savePath):
|
|
|
|
if not FileAccess.file_exists(savePath):
|
2024-06-15 21:56:18 +02:00
|
|
|
return false
|
2024-06-16 19:32:19 +02:00
|
|
|
var fileAccess = FileAccess.open(savePath, FileAccess.READ)
|
|
|
|
var jsonString = fileAccess.get_line()
|
|
|
|
fileAccess.close()
|
2024-06-15 21:56:18 +02:00
|
|
|
|
|
|
|
var json = JSON.new()
|
2024-06-16 19:32:19 +02:00
|
|
|
var error = json.parse(jsonString)
|
2024-06-15 21:56:18 +02:00
|
|
|
if error:
|
2024-06-16 19:32:19 +02:00
|
|
|
print("JSON Parse Error: ", json.get_error_message(), " in ", jsonString, " at line ", json.get_error_line())
|
2024-06-15 21:56:18 +02:00
|
|
|
return false
|
|
|
|
|
|
|
|
data = json.data
|
|
|
|
return true
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
if loadJSON("res://content/stats.json"):
|
|
|
|
for i in range(int(data.get("amount"))):
|
2024-06-16 19:32:19 +02:00
|
|
|
var fieldInstance = field.instantiate()
|
2024-06-16 20:01:56 +02:00
|
|
|
$"ScrollContainer/VBoxContainer".add_child(fieldInstance)
|
|
|
|
$"ScrollContainer/VBoxContainer".get_child(i).position = Vector2(16,(16+i*88))
|
|
|
|
$"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i))
|
|
|
|
$"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40
|
2024-06-15 21:56:18 +02:00
|
|
|
|
2024-06-16 19:32:19 +02:00
|
|
|
func saveJSON(savePath):
|
|
|
|
var saveData = {}
|
2024-06-15 20:33:37 +02:00
|
|
|
|
2024-06-15 21:56:18 +02:00
|
|
|
for i in range(int(data.get("amount"))):
|
|
|
|
if i > 0:
|
2024-06-16 20:01:56 +02:00
|
|
|
saveData[data.get(str(i))] = $"ScrollContainer/VBoxContainer".get_child(i).text
|
2024-06-14 09:44:34 +02:00
|
|
|
|
2024-06-16 19:32:19 +02:00
|
|
|
var jsonString = JSON.stringify(saveData)
|
2024-06-14 09:44:34 +02:00
|
|
|
|
2024-06-16 19:32:19 +02:00
|
|
|
var fileAccess = FileAccess.open(savePath, FileAccess.WRITE)
|
|
|
|
if not fileAccess:
|
2024-06-14 09:44:34 +02:00
|
|
|
print("An error happened while saving data: ", FileAccess.get_open_error())
|
|
|
|
return
|
|
|
|
|
2024-06-16 19:32:19 +02:00
|
|
|
fileAccess.store_line(jsonString)
|
|
|
|
fileAccess.close()
|
2024-06-14 09:44:34 +02:00
|
|
|
|
2024-06-14 10:34:06 +02:00
|
|
|
func _on_button_pressed():
|
2024-06-16 20:01:56 +02:00
|
|
|
var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json")
|
2024-06-16 19:32:19 +02:00
|
|
|
saveJSON(savePath)
|
2024-06-15 20:33:37 +02:00
|
|
|
|
|
|
|
func _process(delta):
|
|
|
|
if Input.is_action_pressed("escape"):
|
|
|
|
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
|