Should Directory.list_dir_begin() Return False When Working?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Hammer Bro.
:warning: Old Version Published before Godot 3 was released.

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.list_dir_begin() 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.list_dir_begin() on Godot 2.1 Stable?

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

:bust_in_silhouette: Reply From: mateusak
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?

Strange. That works, even though list_dir_begin() 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 list_dir_begin() returns false when I don’t think it should.

Hammer Bro. | 2016-09-07 21:29

:bust_in_silhouette: Reply From: xelivous

Yes, the docs are wrong.

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

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.

Hammer Bro. | 2016-09-07 23:00