From 8bb83e987b21e44a6e313470039f61eeffeeb147 Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Tue, 18 Jun 2024 19:17:42 +0200 Subject: [PATCH] Code refactoring: Part 1 --- project.godot | 6 +++++ scripts/content.gd | 8 +++++++ scripts/createStats.gd | 53 ++++++++++-------------------------------- scripts/getStats.gd | 39 ++++++++++--------------------- scripts/load.gd | 17 ++++++++++++++ scripts/save.gd | 12 ++++++++++ 6 files changed, 67 insertions(+), 68 deletions(-) create mode 100644 scripts/content.gd create mode 100644 scripts/load.gd create mode 100644 scripts/save.gd diff --git a/project.godot b/project.godot index 2734c58..42a4049 100644 --- a/project.godot +++ b/project.godot @@ -15,6 +15,12 @@ run/main_scene="res://scenes/menu/main.tscn" config/features=PackedStringArray("4.2", "GL Compatibility") config/icon="res://icon.svg" +[autoload] + +Load="*res://scripts/load.gd" +Save="*res://scripts/save.gd" +Content="*res://scripts/content.gd" + [display] window/size/viewport_width=800 diff --git a/scripts/content.gd b/scripts/content.gd new file mode 100644 index 0000000..dc918a3 --- /dev/null +++ b/scripts/content.gd @@ -0,0 +1,8 @@ +extends Node + +var loadPath +var data + +func _ready(): + loadPath = "res://content/stats.json" + data = Load.loadJSON(loadPath) diff --git a/scripts/createStats.gd b/scripts/createStats.gd index 4d09ec6..7e34f11 100644 --- a/scripts/createStats.gd +++ b/scripts/createStats.gd @@ -1,53 +1,24 @@ 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 +var data 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 = {} - + data = Load.loadJSON("res://content/stats.json") 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() + 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 _on_button_pressed(): 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): if Input.is_action_pressed("escape"): diff --git a/scripts/getStats.gd b/scripts/getStats.gd index a856898..b9e5fd2 100644 --- a/scripts/getStats.gd +++ b/scripts/getStats.gd @@ -1,39 +1,24 @@ extends Node var field = preload("res://scenes/menu/inputOutput.tscn") -var data:Dictionary +var data 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(): - 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).placeholder_text = data.get(str(i)) - $"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40 - if i > 0: - $"ScrollContainer/VBoxContainer".get_child(i).editable = false - amount = data.get("amount") + data = Content.data + for i in range(int(data.get("amount"))): + var fieldInstance = field.instantiate() + $"ScrollContainer/VBoxContainer".add_child(fieldInstance) + $"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i)) + $"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40 + if i > 0: + $"ScrollContainer/VBoxContainer".get_child(i).editable = false + amount = data.get("amount") func _on_button_pressed(): 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): $"ScrollContainer/VBoxContainer".get_child(i+1).text = data.get(str($"ScrollContainer/VBoxContainer".get_child(i+1).placeholder_text)) diff --git a/scripts/load.gd b/scripts/load.gd new file mode 100644 index 0000000..2218678 --- /dev/null +++ b/scripts/load.gd @@ -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 diff --git a/scripts/save.gd b/scripts/save.gd new file mode 100644 index 0000000..c3abd93 --- /dev/null +++ b/scripts/save.gd @@ -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()