0 votes

I'm using Godot 2.1 Stable (x64) on Windows. According to most sources of reference, here is how you'd begin to list the contents of a directory:

func _ready():
    var directory = Directory.new()
    assert(directory.open("res://") == OK)
    if directory.list_dir_begin() == false:
        print("Couldn't begin list.")
    else:
        print("Began list.")
        directory.list_dir_end()

(Edit)

For me, directory.listdirbegin() always returns false, even when the directory can in fact be listed.

According to the current documentation, it should only "Return false if the stream could not be initialised."

It seems to me like this can only be explained by one of three options:

  1. The documentation is inaccurate (or I misunderstand it), and a return code of false does not indicate failure.
  2. Something buggy is happening on my specific deployment, because other people experience return codes of true.
  3. There is a bug affecting all 2.1 deployments, and everyone's return code is false even when the listing succeeds.

Can anyone confirm that they get a return code of true when calling Directory.listdirbegin() on Godot 2.1 Stable?

Or explain that the documentation doesn't mean what I think it means?

asked Sep 7, 2016 in Engine by Hammer Bro. (91 points)
edited Sep 7, 2016 by Hammer Bro.

2 Answers

0 votes
Best answer

Yes, the docs are wrong.

https://github.com/godotengine/godot/blob/master/drivers/windows/dir_access_windows.cpp#L76

According to this, it returns true if the handle is invalid. Otherwise it returns false.

answered Sep 7, 2016 by xelivous (48 points)
selected Sep 7, 2016 by Hammer Bro.

Fancy, thanks. I'll look into posting a bug report, as to me returning False (like the docs suggest) seems like the more expected return value.

+1 vote
func list_files_in_directory(path):
    var files = []
    var dir = Directory.new()
    dir.open(path)
    dir.list_dir_begin()

    while true:
        var file = dir.get_next()
        if file == "":
            break
        elif not file.begins_with("."):
            files.append(file)

    dir.list_dir_end()

    return files

Is that what you want?

answered Sep 7, 2016 by mateusak (1,071 points)

Strange. That works, even though listdirbegin() returns false. According to the current documentation, it should "Return false if the stream could not be initialised."

Perhaps there's a bug in the return code, or the description is out of date? I'll update my question to be more specific based on that information.

Edit: I've reworded the question to focus on the fact that listdirbegin() returns false when I don't think it should.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.