The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes
func get_file_paths_in_folder(folder_path: String) -> Array:

    var file_paths := []

    var dir := Directory.new()
    dir.open(folder_path)
    dir.list_dir_begin(true, true) # true, true params to skip hidden and navigational

    while true:
        var file := dir.get_next()
        if file == "":
            break
        if !file.ends_with(".import"):
            continue 
        file_paths.append(folder_path + file.replace(".import", ""))

    dir.list_dir_end()

    return file_paths

Godot is my first programming language I'm learning and haven't touched static typing at all. I've have to use this func in order to Godot to export to android with all my images I'm using.

My questions:

  1. while what's true? What is the while method using to check if it's true?

  2. list_dir_begin in the Godot docs explains that method as "Initializes the stream used to list all files and directories". Can someone explain what this means in a coding sense

  3. file_paths.append(folder_path + file.replace(".import", "")) This line seems to delete the ".import" at the end of the import files, is this correct? Does this make them .png's again in Godot's eye's? Or is this forcing Godot to some how reference the actual images?

Thank you for your help. I'm still trying to figure this stuff out.

Godot version 3.3
in Engine by (367 points)

2 Answers

+1 vote
Best answer

so here's the answers to your questions :

  1. this actual loop is not making any sense it's like a kid wrote that ... but fyi while true makes an infinite loop, the only way to break it is the break keyword but it's prolly the worst way to make a loop idk why the guy did that, please update the whole while code with that instead :

    var file := dir.get_next()
    while file != "":
        if file.ends_with(".import"):
            file_paths.append(folder_path + file.replace(".import", ""))
        file = dir.get_next()
    

    it's the "not shitty and normal" way to do that.

  2. it's more a low-level language feature but since you can't know in advance how many and which files are located in the directory you're searching, you can't blindly allocate memory to it, it would be unproductive so you're using a stream. A stream is a data flux of an undefined size, you need to read it with a buffer (it's the var dir := Directory.new() in that case), since the data flux is just some data about files contained in a directory put end to end you need the Directory class because it knows how to read the data.

  3. in fact, here's the full purpose of this line : adding at the end of the file_paths array the file path made of the folder path + the name of the file w/out the .import extension. it does that for each file with the .import extension, not for the others. it does not "make them .png's again", the whole function just returns an array with these values, that's all.

upvote if i helped you !

by (46 points)
selected by

Appreciate the feedback. I'll probably scrap this script and generate something that better suits the fix I'm using it for. I appreciate the explanations!

This original script was a github answer to an exporting issue with android.

0 votes
  1. while true is just checking if true == true. if you go while playing:, it just means that while the game is playing, do what is inside that block until playing == false, or there is a break.

you might be right about the third one, I don't know.

not sure about the rest. Hope you work them out.

by (248 points)

Gotcha, So it's a way to make a loop without outside parameters effecting it's life.

Yeah, while true just runs until it is stopped, just not allowing any other things to happen until a 'break' is called.

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 Frequently asked questions and 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 [email protected] with your username.