freettrpg/scripts/save.gd
2024-06-18 18:58:47 +02:00

21 lines
754 B
GDScript

extends Node
# Function to save data in JSON format to a specified path
func saveJSON(savePath, saveData):
# Convert the saveData dictionary to a JSON string
var jsonString = JSON.stringify(saveData)
# Attempt to open a file for writing at the specified savePath
var fileAccess = FileAccess.open(savePath, FileAccess.WRITE)
# Check if the file was successfully opened
if not fileAccess:
# If not, print an error message with the reason for the failure and return an error code
print("An error happened while saving data: ", FileAccess.get_open_error())
return 1
# Write the JSON string to the file
fileAccess.store_line(jsonString)
# Close the file to ensure all data is properly saved and resources are freed
fileAccess.close()