Problem is getnext() seems to only go through one filetype in the folder, whichever comes first alphabetically. If the first thing alphabetically is a folder getnext() only returns folders, if its png’s only png’s etc. this is very annoying when trying to work with externally inserted files cuz if the file you put in just happens to start with a Z it will make a list of the folders instead and everything breaks and id like to not have to rename every folder to zzz(foldername) in the project, any solutions?
get_next() only returns one entry per call, so calling it once just gets you the first thing alphabetically. It doesn’t build a list, which I think is why it looks like it’s picking a filetype.
Try looping it until it returns an empty string:
var subdir = "res://processed_cards/" + folder_name + "/"
var dir = DirAccess.open(subdir)
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if not dir.current_is_dir():
print(file_name) # or append to your array
file_name = dir.get_next()
dir.list_dir_end()
current_is_dir() is how you skip folders (or keep them if you want them).
Godot 4 you can also do a shorter version:
var files = DirAccess.get_files_at("res://processed_cards/" + folder_name)
get_files_at() gives you all the files as a PackedStringArray in one call, and get_directories_at() does the same for folders. No loop and no renaming things to zzz.
One thing to keep in mind since you mentioned externally inserted files, if this is for an exported game, res:// gets packed at export and pngs become .import references, so files added at runtime usually need to live in user:// instead.
oh sorry for the confusion, i am calling it multiple times later in the code in an attempt to cycle through it until i get a png to set that png as an image texture of the object but if the first file it finds with getnext() is not a png it never returns any png’s after and there is also .tres files in there so only skipping the directory’s doesnt work either
for i in dir_card_directory.get_files():
if file_name.ends_with(".png"):
$Card_Img.texture = load("res://processed_cards/"+folder_name+"/"+file_name+"/")
break
file_name = dir_card_directory.get_next()
Can you paste the part where you call get_next() multiple times?
I’m wondering if your loop stops when it hits something that isn’t a png instead of skipping it and continuing. That could explain why a non png first entry means no pngs ever come back.
Something shaped like this should walk the whole folder and only grab the pngs:
var file_name = dir.get_next()
while file_name != "":
if file_name.ends_with(".png"):
# found one, use it
pass
file_name = dir.get_next()
Basically the for loop only assigns to i each pass, file_name only changes when a get_next() line runs. So on the first pass file_name still held whatever the earlier get_next() call returned, and the check ran on that before the update line at the bottom ever fired. It was always one step behind i, checking the previous entry instead of the current one.