Is there a way to handle errors while loading resource just like with FileAccess?
Not only in case a file is not found but rather if “any” other error happens.
I found this in the docs but I have no idea how to use it: Returns an empty resource if no ResourceFormatLoader could handle the file, and prints an error if no file is found at the specified path.
Working example with FileAccess:
var save_game = FileAccess.open("user://my_resource.tres", FileAccess.READ)
var error = FileAccess.get_open_error()
if error == ERR_FILE_NOT_FOUND:
print("File not found, create a new file with default values.")
elif error != OK:
printerr("An error has occured. Error code: " + str(error))
Incomplete + not working example with ResourceLoader:
var save_game: Resource = load("user://my_resource.tres")
print(save_game) # Prints <Object#null> if an error occurs
# How to handle loading error?
This means that it will print an error to the console, not return an Error type as FileAccess unfortunately.
In that case then error handling is pretty limited compared to FileAccess and that’s not what I want.
So I wonder if I should open an issue about that on GitHub.
You could maybe combine both to check for the error with FileAccess and then load the resource with ResourceLoader
var save_game: Resource
FileAccess.open("user://my_resource.tres", FileAccess.READ)
var error = FileAccess.get_open_error()
if error == ERR_FILE_NOT_FOUND:
print("File not found, create a new file with default values.")
elif error != OK:
printerr("An error has occured. Error code: " + str(error))
else:
save_game = load("user://my_resource.tres")
But you can of course open an issue on the GitHub too, as that would be a nice to have on the ResourceLoader directly.
Yes, of course. The solution I mentioned will get you basically only one error covered - in case the file is missing. It won’t handle any other errors that can be thrown by the ResourceLoader internally.
Since you know beforehand that this method could throw an error,
why not just ask if the file exists? e.g.:
if FileAccess.file_exists(path):
var resource = ResourceLoader.load(path)
# maybe an error if you mix up paths and the file turns out to
# "not" be a resource
else:
# handle missing file
But it seems ResourceLoader has its own method specifically for Resources.
if ResourceLoader.exists(path):
var resource = ResourceLoader.load(path)
else:
# handle missing resource
So you are guaranteed to load a resource, if it exists.
And if you want to have more detailed errors you could combine the two. Let’s say you wrote your own custom resource with a specific file extension:
if ResourceLoader.exists(path):
var resource = ResourceLoader.load(path)
elif FileAccess.file_exists(path):
print('Only .xyz files are supported')
else:
print('File does not exist')
You’re wrong in this sentence unfortunately. Loading a Resource with ResourceLoader can fail also for other reasons than just the path being incorrect. If a file at the provided path is not a proper Resource - it will not be loaded, even if the file exists.
Returns whether a recognized resource exists for the given path.
If you wrote a custom resource, I guess it could fail, but, assuming you use the default Resources or you correctly wrote your own ResourceFormatSaver/ResourceFormatLoader, it should be impossible for the ResourcerLoader to fail.