freettrpg/scripts/load.gd
Patrick_Pluto 6b2159b840 Code Refactoring: Part 2
Hopefully final refactoring, offloaded ui scripts to its own global .gd to prevent repetition.
2024-06-19 18:27:40 +02:00

26 lines
692 B
GDScript

extends Node
# 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