Skip to content

Commit

Permalink
Merge pull request godotengine#8 from lawnjelly/duplicate_speedup
Browse files Browse the repository at this point in the history
Cache packed scenes during duplication
  • Loading branch information
lawnjelly authored Mar 30, 2023
2 parents 5f5048c + 3dbe656 commit 15f4c07
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2215,14 +2215,40 @@ Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const

bool instanced = false;

// No need to load a packed scene more than once if features
// several times in the branch being duplicated.
struct LoadedPackedScene {
Ref<PackedScene> scene;
String filename;
};
LocalVector<LoadedPackedScene> loaded_scenes;

if (Object::cast_to<InstancePlaceholder>(this)) {
const InstancePlaceholder *ip = Object::cast_to<const InstancePlaceholder>(this);
InstancePlaceholder *nip = memnew(InstancePlaceholder);
nip->set_instance_path(ip->get_instance_path());
node = nip;

} else if ((p_flags & DUPLICATE_USE_INSTANCING) && get_filename() != String()) {
Ref<PackedScene> res = ResourceLoader::load(get_filename());
// already loaded?
int found = -1;
for (unsigned int n = 0; n < loaded_scenes.size(); n++) {
if (loaded_scenes[n].filename == get_filename()) {
found = n;
break;
}
}
Ref<PackedScene> res = Ref<PackedScene>();
if (found != -1) {
res = loaded_scenes[found].scene;
} else {
LoadedPackedScene ps;
ps.filename = get_filename();
ps.scene = ResourceLoader::load(get_filename());
res = ps.scene;
loaded_scenes.push_back(ps);
}

ERR_FAIL_COND_V(res.is_null(), nullptr);
PackedScene::GenEditState ges = PackedScene::GEN_EDIT_STATE_DISABLED;
#ifdef TOOLS_ENABLED
Expand Down

0 comments on commit 15f4c07

Please sign in to comment.