浏览代码

Merge pull request #17295 from eon-s/instance-placeholder

Make InstancePlaceholder more flexible by allowing to instance without removing it.
Juan Linietsky 7 年之前
父节点
当前提交
7b85c9d0ea
共有 2 个文件被更改,包括 19 次插入6 次删除
  1. 18 6
      scene/main/instance_placeholder.cpp
  2. 1 0
      scene/main/instance_placeholder.h

+ 18 - 6
scene/main/instance_placeholder.cpp

@@ -52,6 +52,7 @@ bool InstancePlaceholder::_get(const StringName &p_name, Variant &r_ret) const {
 	}
 	}
 	return false;
 	return false;
 }
 }
+
 void InstancePlaceholder::_get_property_list(List<PropertyInfo> *p_list) const {
 void InstancePlaceholder::_get_property_list(List<PropertyInfo> *p_list) const {
 
 
 	for (const List<PropSet>::Element *E = stored_values.front(); E; E = E->next()) {
 	for (const List<PropSet>::Element *E = stored_values.front(); E; E = E->next()) {
@@ -73,13 +74,14 @@ String InstancePlaceholder::get_instance_path() const {
 
 
 	return path;
 	return path;
 }
 }
-void InstancePlaceholder::replace_by_instance(const Ref<PackedScene> &p_custom_scene) {
 
 
-	ERR_FAIL_COND(!is_inside_tree());
+Node *InstancePlaceholder::create_instance(bool p_replace, const Ref<PackedScene> &p_custom_scene) {
+
+	ERR_FAIL_COND_V(!is_inside_tree(), NULL);
 
 
 	Node *base = get_parent();
 	Node *base = get_parent();
 	if (!base)
 	if (!base)
-		return;
+		return NULL;
 
 
 	Ref<PackedScene> ps;
 	Ref<PackedScene> ps;
 	if (p_custom_scene.is_valid())
 	if (p_custom_scene.is_valid())
@@ -88,7 +90,7 @@ void InstancePlaceholder::replace_by_instance(const Ref<PackedScene> &p_custom_s
 		ps = ResourceLoader::load(path, "PackedScene");
 		ps = ResourceLoader::load(path, "PackedScene");
 
 
 	if (!ps.is_valid())
 	if (!ps.is_valid())
-		return;
+		return NULL;
 	Node *scene = ps->instance();
 	Node *scene = ps->instance();
 	scene->set_name(get_name());
 	scene->set_name(get_name());
 	int pos = get_position_in_parent();
 	int pos = get_position_in_parent();
@@ -97,11 +99,20 @@ void InstancePlaceholder::replace_by_instance(const Ref<PackedScene> &p_custom_s
 		scene->set(E->get().name, E->get().value);
 		scene->set(E->get().name, E->get().value);
 	}
 	}
 
 
-	queue_delete();
+	if (p_replace) {
+		queue_delete();
+		base->remove_child(this);
+	}
 
 
-	base->remove_child(this);
 	base->add_child(scene);
 	base->add_child(scene);
 	base->move_child(scene, pos);
 	base->move_child(scene, pos);
+
+	return scene;
+}
+
+void InstancePlaceholder::replace_by_instance(const Ref<PackedScene> &p_custom_scene) {
+	//Deprecated by
+	create_instance(true, p_custom_scene);
 }
 }
 
 
 Dictionary InstancePlaceholder::get_stored_values(bool p_with_order) {
 Dictionary InstancePlaceholder::get_stored_values(bool p_with_order) {
@@ -124,6 +135,7 @@ Dictionary InstancePlaceholder::get_stored_values(bool p_with_order) {
 void InstancePlaceholder::_bind_methods() {
 void InstancePlaceholder::_bind_methods() {
 
 
 	ClassDB::bind_method(D_METHOD("get_stored_values", "with_order"), &InstancePlaceholder::get_stored_values, DEFVAL(false));
 	ClassDB::bind_method(D_METHOD("get_stored_values", "with_order"), &InstancePlaceholder::get_stored_values, DEFVAL(false));
+	ClassDB::bind_method(D_METHOD("create_instance", "replace", "custom_scene"), &InstancePlaceholder::create_instance, DEFVAL(false), DEFVAL(Variant()));
 	ClassDB::bind_method(D_METHOD("replace_by_instance", "custom_scene"), &InstancePlaceholder::replace_by_instance, DEFVAL(Variant()));
 	ClassDB::bind_method(D_METHOD("replace_by_instance", "custom_scene"), &InstancePlaceholder::replace_by_instance, DEFVAL(Variant()));
 	ClassDB::bind_method(D_METHOD("get_instance_path"), &InstancePlaceholder::get_instance_path);
 	ClassDB::bind_method(D_METHOD("get_instance_path"), &InstancePlaceholder::get_instance_path);
 }
 }

+ 1 - 0
scene/main/instance_placeholder.h

@@ -60,6 +60,7 @@ public:
 
 
 	Dictionary get_stored_values(bool p_with_order = false);
 	Dictionary get_stored_values(bool p_with_order = false);
 
 
+	Node *create_instance(bool p_replace = false, const Ref<PackedScene> &p_custom_scene = Ref<PackedScene>());
 	void replace_by_instance(const Ref<PackedScene> &p_custom_scene = Ref<PackedScene>());
 	void replace_by_instance(const Ref<PackedScene> &p_custom_scene = Ref<PackedScene>());
 
 
 	InstancePlaceholder();
 	InstancePlaceholder();