+2 votes

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?

in Engine by (146 points)

1 Answer

+3 votes
Best answer

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

print(ResourceLoader.exists("res://sprite.tscn"))
by (19,268 points)
selected by

Thanks, that's exactly what I need!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.