Node *Node::duplicate_and_reown(const Map<Node*,Node*>& p_reown_map) const { ERR_FAIL_COND_V(get_filename()!="",NULL); Node *node=NULL; Object *obj = ObjectTypeDB::instance(get_type()); if (!obj) { print_line("could not duplicate: "+String(get_type())); } ERR_FAIL_COND_V(!obj,NULL); node = obj->cast_to<Node>(); if (!node) memdelete(obj); ERR_FAIL_COND_V(!node,NULL); node->set_name(get_name()); List<PropertyInfo> plist; get_property_list(&plist); for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) { if (!(E->get().usage&PROPERTY_USAGE_STORAGE)) continue; String name = E->get().name; node->set( name, get(name) ); } for(int i=0;i<get_child_count();i++) { get_child(i)->_duplicate_and_reown(node,p_reown_map); } _duplicate_signals(this,node); return node; }
Node *Node::duplicate(bool p_use_instancing) const { Node *node=NULL; bool instanced=false; if (cast_to<InstancePlaceholder>()) { const InstancePlaceholder *ip = cast_to<const InstancePlaceholder>(); InstancePlaceholder *nip = memnew( InstancePlaceholder ); nip->set_instance_path( ip->get_instance_path() ); node=nip; } else if (p_use_instancing && get_filename()!=String()) { Ref<PackedScene> res = ResourceLoader::load(get_filename()); ERR_FAIL_COND_V(res.is_null(),NULL); node=res->instance(); ERR_FAIL_COND_V(!node,NULL); instanced=true; } else { Object *obj = ObjectTypeDB::instance(get_type()); ERR_FAIL_COND_V(!obj,NULL); node = obj->cast_to<Node>(); if (!node) memdelete(obj); ERR_FAIL_COND_V(!node,NULL); } if (get_filename()!="") { //an instance node->set_filename(get_filename()); } List<PropertyInfo> plist; get_property_list(&plist); for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) { if (!(E->get().usage&PROPERTY_USAGE_STORAGE)) continue; String name = E->get().name; node->set( name, get(name) ); } node->set_name(get_name()); List<GroupInfo> gi; get_groups(&gi); for (List<GroupInfo>::Element *E=gi.front();E;E=E->next()) { node->add_to_group(E->get().name, E->get().persistent); } _duplicate_signals(this, node); for(int i=0;i<get_child_count();i++) { if (get_child(i)->data.parent_owned) continue; if (instanced && get_child(i)->data.owner==this) continue; //part of instance Node *dup = get_child(i)->duplicate(p_use_instancing); if (!dup) { memdelete(node); return NULL; } node->add_child(dup); } return node; }