freettrpg/scripts/createStats.gd
Patrick_Pluto 5e4f79ee19 Version: 0.0.4
Scrolling implemented in view and creator, so you can add infinite variables to the content.
2024-06-16 20:01:56 +02:00

54 lines
1.7 KiB
GDScript

extends Node
var field = preload("res://scenes/menu/inputOutput.tscn")
var data:Dictionary
func loadJSON(savePath):
if not FileAccess.file_exists(savePath):
return false
var fileAccess = FileAccess.open(savePath, FileAccess.READ)
var jsonString = fileAccess.get_line()
fileAccess.close()
var json = JSON.new()
var error = json.parse(jsonString)
if error:
print("JSON Parse Error: ", json.get_error_message(), " in ", jsonString, " 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()
$"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
func saveJSON(savePath):
var saveData = {}
for i in range(int(data.get("amount"))):
if i > 0:
saveData[data.get(str(i))] = $"ScrollContainer/VBoxContainer".get_child(i).text
var jsonString = JSON.stringify(saveData)
var fileAccess = FileAccess.open(savePath, FileAccess.WRITE)
if not fileAccess:
print("An error happened while saving data: ", FileAccess.get_open_error())
return
fileAccess.store_line(jsonString)
fileAccess.close()
func _on_button_pressed():
var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json")
saveJSON(savePath)
func _process(delta):
if Input.is_action_pressed("escape"):
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")