This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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 =
if"res://SAVES_folder") == true:
    var files = dir.get_next()
    while(files != ""):
        if dir.current_is_dir():
            files = dir.get_next()

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 =

if == OK:


var filename = dir.getnext()

while (file_name != ""):

if dir.currentisdir():

print("Found directory: " + file_name)


print("Found file: " + file_name)

filename = dir.getnext()


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

in Engine by (227 points)

1 Answer

+1 vote
Best answer

First of all, the line:

if"res://SAVES_folder") == true:

needs to have OK instead of true, like so:

if"res://SAVES_folder") == OK:

Since"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.

by (162 points)
selected by

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

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

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():
    files = dir.get_next()

It worked! Thank you very much! :)

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!

Yeah,, luckely, i remembered about the Not statement..
But how do i make "if no files are found"?

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():
    number_of_files +=1 #This is the increment statement

After the while, add:

    print("No save files found!")

Thank you, works like a charm! :)

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.