Added the ability to save character data from add_character.tscn, to CSV. Working on loading next

This commit is contained in:
PersonGuyGit 2024-04-07 11:57:59 -06:00
parent b092a68ca7
commit 347e48080b

View File

@ -42,19 +42,64 @@ func _prep_PlayerCharacter_Template():
pSingleton.armor = Armor.text
pSingleton.quote = Quote.text
func _on_But_SaveChar_pressed() -> void:
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():
_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.
var file_path = "user://character_data.csv"
var pc = get_node("/root/PlayerCharacter")
var newCharFile = File.new()
newCharFile.open(path, 2)
# 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)
##
#