Debian Codegraph @

scene/main/instance_placeholder.cpp

raw symbols SCIP
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
31 #include "instance_placeholder.h"
32
33 #include "core/io/resource_loader.h"
34 #include "scene/resources/packed_scene.h"
35
36 bool InstancePlaceholder::_set(const StringName &p_name, const Variant &p_value) {
37 PropSet ps;
38 ps.name = p_name;
39 ps.value = p_value;
40 stored_values.push_back(ps);
41 return true;
42 }
43
44 bool InstancePlaceholder::_get(const StringName &p_name, Variant &r_ret) const {
45 for (const PropSet &E : stored_values) {
46 if (E.name == p_name) {
47 r_ret = E.value;
48 return true;
49 }
50 }
51 return false;
52 }
53
54 void InstancePlaceholder::_get_property_list(List<PropertyInfo> *p_list) const {
55 for (const PropSet &E : stored_values) {
56 PropertyInfo pi;
57 pi.name = E.name;
58 pi.type = E.value.get_type();
59 pi.usage = PROPERTY_USAGE_STORAGE;
60
61 p_list->push_back(pi);
62 }
63 }
64
65 void InstancePlaceholder::set_instance_path(const String &p_name) {
66 path = p_name;
67 }
68
69 String InstancePlaceholder::get_instance_path() const {
70 return path;
71 }
72
73 Node *InstancePlaceholder::create_instance(bool p_replace, const Ref<PackedScene> &p_custom_scene) {
74 ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
75
76 Node *base = get_parent();
77 if (!base) {
78 return nullptr;
79 }
80
81 Ref<PackedScene> ps;
82 if (p_custom_scene.is_valid()) {
83 ps = p_custom_scene;
84 } else {
85 ps = ResourceLoader::load(path, "PackedScene");
86 }
87
88 if (ps.is_null()) {
89 return nullptr;
90 }
91 Node *instance = ps->instantiate();
92 if (!instance) {
93 return nullptr;
94 }
95 instance->set_name(get_name());
96 instance->set_multiplayer_authority(get_multiplayer_authority());
97 int pos = get_index();
98
99 for (const PropSet &E : stored_values) {
100 set_value_on_instance(this, instance, E);
101 }
102
103 if (p_replace) {
104 queue_free();
105 base->remove_child(this);
106 }
107
108 base->add_child(instance);
109 base->move_child(instance, pos);
110
111 return instance;
112 }
113
114 // This method will attempt to set the correct values on the placeholder instance
115 // for regular types this is trivial and unnecessary.
116 // For nodes however this becomes a bit tricky because they might now have existed until the instantiation,
117 // so this method will try to find the correct nodes and resolve them.
118 void InstancePlaceholder::set_value_on_instance(InstancePlaceholder *p_placeholder, Node *p_instance, const PropSet &p_set) {
119 bool is_valid;
120
121 // If we don't have any info, we can't do anything,
122 // so try setting the value directly.
123 Variant current = p_instance->get(p_set.name, &is_valid);
124 if (!is_valid) {
125 p_instance->set(p_set.name, p_set.value, &is_valid);
126 return;
127 }
128
129 Variant::Type current_type = current.get_type();
130 Variant::Type placeholder_type = p_set.value.get_type();
131
132 // Arrays are a special case, because their containing type might be different.
133 if (current_type != Variant::Type::ARRAY) {
134 // Check if the variant types match.
135 if (Variant::evaluate(Variant::OP_EQUAL, current_type, placeholder_type)) {
136 p_instance->set(p_set.name, p_set.value, &is_valid);
137 if (is_valid) {
138 return;
139 }
140 // Types match but setting failed? This is strange, so let's print a warning!
141 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()));
142 return;
143 }
144 } else {
145 // We are dealing with an Array.
146 // Let's check if the subtype of the array matches first.
147 // This is needed because the set method of ScriptInstance checks for type,
148 // but the ClassDB set method doesn't! So we cannot reliably know what actually happens.
149 Array current_array = current;
150 Array placeholder_array = p_set.value;
151 if (current_array.is_same_typed(placeholder_array)) {
152 p_instance->set(p_set.name, p_set.value, &is_valid);
153 if (is_valid) {
154 return;
155 }
156 // Internal array types match but setting failed? This is strange, so let's print a warning!
157 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()));
158 }
159 // Arrays are not the same internal type. This should be happening because we have a NodePath Array,
160 // but the instance wants a Node Array.
161 }
162
163 switch (current_type) {
164 case Variant::Type::NIL: {
165 Ref<Resource> resource = p_set.value;
166 if (placeholder_type != Variant::Type::NODE_PATH && resource.is_null()) {
167 break;
168 }
169 // If it's nil but we have a NodePath or a Resource, we guess what works.
170 p_instance->set(p_set.name, p_set.value, &is_valid);
171 if (is_valid) {
172 break;
173 }
174
175 p_instance->set(p_set.name, try_get_node(p_placeholder, p_instance, p_set.value), &is_valid);
176 break;
177 }
178 case Variant::Type::OBJECT: {
179 if (placeholder_type != Variant::Type::NODE_PATH) {
180 break;
181 }
182 // Easiest case, we want a node, but we have a deferred NodePath.
183 p_instance->set(p_set.name, try_get_node(p_placeholder, p_instance, p_set.value));
184 break;
185 }
186 case Variant::Type::ARRAY: {
187 // If we have reached here it means our array types don't match,
188 // so we will convert the placeholder array into the correct type
189 // and resolve nodes if necessary.
190 Array current_array = current;
191 Array converted_array;
192 Array placeholder_array = p_set.value;
193 converted_array = current_array.duplicate();
194 converted_array.resize(placeholder_array.size());
195
196 if (Variant::evaluate(Variant::OP_EQUAL, current_array.get_typed_builtin(), Variant::Type::NODE_PATH)) {
197 // We want a typed NodePath array.
198 for (int i = 0; i < placeholder_array.size(); i++) {
199 converted_array.set(i, placeholder_array[i]);
200 }
201 } else {
202 // We want Nodes, convert NodePaths.
203 for (int i = 0; i < placeholder_array.size(); i++) {
204 converted_array.set(i, try_get_node(p_placeholder, p_instance, placeholder_array[i]));
205 }
206 }
207
208 p_instance->set(p_set.name, converted_array, &is_valid);
209 if (!is_valid) {
210 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()));
211 }
212 break;
213 }
214 default: {
215 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()));
216 break;
217 }
218 }
219 }
220
221 Node *InstancePlaceholder::try_get_node(InstancePlaceholder *p_placeholder, Node *p_instance, const NodePath &p_path) {
222 // First try to resolve internally,
223 // if that fails try resolving externally.
224 Node *node = p_instance->get_node_or_null(p_path);
225 if (node == nullptr) {
226 node = p_placeholder->get_node_or_null(p_path);
227 }
228
229 return node;
230 }
231
232 Dictionary InstancePlaceholder::get_stored_values(bool p_with_order) {
233 Dictionary ret;
234 PackedStringArray order;
235
236 for (const PropSet &E : stored_values) {
237 ret[E.name] = E.value;
238 if (p_with_order) {
239 order.push_back(E.name);
240 }
241 };
242
243 if (p_with_order) {
244 ret[".order"] = order;
245 }
246
247 return ret;
248 }
249
250 void InstancePlaceholder::_bind_methods() {
251 ClassDB::bind_method(D_METHOD("get_stored_values", "with_order"), &InstancePlaceholder::get_stored_values, DEFVAL(false));
252 ClassDB::bind_method(D_METHOD("create_instance", "replace", "custom_scene"), &InstancePlaceholder::create_instance, DEFVAL(false), DEFVAL(Variant()));
253 ClassDB::bind_method(D_METHOD("get_instance_path"), &InstancePlaceholder::get_instance_path);
254 }
255
256 InstancePlaceholder::InstancePlaceholder() {
257 }