diff --git a/editor/editor_file_system.cpp b/editor/editor_file_system.cpp index 22ff7a4509b..bcfc29f7a3b 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -95,25 +95,35 @@ String EditorFileSystemDirectory::get_file(int p_idx) const { } String EditorFileSystemDirectory::get_path() const { - String p; - const EditorFileSystemDirectory *d = this; - while (d->parent) { - p = d->name.path_join(p); - d = d->parent; + int parents = 0; + const EditorFileSystemDirectory *efd = this; + // Determine the level of nesting. + while (efd->parent) { + parents++; + efd = efd->parent; } - return "res://" + p; + if (parents == 0) { + return "res://"; + } + + // Using PackedStringArray, because the path is built in reverse order. + PackedStringArray path_bits; + // Allocate an array based on nesting. It will store path bits. + path_bits.resize(parents + 2); // Last String is empty, so paths end with /. + String *path_write = path_bits.ptrw(); + path_write[0] = "res:/"; + + efd = this; + for (int i = parents; i > 0; i--) { + path_write[i] = efd->name; + efd = efd->parent; + } + return String("/").join(path_bits); } String EditorFileSystemDirectory::get_file_path(int p_idx) const { - String file = get_file(p_idx); - const EditorFileSystemDirectory *d = this; - while (d->parent) { - file = d->name.path_join(file); - d = d->parent; - } - - return "res://" + file; + return get_path().path_join(get_file(p_idx)); } Vector EditorFileSystemDirectory::get_file_deps(int p_idx) const {