Created
September 8, 2019 02:24
-
-
Save Razzlegames/28dcb74bae01a41843276b7a0252272e to your computer and use it in GitHub Desktop.
This file contains 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
extends Node2D | |
export(String, DIR) var levelsPath | |
func _ready(): | |
print("-----------------------------------------------------------") | |
print("Start Directory walk: "+ levelsPath) | |
print("-----------------------------------------------------------") | |
print("-----------------------------------------------------------") | |
print("Start Recursive Depth First Walk") | |
print("-----------------------------------------------------------") | |
recursiveWalk(levelsPath) | |
print("-----------------------------------------------------------") | |
print("Start Breadth First Walk") | |
print("-----------------------------------------------------------") | |
breadthFirstWalk(levelsPath) | |
print("-----------------------------------------------------------") | |
print(" Done recursing all directories :)") | |
print("-----------------------------------------------------------") | |
func recursiveWalk(dirPath): | |
var dir = openDir(dirPath) | |
dir.list_dir_begin(true, true) | |
var fileName = dir.get_next() | |
while fileName != "": | |
var filePath = dirPath + "/" + fileName | |
if dir.current_is_dir(): | |
print("Dir found decending " + filePath) | |
recursiveWalk(filePath) | |
else: | |
print("File Path: " + filePath) | |
# Process file HERE | |
fileName = dir.get_next() | |
print("Directory walking done: " + dirPath) | |
dir.list_dir_end() | |
func breadthFirstWalk(dirPath): | |
print("-----------------------------------------------------------") | |
print(dirPath + " is the ROOT directory") | |
print("-----------------------------------------------------------") | |
var directories = [dirPath] | |
while !directories.empty(): | |
var currentDirPath = directories.pop_front() | |
var dir = openDir(currentDirPath) | |
dir.list_dir_begin(true, true) | |
var fileName = dir.get_next() | |
while fileName != "": | |
var filePath = currentDirPath + "/" + fileName | |
if dir.current_is_dir(): | |
print(filePath + " is a directory") | |
directories.push_back(filePath) | |
else: | |
print(filePath + " is a file") | |
# Process File HERE | |
fileName = dir.get_next() | |
dir.list_dir_end() | |
print("Done with directory : " + currentDirPath) | |
func openDir(dirPath): | |
var dir = Directory.new() | |
if dir.open(dirPath) != OK: | |
print("Error opening directory: "+ dirPath) | |
assert(false) | |
return null | |
return dir |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment