How to receive folder contents in the form of list?


res://Sound/

  • I think DirAccess was still named Directory in Godot 3.x.
    In that case the built-in documentation has an example:

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

    Here is the correct link to the documentation: https://docs.godotengine.org/en/stable/classes/class_directory.html

Described in documentation does not work. var dir = DirAccess.open(path)
How to do it right?

Godo 3.x

Godo 4.x

I suspect you shouldn't derive variable type from the DirAccess assignment. Try:

var my_file = DirAccess.open("res://sound/")

instead of:

var my_file := DirAccess.open("res://sound/")

Also, what's the current scope there? What class are you extending?

I think DirAccess was still named Directory in Godot 3.x.
In that case the built-in documentation has an example:

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

Here is the correct link to the documentation: https://docs.godotengine.org/en/stable/classes/class_directory.html

    Jummit Here is the correct link to the documentation

    This topic has the "Godox 4.x" tag, so I provided the documentation link for that version.
    I tested the example DirAccess code in Godot 4.0-beta7, and it works correctly with no errors.
    I tested with both = and :=, and there was no difference.
    The script was in a Node2D.