Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/property_utils.cpp
9822 views
1
/**************************************************************************/
2
/* property_utils.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 "property_utils.h"
32
33
#include "core/config/engine.h"
34
#include "core/object/script_language.h"
35
#include "core/templates/local_vector.h"
36
#include "scene/resources/packed_scene.h"
37
38
#ifdef TOOLS_ENABLED
39
#include "editor/editor_node.h"
40
#endif // TOOLS_ENABLED
41
42
bool PropertyUtils::is_property_value_different(const Object *p_object, const Variant &p_a, const Variant &p_b) {
43
if (p_a.get_type() == Variant::FLOAT && p_b.get_type() == Variant::FLOAT) {
44
// This must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error.
45
return !Math::is_equal_approx((float)p_a, (float)p_b);
46
} else if (p_a.get_type() == Variant::NODE_PATH && p_b.get_type() == Variant::OBJECT) {
47
// With properties of type Node, left side is NodePath, while right side is Node.
48
const Node *base_node = Object::cast_to<Node>(p_object);
49
const Node *target_node = Object::cast_to<Node>(p_b);
50
if (base_node && target_node) {
51
return p_a != base_node->get_path_to(target_node);
52
}
53
}
54
55
if (p_a.get_type() == Variant::ARRAY && p_b.get_type() == Variant::ARRAY) {
56
const Node *base_node = Object::cast_to<Node>(p_object);
57
Array array1 = p_a;
58
Array array2 = p_b;
59
60
if (base_node && !array1.is_empty() && array2.size() == array1.size() && array1[0].get_type() == Variant::NODE_PATH && array2[0].get_type() == Variant::OBJECT) {
61
// Like above, but NodePaths/Nodes are inside arrays.
62
for (int i = 0; i < array1.size(); i++) {
63
const Node *target_node = Object::cast_to<Node>(array2[i]);
64
if (!target_node || array1[i] != base_node->get_path_to(target_node)) {
65
return true;
66
}
67
}
68
return false;
69
}
70
}
71
72
// For our purposes, treating null object as NIL is the right thing to do
73
const Variant &a = p_a.get_type() == Variant::OBJECT && (Object *)p_a == nullptr ? Variant() : p_a;
74
const Variant &b = p_b.get_type() == Variant::OBJECT && (Object *)p_b == nullptr ? Variant() : p_b;
75
return a != b;
76
}
77
78
Variant PropertyUtils::get_property_default_value(const Object *p_object, const StringName &p_property, bool *r_is_valid, const Vector<SceneState::PackState> *p_states_stack_cache, bool p_update_exports, const Node *p_owner, bool *r_is_class_default) {
79
// This function obeys the way property values are set when an object is instantiated,
80
// which is the following (the latter wins):
81
// 1. Default value from builtin class
82
// 2. Default value from script exported variable (from the topmost script)
83
// 3. Value overrides from the instantiation/inheritance stack
84
85
if (r_is_class_default) {
86
*r_is_class_default = false;
87
}
88
if (r_is_valid) {
89
*r_is_valid = false;
90
}
91
92
// Handle special case "script" property, where the default value is either null or the custom type script.
93
// Do this only if there's no states stack cache to trace for default values.
94
if (!p_states_stack_cache && p_property == CoreStringName(script) && p_object->has_meta(SceneStringName(_custom_type_script))) {
95
Ref<Script> ct_scr = get_custom_type_script(p_object);
96
if (r_is_valid) {
97
*r_is_valid = true;
98
}
99
return ct_scr;
100
}
101
102
Ref<Script> topmost_script;
103
104
if (const Node *node = Object::cast_to<Node>(p_object)) {
105
// Check inheritance/instantiation ancestors
106
const Vector<SceneState::PackState> &states_stack = p_states_stack_cache ? *p_states_stack_cache : PropertyUtils::get_node_states_stack(node, p_owner);
107
for (int i = 0; i < states_stack.size(); ++i) {
108
const SceneState::PackState &ia = states_stack[i];
109
bool found = false;
110
bool node_deferred = false;
111
Variant value_in_ancestor = ia.state->get_property_value(ia.node, p_property, found, node_deferred);
112
if (found) {
113
if (r_is_valid) {
114
*r_is_valid = true;
115
}
116
// Replace properties stored as NodePaths with actual Nodes.
117
// Otherwise, the property value would be considered as overridden.
118
if (node_deferred) {
119
if (value_in_ancestor.get_type() == Variant::ARRAY) {
120
Array paths = value_in_ancestor;
121
122
bool valid = false;
123
Array array = node->get(p_property, &valid);
124
ERR_CONTINUE(!valid);
125
array = array.duplicate();
126
127
array.resize(paths.size());
128
for (int j = 0; j < array.size(); j++) {
129
array.set(j, node->get_node_or_null(paths[j]));
130
}
131
value_in_ancestor = array;
132
} else {
133
value_in_ancestor = node->get_node_or_null(value_in_ancestor);
134
}
135
}
136
return value_in_ancestor;
137
}
138
// Save script for later
139
bool has_script = false;
140
Variant script = ia.state->get_property_value(ia.node, SNAME("script"), has_script, node_deferred);
141
if (has_script) {
142
Ref<Script> scr = script;
143
if (scr.is_valid()) {
144
topmost_script = scr;
145
}
146
}
147
}
148
}
149
150
// Let's see what default is set by the topmost script having a default, if any
151
if (topmost_script.is_null()) {
152
topmost_script = p_object->get_script();
153
}
154
if (topmost_script.is_valid()) {
155
// Should be called in the editor only and not at runtime,
156
// otherwise it can cause problems because of missing instance state support
157
if (p_update_exports && Engine::get_singleton()->is_editor_hint()) {
158
topmost_script->update_exports();
159
}
160
Variant default_value;
161
if (topmost_script->get_property_default_value(p_property, default_value)) {
162
if (r_is_valid) {
163
*r_is_valid = true;
164
}
165
return default_value;
166
}
167
}
168
169
// Fall back to the default from the native class
170
{
171
if (r_is_class_default) {
172
*r_is_class_default = true;
173
}
174
bool valid = false;
175
Variant value = ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property, &valid);
176
if (valid) {
177
if (r_is_valid) {
178
*r_is_valid = true;
179
}
180
return value;
181
} else {
182
// Heuristically check if this is a synthetic property (whatever/0, whatever/1, etc.)
183
// because they are not in the class DB yet must have a default (null).
184
String prop_str = String(p_property);
185
int p = prop_str.rfind_char('/');
186
if (p != -1 && p < prop_str.length() - 1) {
187
bool all_digits = true;
188
for (int i = p + 1; i < prop_str.length(); i++) {
189
if (!is_digit(prop_str[i])) {
190
all_digits = false;
191
break;
192
}
193
}
194
if (r_is_valid) {
195
*r_is_valid = all_digits;
196
}
197
}
198
return Variant();
199
}
200
}
201
}
202
203
// Like SceneState::PackState, but using a raw pointer to avoid the cost of
204
// updating the reference count during the internal work of the functions below
205
namespace {
206
struct _FastPackState {
207
SceneState *state = nullptr;
208
int node = -1;
209
};
210
} // namespace
211
212
static bool _collect_inheritance_chain(const Ref<SceneState> &p_state, const NodePath &p_path, LocalVector<_FastPackState> &r_states_stack) {
213
bool found = false;
214
215
LocalVector<_FastPackState> inheritance_states;
216
217
Ref<SceneState> state = p_state;
218
while (state.is_valid()) {
219
int node = state->find_node_by_path(p_path);
220
if (node >= 0) {
221
// This one has state for this node
222
inheritance_states.push_back({ state.ptr(), node });
223
found = true;
224
}
225
state = state->get_base_scene_state();
226
}
227
228
if (inheritance_states.size() > 0) {
229
for (int i = inheritance_states.size() - 1; i >= 0; --i) {
230
r_states_stack.push_back(inheritance_states[i]);
231
}
232
}
233
234
return found;
235
}
236
237
Vector<SceneState::PackState> PropertyUtils::get_node_states_stack(const Node *p_node, const Node *p_owner, bool *r_instantiated_by_owner) {
238
if (r_instantiated_by_owner) {
239
*r_instantiated_by_owner = true;
240
}
241
242
LocalVector<_FastPackState> states_stack;
243
{
244
const Node *owner = p_owner;
245
#ifdef TOOLS_ENABLED
246
if (!p_owner && Engine::get_singleton()->is_editor_hint()) {
247
owner = EditorNode::get_singleton()->get_edited_scene();
248
}
249
#endif
250
251
const Node *n = p_node;
252
while (n) {
253
if (n == owner) {
254
const Ref<SceneState> &state = n->get_scene_inherited_state();
255
if (_collect_inheritance_chain(state, n->get_path_to(p_node), states_stack)) {
256
if (r_instantiated_by_owner) {
257
*r_instantiated_by_owner = false;
258
}
259
}
260
break;
261
} else if (!n->get_scene_file_path().is_empty()) {
262
const Ref<SceneState> &state = n->get_scene_instance_state();
263
_collect_inheritance_chain(state, n->get_path_to(p_node), states_stack);
264
}
265
n = n->get_owner();
266
}
267
}
268
269
// Convert to the proper type for returning, inverting the vector on the go
270
// (it was more convenient to fill the vector in reverse order)
271
Vector<SceneState::PackState> states_stack_ret;
272
{
273
states_stack_ret.resize(states_stack.size());
274
_FastPackState *ps = states_stack.ptr();
275
if (states_stack.size() > 0) {
276
for (int i = states_stack.size() - 1; i >= 0; --i) {
277
states_stack_ret.write[i].state.reference_ptr(ps->state);
278
states_stack_ret.write[i].node = ps->node;
279
++ps;
280
}
281
}
282
}
283
return states_stack_ret;
284
}
285
286
void PropertyUtils::assign_custom_type_script(Object *p_object, const Ref<Script> &p_script) {
287
ERR_FAIL_NULL(p_object);
288
ERR_FAIL_COND(p_script.is_null());
289
290
const String &path = p_script->get_path();
291
ERR_FAIL_COND(!path.is_resource_file());
292
293
ResourceUID::ID script_uid = ResourceLoader::get_resource_uid(path);
294
if (script_uid != ResourceUID::INVALID_ID) {
295
p_object->set_meta(SceneStringName(_custom_type_script), ResourceUID::get_singleton()->id_to_text(script_uid));
296
}
297
}
298
299
Ref<Script> PropertyUtils::get_custom_type_script(const Object *p_object) {
300
Variant custom_script = p_object->get_meta(SceneStringName(_custom_type_script));
301
#ifndef DISABLE_DEPRECATED
302
if (custom_script.get_type() == Variant::OBJECT) {
303
// Convert old script meta.
304
Ref<Script> script_object(custom_script);
305
assign_custom_type_script(const_cast<Object *>(p_object), script_object);
306
return script_object;
307
}
308
#endif
309
ResourceUID::ID id = ResourceUID::get_singleton()->text_to_id(custom_script);
310
if (unlikely(id == ResourceUID::INVALID_ID || !ResourceUID::get_singleton()->has_id(id))) {
311
const_cast<Object *>(p_object)->remove_meta(SceneStringName(_custom_type_script));
312
ERR_FAIL_V_MSG(Ref<Script>(), vformat("Invalid custom type script UID: %s. Removing.", custom_script.operator String()));
313
} else {
314
custom_script = ResourceUID::get_singleton()->get_id_path(id);
315
}
316
return ResourceLoader::load(custom_script);
317
}
318
319