Code refactoring: Part 1

This commit is contained in:
Patrick_Pluto 2024-06-18 19:17:42 +02:00
parent e1ce830b19
commit 8bb83e987b
6 changed files with 67 additions and 68 deletions

View file

@ -15,6 +15,12 @@ run/main_scene="res://scenes/menu/main.tscn"
config/features=PackedStringArray("4.2", "GL Compatibility") config/features=PackedStringArray("4.2", "GL Compatibility")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[autoload]
Load="*res://scripts/load.gd"
Save="*res://scripts/save.gd"
Content="*res://scripts/content.gd"
[display] [display]
window/size/viewport_width=800 window/size/viewport_width=800

8
scripts/content.gd Normal file
View file

@ -0,0 +1,8 @@
extends Node
var loadPath
var data
func _ready():
loadPath = "res://content/stats.json"
data = Load.loadJSON(loadPath)

View file

@ -1,53 +1,24 @@
extends Node extends Node
var field = preload("res://scenes/menu/inputOutput.tscn") var field = preload("res://scenes/menu/inputOutput.tscn")
var data:Dictionary var data
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(): func _ready():
if loadJSON("res://content/stats.json"): data = Load.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"))): for i in range(int(data.get("amount"))):
if i > 0: var fieldInstance = field.instantiate()
saveData[data.get(str(i))] = $"ScrollContainer/VBoxContainer".get_child(i).text $"ScrollContainer/VBoxContainer".add_child(fieldInstance)
$"ScrollContainer/VBoxContainer".get_child(i).position = Vector2(16,(16+i*88))
var jsonString = JSON.stringify(saveData) $"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i))
$"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40
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(): func _on_button_pressed():
var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json") var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json")
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
Save.saveJSON(savePath, saveData)
func _process(delta): func _process(delta):
if Input.is_action_pressed("escape"): if Input.is_action_pressed("escape"):

View file

@ -1,39 +1,24 @@
extends Node extends Node
var field = preload("res://scenes/menu/inputOutput.tscn") var field = preload("res://scenes/menu/inputOutput.tscn")
var data:Dictionary var data
var amount 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(): func _ready():
if loadJSON("res://content/stats.json"): data = Content.data
for i in range(int(data.get("amount"))): for i in range(int(data.get("amount"))):
var fieldInstance = field.instantiate() var fieldInstance = field.instantiate()
$"ScrollContainer/VBoxContainer".add_child(fieldInstance) $"ScrollContainer/VBoxContainer".add_child(fieldInstance)
$"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i)) $"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i))
$"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40 $"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40
if i > 0: if i > 0:
$"ScrollContainer/VBoxContainer".get_child(i).editable = false $"ScrollContainer/VBoxContainer".get_child(i).editable = false
amount = data.get("amount") amount = data.get("amount")
func _on_button_pressed(): func _on_button_pressed():
var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json") var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json")
if loadJSON(savePath): data = Load.loadJSON(savePath)
if typeof(data) == 27:
for i in range(int(amount)-1): 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)) $"ScrollContainer/VBoxContainer".get_child(i+1).text = data.get(str($"ScrollContainer/VBoxContainer".get_child(i+1).placeholder_text))

17
scripts/load.gd Normal file
View file

@ -0,0 +1,17 @@
extends Node
func loadJSON(savePath):
var data
if not FileAccess.file_exists(savePath):
return 1
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:
return 1
data = json.data
return data

12
scripts/save.gd Normal file
View file

@ -0,0 +1,12 @@
extends Node
func saveJSON(savePath, saveData):
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 1
fileAccess.store_line(jsonString)
fileAccess.close()