How do i get the path of a png currently selected with get.next in a directory?

Godot Version

Godot v4.6.1

Question

So with the .get_next()method you can cycle through the files in a directory, like if i have opened a directory containing apple.png, grapes.png and orange.png with DirAccess.open and then start cycling through it with Example_Var = Example_Directory.get_next() the Example_Var will be “orange.png” the first time then “apple.png” the second etc, is it possible to get the path of the file the Example_Varis currently set to? Like getting “res://fruits/orange.png“ when Example_Var set to orange.png for instance so you can use it as the filepath for stuff like changing a texture.

Sorry if its a stupid question or i asked this wrong or smth, its my first time here.

What is your use case?

Does it not work to simply preload them at the top of your script and change the texture?

Something like:

var orange := preload(“res://fruits/orange.png”)

func change_png():
png.texture = orange

get_next() already provides the filename with extenstion, so you just need to merge the filename with the directory path.

var file_name = dir.get_next()
var file_path = str(dir_path + "/" + file_name)

This may not work as you expect in exported games, “res://” paths will change but functions like load still work as expected, DirAccess exposes what files really end up in the pack as .remap files.

If you want to read a directory of resources use ResourceLoader.list_directory