Reputation: 67
I want to fill my array with loaded scenes of rooms in godot 4.1.1. So i created func to check whether they exist or not. If they exist then append to array, if not exist then break the loop. So when i run script i get error in debugger like this scene does not exist and failed to loading. Is it good? or there are any other ways to check whether scene exist or not? Im new to godot
func get_room_array():
var i = 1
while true:
if load(get_room_path(i)) != null:
room_array.append(load(get_room_path(i)))
else:
break
i+=1
Upvotes: 2
Views: 2814
Reputation: 40285
If you want to check if an imported resource (including scenes) can be loaded, you can use: ResourceLoader.exists
:
if ResourceLoader.exists(get_room_path(i)):
room_array.append(load(get_room_path(i)))
So you don't only don't load resources twice (which you could have addressed by other means), but you shouldn't have a load error.
This is not the same as checking if the file exists (FileAccess.file_exists
because even if a file exists, it does not mean it can be loaded… And a resource path might not be a file path).
Upvotes: 2