Created
February 19, 2023 07:18
-
-
Save workhorsy/2fc431338ad76372374591a69c5d20ad to your computer and use it in GitHub Desktop.
GDScript DirectoryIterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class DirectoryIterator: | |
| var _path : String | |
| var _dir : Directory | |
| var _file_name := "" | |
| var _skip_navigational := false | |
| var _skip_hidden := false | |
| func _init(path : String, skip_navigational := false, skip_hidden := false) -> void: | |
| _path = path | |
| _skip_navigational = skip_navigational | |
| _skip_hidden = skip_hidden | |
| func should_continue() -> bool: | |
| return not _file_name.empty() | |
| func _iter_init(arg) -> bool: | |
| _dir = Directory.new() | |
| assert(_dir.open(_path) == OK) | |
| assert(_dir.list_dir_begin(_skip_navigational, _skip_hidden) == OK) | |
| _file_name = _dir.get_next() | |
| return should_continue() | |
| func _iter_next(arg) -> bool: | |
| _file_name = _dir.get_next() | |
| # Close the directory if this is the end | |
| var retval := should_continue() | |
| if not retval: | |
| _dir.list_dir_end() | |
| return retval | |
| func _iter_get(arg) -> Dictionary: | |
| return { | |
| "name" : _file_name, | |
| "is_dir" : _dir.current_is_dir() | |
| } | |
| func _ready() -> void: | |
| var paths := ["res://"] | |
| while not paths.empty(): | |
| var path = paths.pop_front() | |
| for entry in DirectoryIterator.new(path, true, true): | |
| if entry.is_dir: | |
| paths.append(path + entry.name + '/') | |
| else: | |
| print(path + entry.name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment