0 votes

While trying to figure out the easiest way of sorting the best lap times I decided to store the lap times in the name of the files so that they are automatically sorted in an ascending order inside the folder. But how can I call only the first 10 files in the folder?

Godot version 3.2.3
in Engine by (326 points)

1 Answer

0 votes

Hello,
what you are trying to do can be achieved using the following code:

func get_10_files(dir: String) -> Array:
    var d := Directory.new()
    d.open(dir)
    d.list_dir_begin(true, true)
    var files := []
    for i in 10:
        var file_name := d.get_next()
        if lap_time == "":
            break # there was less than 10 files
        files.append(file_name)
    return files

However this is a really weird way of doing things. Array have a sort method that sort their content.
A better method would be to store al scores in a single file and work from here rather than relying on your OS sorting file names...

by (728 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.