instance_placeholder.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**************************************************************************/
  2. /* instance_placeholder.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "instance_placeholder.h"
  31. #include "core/io/resource_loader.h"
  32. #include "scene/resources/packed_scene.h"
  33. bool InstancePlaceholder::_set(const StringName &p_name, const Variant &p_value) {
  34. PropSet ps;
  35. ps.name = p_name;
  36. ps.value = p_value;
  37. stored_values.push_back(ps);
  38. return true;
  39. }
  40. bool InstancePlaceholder::_get(const StringName &p_name, Variant &r_ret) const {
  41. for (const PropSet &E : stored_values) {
  42. if (E.name == p_name) {
  43. r_ret = E.value;
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. void InstancePlaceholder::_get_property_list(List<PropertyInfo> *p_list) const {
  50. for (const PropSet &E : stored_values) {
  51. PropertyInfo pi;
  52. pi.name = E.name;
  53. pi.type = E.value.get_type();
  54. pi.usage = PROPERTY_USAGE_STORAGE;
  55. p_list->push_back(pi);
  56. }
  57. }
  58. void InstancePlaceholder::set_instance_path(const String &p_name) {
  59. path = p_name;
  60. }
  61. String InstancePlaceholder::get_instance_path() const {
  62. return path;
  63. }
  64. Node *InstancePlaceholder::create_instance(bool p_replace, const Ref<PackedScene> &p_custom_scene) {
  65. ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
  66. Node *base = get_parent();
  67. if (!base) {
  68. return nullptr;
  69. }
  70. Ref<PackedScene> ps;
  71. if (p_custom_scene.is_valid()) {
  72. ps = p_custom_scene;
  73. } else {
  74. ps = ResourceLoader::load(path, "PackedScene");
  75. }
  76. if (!ps.is_valid()) {
  77. return nullptr;
  78. }
  79. Node *instance = ps->instantiate();
  80. if (!instance) {
  81. return nullptr;
  82. }
  83. instance->set_name(get_name());
  84. instance->set_multiplayer_authority(get_multiplayer_authority());
  85. int pos = get_index();
  86. for (const PropSet &E : stored_values) {
  87. set_value_on_instance(this, instance, E);
  88. }
  89. if (p_replace) {
  90. queue_free();
  91. base->remove_child(this);
  92. }
  93. base->add_child(instance);
  94. base->move_child(instance, pos);
  95. return instance;
  96. }
  97. // This method will attempt to set the correct values on the placeholder instance
  98. // for regular types this is trivial and unnecessary.
  99. // For nodes however this becomes a bit tricky because they might now have existed until the instantiation,
  100. // so this method will try to find the correct nodes and resolve them.
  101. void InstancePlaceholder::set_value_on_instance(InstancePlaceholder *p_placeholder, Node *p_instance, const PropSet &p_set) {
  102. bool is_valid;
  103. // If we don't have any info, we can't do anything,
  104. // so try setting the value directly.
  105. Variant current = p_instance->get(p_set.name, &is_valid);
  106. if (!is_valid) {
  107. p_instance->set(p_set.name, p_set.value, &is_valid);
  108. return;
  109. }
  110. Variant::Type current_type = current.get_type();
  111. Variant::Type placeholder_type = p_set.value.get_type();
  112. // Arrays are a special case, because their containing type might be different.
  113. if (current_type != Variant::Type::ARRAY) {
  114. // Check if the variant types match.
  115. if (Variant::evaluate(Variant::OP_EQUAL, current_type, placeholder_type)) {
  116. p_instance->set(p_set.name, p_set.value, &is_valid);
  117. if (is_valid) {
  118. return;
  119. }
  120. // Types match but setting failed? This is strange, so let's print a warning!
  121. WARN_PRINT(vformat("Property '%s' with type '%s' could not be set when creating instance of '%s'.", p_set.name, Variant::get_type_name(current_type), p_placeholder->get_name()));
  122. return;
  123. }
  124. } else {
  125. // We are dealing with an Array.
  126. // Let's check if the subtype of the array matches first.
  127. // This is needed because the set method of ScriptInstance checks for type,
  128. // but the ClassDB set method doesn't! So we cannot reliably know what actually happens.
  129. Array current_array = current;
  130. Array placeholder_array = p_set.value;
  131. if (current_array.is_same_typed(placeholder_array)) {
  132. p_instance->set(p_set.name, p_set.value, &is_valid);
  133. if (is_valid) {
  134. return;
  135. }
  136. // Internal array types match but setting failed? This is strange, so let's print a warning!
  137. WARN_PRINT(vformat("Array Property '%s' with type '%s' could not be set when creating instance of '%s'.", p_set.name, Variant::get_type_name(Variant::Type(current_array.get_typed_builtin())), p_placeholder->get_name()));
  138. }
  139. // Arrays are not the same internal type. This should be happening because we have a NodePath Array,
  140. // but the instance wants a Node Array.
  141. }
  142. switch (current_type) {
  143. case Variant::Type::NIL: {
  144. Ref<Resource> resource = p_set.value;
  145. if (placeholder_type != Variant::Type::NODE_PATH && !resource.is_valid()) {
  146. break;
  147. }
  148. // If it's nil but we have a NodePath or a Resource, we guess what works.
  149. p_instance->set(p_set.name, p_set.value, &is_valid);
  150. if (is_valid) {
  151. break;
  152. }
  153. p_instance->set(p_set.name, try_get_node(p_placeholder, p_instance, p_set.value), &is_valid);
  154. break;
  155. }
  156. case Variant::Type::OBJECT: {
  157. if (placeholder_type != Variant::Type::NODE_PATH) {
  158. break;
  159. }
  160. // Easiest case, we want a node, but we have a deferred NodePath.
  161. p_instance->set(p_set.name, try_get_node(p_placeholder, p_instance, p_set.value));
  162. break;
  163. }
  164. case Variant::Type::ARRAY: {
  165. // If we have reached here it means our array types don't match,
  166. // so we will convert the placeholder array into the correct type
  167. // and resolve nodes if necessary.
  168. Array current_array = current;
  169. Array converted_array;
  170. Array placeholder_array = p_set.value;
  171. converted_array = current_array.duplicate();
  172. converted_array.resize(placeholder_array.size());
  173. if (Variant::evaluate(Variant::OP_EQUAL, current_array.get_typed_builtin(), Variant::Type::NODE_PATH)) {
  174. // We want a typed NodePath array.
  175. for (int i = 0; i < placeholder_array.size(); i++) {
  176. converted_array.set(i, placeholder_array[i]);
  177. }
  178. } else {
  179. // We want Nodes, convert NodePaths.
  180. for (int i = 0; i < placeholder_array.size(); i++) {
  181. converted_array.set(i, try_get_node(p_placeholder, p_instance, placeholder_array[i]));
  182. }
  183. }
  184. p_instance->set(p_set.name, converted_array, &is_valid);
  185. if (!is_valid) {
  186. WARN_PRINT(vformat("Property '%s' with type '%s' could not be set when creating instance of '%s'.", p_set.name, Variant::get_type_name(current_type), p_placeholder->get_name()));
  187. }
  188. break;
  189. }
  190. default: {
  191. WARN_PRINT(vformat("Property '%s' with type '%s' could not be set when creating instance of '%s'.", p_set.name, Variant::get_type_name(current_type), p_placeholder->get_name()));
  192. break;
  193. }
  194. }
  195. }
  196. Node *InstancePlaceholder::try_get_node(InstancePlaceholder *p_placeholder, Node *p_instance, const NodePath &p_path) {
  197. // First try to resolve internally,
  198. // if that fails try resolving externally.
  199. Node *node = p_instance->get_node_or_null(p_path);
  200. if (node == nullptr) {
  201. node = p_placeholder->get_node_or_null(p_path);
  202. }
  203. return node;
  204. }
  205. Dictionary InstancePlaceholder::get_stored_values(bool p_with_order) {
  206. Dictionary ret;
  207. PackedStringArray order;
  208. for (const PropSet &E : stored_values) {
  209. ret[E.name] = E.value;
  210. if (p_with_order) {
  211. order.push_back(E.name);
  212. }
  213. };
  214. if (p_with_order) {
  215. ret[".order"] = order;
  216. }
  217. return ret;
  218. }
  219. void InstancePlaceholder::_bind_methods() {
  220. ClassDB::bind_method(D_METHOD("get_stored_values", "with_order"), &InstancePlaceholder::get_stored_values, DEFVAL(false));
  221. ClassDB::bind_method(D_METHOD("create_instance", "replace", "custom_scene"), &InstancePlaceholder::create_instance, DEFVAL(false), DEFVAL(Variant()));
  222. ClassDB::bind_method(D_METHOD("get_instance_path"), &InstancePlaceholder::get_instance_path);
  223. }
  224. InstancePlaceholder::InstancePlaceholder() {
  225. }