freettrpg/scripts/getStats.gd
Patrick_Pluto 6b2159b840 Code Refactoring: Part 2
Hopefully final refactoring, offloaded ui scripts to its own global .gd to prevent repetition.
2024-06-19 18:27:40 +02:00

37 lines
1.3 KiB
GDScript

extends Node
# Variable to store loaded JSON data
var data
var amount
# Get all needed things for Ui.placeFields and save amount from it.
func _ready():
data = Content.data
var container = $"ScrollContainer/VBoxContainer"
Ui.placeFields(data, container, false)
amount = data.get("amount")
# Upon pressing the "Get" button, also called in game
func _on_button_pressed():
# Construct save path based on user input
var savePath = "user://player_" + $"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, although the wiki says it's 18...)
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))
# Only for the Main Menu "shortcut"
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