Load files from directory

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

I have an AnimatedSprite node, in which I want to load all .png files from a folder, and add them as frames. So before I can add them as frames I try to use this code to create a list of all files. But when I run this code, it only returns an endless stream of “Found dir: .”, which I assume is the current directory?

extends AnimatedSprite

func _ready():

func dir_contents(path):
	var dir = Directory.new()
	if dir.open(path) == OK:
		var file_name = dir.get_next()
		while file_name != "":
			if dir.current_is_dir():
				print('Found dir: ' + file_name)
				print('Found file: ' + file_name)
		file_name = dir.get_next()
		print("An error occurred when trying to access the path.")
:bust_in_silhouette: Reply From: Lopy

file_name = dir.get_next() is one indentation level to high, which means file_name does not change inside the loop.

“.” is indeed the current folder. You can use list_dir_begin(true) to skip it, along with “…” (parent folder).

Oh, that is such an embarrassing mistake! Thank you for your help, it works now, of course!

Karl Wilhelm | 2020-12-27 22:05