0 votes

I want to make a load scene where every .file is represented in a box. Meaning I want to create a multiple load scene that I can just press and it load. I need to detect if there is .file with any name so I can use it

Godot version 3.2.3
in Engine by (383 points)

1 Answer

0 votes

You can use this function:

func get_files_from_directory(path : String, suffix : String = "") -> PoolStringArray:
    var list : PoolStringArray = PoolStringArray()
    var dir : Directory = Directory.new()
    if not dir.dir_exists(path):
        push_warning("Path does not exist: " + path)
        return list
    dir.open(path)
    dir.list_dir_begin(true)
    var file : String = " "
    while file != "":
        file = dir.get_next()
        if not file:
            break # EOD
        if dir.current_is_dir():
            continue # Just files, no directories
        if suffix and not file.ends_with(suffix):
            continue # not the correct suffix
        list.append(path + file)
    return list

It will return an Array with all files that end with '.file' in user:// if you use it like this:

var files : PoolStringArray = get_files_from_directory("user://", ".file")
by (1,480 points)
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.