0 votes

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.listdirbegin()

var filename = dir.getnext()

while (file_name != ""):

if dir.currentisdir():

print("Found directory: " + file_name)

else:

print("Found file: " + file_name)

filename = dir.getnext()

else:

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

asked Mar 19, 2017 in Engine by Serenade (222 points)

1 Answer

+1 vote
Best answer

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.

answered Mar 19, 2017 by M. Alkotob (162 points)
selected Mar 20, 2017 by Serenade

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():
        print(files)
    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():
    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!")

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 How to use this Q&A? before posting your first questions.