Checking for tscn files

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Serenade
:warning: Old Version Published before Godot 3 was released.

Hey!
Im trying to make a func, where when you press a button, it searches, are there any files there and then prints them(each’s name)… but it doesnt work!
Here’s the code…

func _on_Hide_pressed():
var dir = Directory.new()
if dir.open("res://SAVES_folder") == true:
	dir.list_dir_begin()
	var files = dir.get_next()
	while(files != ""):
		if dir.current_is_dir():
			print(files)
		else:
			files = dir.get_next()
else:
	print("error")

When i press the Hide button, it prints the “error” statement of my code. Whats is wrong here? I followed the example that is in Godot code it self…

“”“”“”“”"
func dir_contents(path):

var dir = Directory.new()

if dir.open(path) == OK:

dir.list_dir_begin()

var file_name = dir.get_next()

while (file_name != “”):

if dir.current_is_dir():

print("Found directory: " + file_name)

else:

print("Found file: " + file_name)

file_name = dir.get_next()

else:

print(“An error occurred when trying to access the path.”)
“”“”“”"

:bust_in_silhouette: Reply From: M. Alkotob

First of all, the line:

if dir.open("res://SAVES_folder") == true:

needs to have OK instead of true, like so:

if dir.open("res://SAVES_folder") == OK:

Since dir.open(“res://SAVES_folder”) returns either 0 or 31, and doesn’t return true/false. (OK is 0 in this case).

Also, in your code:

if dir.current_is_dir(): print(files)

This will print out the name of folders and not files, since dir.current_is_dir() returns true when the path is a directory.

If you want to print file names, try this:
if not dir.current_is_dir(): print(files)

Which will invert the condition.

And use user:// for user files, not res://, you may have problems later on the exported game

eons | 2017-03-19 21:24

I did this, changed to == OK and if not dir.current_is_dir(): print(files)…
But when i run it and press on that Hide button, it freezes and it shows no errors in debugger :confused:

Serenade | 2017-03-20 07:36

Remove the “else” from the while loop. The last line needs to execute in every single while iteration.

while(files != ""):
    if dir.current_is_dir():
        print(files)
    files = dir.get_next()

M. Alkotob | 2017-03-20 10:00

It worked! Thank you very much! :slight_smile:

Serenade | 2017-03-20 10:11

It’s my bad, I forgot add the “not” to the new code.

This line should be like this:

if dir.current_is_dir():

if not dir.current_is_dir():

Edit: You’re welcome!

M. Alkotob | 2017-03-20 10:18

Yeah, luckely, i remembered about the Not statement…
But how do i make “if no files are found”?

Serenade | 2017-03-20 10:21

Before the while put:

var number_of_files = 0

and in the if() inside the while(), add an incrementor:

if not dir.current_is_dir():
    print(files)
    number_of_files +=1 #This is the increment statement

After the while, add:

if(number_of_files==0):
    print("No save files found!")

M. Alkotob | 2017-03-20 11:07

Thank you, works like a charm! :slight_smile:

Serenade | 2017-03-20 11:41