How should I remove the arrays before the last one from my print?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Koterminion

I am tryin to open files inside subfolders with the file dialog

extends Node

func get_dir_files(path: String):
    var arr = []
    var dir =

    if dir.file_exists(path):

        dir.list_dir_begin(true, true)
            var subpath = dir.get_next()
            if subpath.empty():
            arr += get_dir_files(path.plus_file(subpath))
    return arr

when I run this (it browses for folders recursively and adds files inside them to an array), it keeps the files it finds in separate arrays that are all still referenced by arr (because if I do + [1] it adds it to every single array) The last array is essentially the array I need but I want to get rid of all the previous ones :slight_smile: How would I go on doing that, the last array is the only necessary one? The print below shows the output:

[E:/godot/PROJECT/scenes/, E:/godot/PROJECT/scenes/main.tscn, E:/godot/PROJECT/scenes/UI_Hotbar.tscn]

I know it happens because of

func get_dir_files(path: String):
    var arr = []
    var dir =

happening recursively but I am not sure where to add the final puzzle piece to make this thing clean and functional!

can you explain what do you mean with the “last array”?

This is the array what the function will return back to your initial call:

[E:/godot/PROJECT/scenes/, E:/godot/PROJECT/scenes/main.tscn, E:/godot/PROJECT/scenes/UI_Hotbar.tscn]

klaas | 2021-07-09 20:25

It got solved! I had to add an array that wasn’t local and += that array with the local one!

Koterminion | 2021-07-10 00:04