How to test if a resource exists, without throwing an error?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Poobslag

Is there a way to test if a resource exists before calling load()? For a practical use case, consider a load_face() function:

func load_face(name: String)
  ears_resource = load("res://%s/ears.png" % name)
  nose_resource = load("res://%s/nose.png" % name)
  eyes_resource = load("res://%s/eyes.png" % name)
  mouth_resource = load("res://%s/mouth.png" % name)
  beard_resource = load("res://%s/beard.png" % name)
  moustache_resource = load("res://%s/moustache.png"  % name)

Calling load_face("debbie") loads Debbie’s features from the corresponding folder, and her beard and moustache resources are null which all makes sense. But – this results in numerous Resource file not found errors being logged because you asked Godot to load a non-existent beard and moustache resource.

Is there some way to maintain this design without printing out errors?

:bust_in_silhouette: Reply From: jgodfrey

I think you want the ResourceLoader.exists() method.

print(ResourceLoader.exists("res://sprite.tscn"))

Thanks, that’s exactly what I need!

Poobslag | 2020-04-03 22:55