+1 vote

What is the difference in Godot 2.1.4 (sadly not 3.0) in Directory class open() and change_dir() methods?

asked Oct 21, 2017 in Engine by Maksanty (95 points)

Think of it like this.

If you click a folder on your desktop,
a new window opens and you see inside the folder.

If you click a folder inside that window,
you change the directory to look at another one.

1 Answer

+2 votes
Best answer

I never tried it myself, but according to the documentation I understand that first you need to open a directory.
Once it is open, you can use change_dir to point it elsewhere if you wish.

For example

var dir = Directory.new()
if dir.open("user://") == OK: 
   if dir.change_dir("./xyz") == OK:
       print("You have now descended into the xyz sub directory")
answered Oct 21, 2017 by Artium Nihamkin (257 points)
selected Oct 21, 2017 by Maksanty


Good answer!

Verified that code works ( in Godot 2.1.4 )

Adding a little more for reference in case anyone needs it.
One dot   (".")   or ("./") refers to the current directory.
Two dots ("..") or ("../") refers to the parent directory.

When changing a directory, you can use
my_dir.change_dir("../") or
to change to the parent directory

Above dir.change_dir("./xyz")
looks in the current directory for a folder named "xyz",
and returns OK if it's able to change to that directory.

If it had said instead dir.change_dir("../xyz")
it would look up to the parent directory of where you are
then look inside the parent for a folder named "xyz".

Quick warning :

I just tried to make a directory in Godot 2.1.4
and you cannot use ./ to do this:
my_dir.make_dir("./xyz") will NOT work.

Instead you must use
to create a folder named "xyz" inside the current directory.

Or you can use an absolute path :

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.