freettrpg/scripts/save.gd

22 lines
754 B
GDScript3
Raw Normal View History

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