Skip to content

Commit

Permalink
Added the ability to save character data from add_character.tscn, to …
Browse files Browse the repository at this point in the history
…CSV. Working on loading next
  • Loading branch information
PersonGuyGit committed Apr 7, 2024
1 parent b092a68 commit 347e480
Showing 1 changed file with 58 additions and 13 deletions.
Expand Up @@ -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)
##
#

0 comments on commit 347e480

Please sign in to comment.