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

30 lines
872 B
GDScript

extends Node
# Function to load JSON data from a file
func loadJSON(savePath):
var data # Variable to store loaded data
# 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
data = json.data # Extract data from the parsed JSON
return data # Return the loaded data