freettrpg/scripts/load.gd

27 lines
692 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 load JSON data from a file
func loadJSON(savePath):
# Check if the file exists
if not FileAccess.file_exists(savePath):
return 1 # Return error code 1 if file does not exist
# Open the file for reading
var fileAccess = FileAccess.open(savePath, FileAccess.READ)
# Read the entire JSON string from the file
var jsonString = fileAccess.get_line()
fileAccess.close() # Close the file
# Create a new JSON instance
var json = JSON.new()
# Parse the JSON string into a JSON object
var error = json.parse(jsonString)
if error:
return 1 # Return error code 1 if there was an error parsing JSON
return json.data # Return the loaded data