下面的函数打印路径中的目录和文件,我希望它通过使用递归函数进入目录和子目录。这会导致堆栈溢出错误。它只有在不调用"RecursiveSearch“函数,而只打印出目录和文件,而不是子目录的情况下才能工作。
extends Node
func RecursiveSearch(dir):
var path = ("res://")
var err = dir.open(path)
if err != OK:
print("error occurred")
return
dir.list_dir_begin() # points to the first one, true ignores special directory entries (.) and (..)
var name = dir.get_next() # retrieves the firdt file or dir
while name != "":
if dir.current_is_dir(): # test if it's a dir type
print("dir : " , name)
RecursiveSearch(dir)
elif !dir.current_is_dir():
print("file : " , name)
else:
break
name = dir.get_next() # points to the next dir or file
dir.list_dir_end()
func _ready():
var dir = Directory.new()
RecursiveSearch(dir)发布于 2020-12-30 13:38:42
Directory.list_dir_begin()仅列出目录的直接子目录。要递归到子目录,您需要为该子目录创建一个新的目录对象,并在该对象上调用您的RecursiveSearch。
现在,您的函数使用自己的参数调用自己。它将做完全相同的事情--用相同的参数调用自己--一次又一次,直到它达到递归极限。
https://stackoverflow.com/questions/65383730
复制相似问题