0 votes

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 :) 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!

Godot version 3.2
in Engine by (12 points)

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]

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

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.