From 347e48080b6aabc240b4e1f048ac4a4d05be25f9 Mon Sep 17 00:00:00 2001 From: PersonGuyGit Date: Sun, 7 Apr 2024 11:57:59 -0600 Subject: [PATCH] Added the ability to save character data from add_character.tscn, to CSV. Working on loading next --- .../Screens/Character_Add.gd | 69 +++++++++++++++---- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/Phase2/Godot_GameCode/_MVP_Game_Demo/Luke/bcirpg_game_mvp_2024_0204A/Screens/Character_Add.gd b/Phase2/Godot_GameCode/_MVP_Game_Demo/Luke/bcirpg_game_mvp_2024_0204A/Screens/Character_Add.gd index 27aa6c9..da95615 100644 --- a/Phase2/Godot_GameCode/_MVP_Game_Demo/Luke/bcirpg_game_mvp_2024_0204A/Screens/Character_Add.gd +++ b/Phase2/Godot_GameCode/_MVP_Game_Demo/Luke/bcirpg_game_mvp_2024_0204A/Screens/Character_Add.gd @@ -42,19 +42,64 @@ func _prep_PlayerCharacter_Template(): pSingleton.armor = Armor.text pSingleton.quote = Quote.text -func _on_But_SaveChar_pressed() -> void: - _prep_PlayerCharacter_Template() - $Title/FileDialog.popup() -#DKM TEMP: this code was inherited and it needs pretty substantial -# overhaul for use in the module, depending on toolset use. -# For now all the labels are individual lineEdits we need to grab. -func _on_FileDialog_file_selected(path: String) -> void: -# This function runs when you hit the button to save your file, after you selected the name and location -# TODO: Create the CSV File, Populate the CSV File, workout where it saves to. +func save_data_to_csv(data: Array, file_path: String): + var file = File.new() + if file.open(file_path, File.WRITE) == OK: + for row in data: + file.store_string(format_row(row)) + file.close() + print("Data saved to ", file_path) + else: + print("Failed to open", file_path, "for writing.") + +func format_row(row_data: Array) -> String: + # Convert the array of data to a comma-separated string + var formatted_row = "" + for i in range(row_data.size()): + formatted_row += str(row_data[i]) + if i < row_data.size() - 1: + formatted_row += "," + formatted_row += "\n" + return formatted_row + + +func _on_But_SaveChar_pressed(): - var pc = get_node("/root/PlayerCharacter") - var newCharFile = File.new() - newCharFile.open(path, 2) + _prep_PlayerCharacter_Template() + var file_path = "user://character_data.csv" + +# To Do. Format data into the correct table. + var input_data = [ + ["Name", "Profession", "Strength", "Intellect", "Willpower", "Charm", "Weapon", "Armor", "Quote"], + [pSingleton.name, pSingleton.profession, pSingleton.strength, pSingleton.intellect, + pSingleton.willpower, pSingleton.charm, pSingleton.weapon, pSingleton.armor, pSingleton.quote], +] + + save_data_to_csv(input_data, file_path) + +##DKM TEMP: this code was inherited and it needs pretty substantial +## overhaul for use in the module, depending on toolset use. +## For now all the labels are individual lineEdits we need to grab. +#func _on_FileDialog_file_selected(path: String) -> void: +## This function runs when you hit the button to save your file, after you selected the name and location +## TODO: Create the CSV File, Populate the CSV File, workout where it saves to. +# +# var pc = get_node("/root/PlayerCharacter") +# var newCharFile = File.new() +# newCharFile.open(path, 2) +# +## var file_path = "user://character_data.csv" +## +## var input_data = [ +## ["Name", "Age", "Score"], +## ["John", 25, 85], +## ["Alice", 30, 92], +## ["Bob", 28, 78], +##] +## +## save_data_to_csv(input_data, file_path) +## +#