convert file path to relative and back - something like nodejs path.relative(from,to) in gdscript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By blurymind

I need this in order to implement proper file saving in a little app I am making in godot 2.3

I havent upgraded it to 3*, because the task is too huge.
I need to do this in gdscript, and have no idea how to do it in a clean way:
https://nodejs.org/api/path.html#path_path_relative_from_to

then of course convert the relative paths back to absolute paths. How does one do it in godot?

:bust_in_silhouette: Reply From: wombatstampede

I am still unsure if you search the answer for Godot 3.x oder 2.x. Also some example paths would be helpful. Also why the paths would be relative and why there would be the need to convert them. I am also unsure if you mean relative paths in term of “res://” and “user://” or in terms of “…/file_in_parentdir.txt”.

Helpful for path management (i put Godot 3.x links in here) is the File class:
https://docs.godotengine.org/en/latest/classes/class_file.html?highlight=absolute
It has the methods: get_path and get_path_absolute
The caveat is that a file has to be opened for read to be able to use these methods. (I see no other way here at least)

Another helpful class is the String class:

It has some methods to split up a path to directory/filename/extension and also the methods : bool is_abs_path ( ) and bool is_rel_path ( )

Then there’s also the OS class which gives paths to some directory used in the operating system. You can get these together with string functions to i.e. move paths between user:// and the absolute OS path.

Be aware that there might not always be a resource data dir in a released app. Resources (images, 3d, sound) might as well be into some package. Also ressources which you import/copy into a godot 3 project might not be present in the release as they may be converted and moved into another directory.