Just want to understand what this script is saying

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dumuz
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.

:bust_in_silhouette: Reply From: Snail0259
  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.

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

Dumuz | 2021-07-25 04:09

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

Snail0259 | 2021-07-25 10:09

:bust_in_silhouette: Reply From: nouchata

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.

  1. 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.

  2. 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 !

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.

Dumuz | 2021-07-25 21:10