freettrpg/scripts/getStats.gd
2024-06-18 18:58:47 +02:00

53 lines
2 KiB
GDScript

extends Node
# Reference to the inputOutput.tscn scene
var field = preload("res://scenes/menu/inputOutput.tscn")
# Variables to store data and amount
var data
var amount
# Called when the node is added to the scene
func _ready():
# Get data from Content singleton
data = Content.data
# Instantiate input fields based on 'amount' from data
for i in range(int(data.get("amount"))):
var fieldInstance = field.instance()
$"ScrollContainer/VBoxContainer".add_child(fieldInstance)
# Set placeholder text and minimum size for each field
$"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i))
$"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40
# Make fields editable except the first one
if i > 0:
$"ScrollContainer/VBoxContainer".get_child(i).editable = false
amount = data.get("amount") # Store the amount of fields
# Handler for when a button is pressed
func _on_button_pressed():
# Construct save path based on user input
var savePath = "user://player_data" + $"ScrollContainer/VBoxContainer".get_child(0).text + ".json"
# Load JSON data from the constructed save path
data = Load.loadJSON(savePath)
# Check if data is successfully loaded (type 27 corresponds to Dictionary in Godot)
if typeof(data) == 27:
# Populate fields with loaded data
for i in range(int(amount) - 1):
$"ScrollContainer/VBoxContainer".get_child(i + 1).text = data.get(str($"ScrollContainer/VBoxContainer".get_child(i + 1).placeholder_text))
# Process function called every frame
func _process(_delta):
# Check if the Escape key is pressed to change scene to main.tscn
if Input.is_action_pressed("escape"):
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
# Function to hide the 'get' button
func hideGet():
$button.visible = false
$button.disabled = true