open() vs change_dir()

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

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

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.

Grass H0PEr | 2017-10-21 21:18

:bust_in_silhouette: Reply From: Artium Nihamkin

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")

Directory — Godot Engine (2.1) documentation in English

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
my_dir.change_dir("..")
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
my_dir.make_dir("xyz")
to create a folder named “xyz” inside the current directory.

Or you can use an absolute path :
my_dir.make_dir("user://xyz")

Grass H0PEr | 2017-10-21 21:35