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 = Directory.new()
    dir.open(path)

    if dir.file_exists(path):
        arr.append(path)

    else:
        dir.list_dir_begin(true, true)
        while(true):
            var subpath = dir.get_next()
            if subpath.empty():
                break
            arr += get_dir_files(path.plus_file(subpath))
    print(arr)
    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/image_viewer.gd]
[E:/godot/PROJECT/scenes/main.tscn]
[E:/godot/PROJECT/scenes/UI_Hotbar.tscn]
[E:/godot/PROJECT/scenes/image_viewer.gd, E:/godot/PROJECT/scenes/main.tscn, E:/godot/PROJECT/scenes/UI_Hotbar.tscn]

EDIT:
I know it happens because of

func get_dir_files(path: String):
    var arr = []
    var dir = Directory.new()
    dir.open(path)

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

Hi,
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/image_viewer.gd, 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