Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/node.cpp
9903 views
1
/**************************************************************************/
2
/* node.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 "node.h"
32
#include "node.compat.inc"
33
34
#include "core/config/project_settings.h"
35
#include "core/io/resource_loader.h"
36
#include "core/object/message_queue.h"
37
#include "core/object/script_language.h"
38
#include "core/string/print_string.h"
39
#include "instance_placeholder.h"
40
#include "scene/animation/tween.h"
41
#include "scene/debugger/scene_debugger.h"
42
#include "scene/main/multiplayer_api.h"
43
#include "scene/main/window.h"
44
#include "scene/resources/packed_scene.h"
45
#include "viewport.h"
46
47
int Node::orphan_node_count = 0;
48
49
thread_local Node *Node::current_process_thread_group = nullptr;
50
51
void Node::_notification(int p_notification) {
52
switch (p_notification) {
53
case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
54
if (data.accessibility_element.is_valid()) {
55
DisplayServer::get_singleton()->accessibility_free_element(data.accessibility_element);
56
data.accessibility_element = RID();
57
}
58
} break;
59
60
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
61
RID ae = get_accessibility_element();
62
ERR_FAIL_COND(ae.is_null());
63
64
DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_name());
65
66
// Node children.
67
if (!accessibility_override_tree_hierarchy()) {
68
for (int i = 0; i < get_child_count(); i++) {
69
Node *child_node = get_child(i);
70
Window *child_wnd = Object::cast_to<Window>(child_node);
71
if (child_wnd && !(child_wnd->is_visible() && (child_wnd->is_embedded() || child_wnd->is_popup()))) {
72
continue;
73
}
74
if (child_node->is_part_of_edited_scene()) {
75
continue;
76
}
77
DisplayServer::get_singleton()->accessibility_update_add_child(ae, child_node->get_accessibility_element());
78
}
79
}
80
} break;
81
82
case NOTIFICATION_PROCESS: {
83
GDVIRTUAL_CALL(_process, get_process_delta_time());
84
} break;
85
86
case NOTIFICATION_PHYSICS_PROCESS: {
87
GDVIRTUAL_CALL(_physics_process, get_physics_process_delta_time());
88
} break;
89
90
case NOTIFICATION_ENTER_TREE: {
91
ERR_FAIL_NULL(get_viewport());
92
ERR_FAIL_NULL(data.tree);
93
94
if (data.tree->is_accessibility_supported() && !is_part_of_edited_scene()) {
95
data.tree->_accessibility_force_update();
96
data.tree->_accessibility_notify_change(this);
97
if (data.parent) {
98
data.tree->_accessibility_notify_change(data.parent);
99
} else {
100
data.tree->_accessibility_notify_change(get_window()); // Root node.
101
}
102
}
103
104
// Update process mode.
105
if (data.process_mode == PROCESS_MODE_INHERIT) {
106
if (data.parent) {
107
data.process_owner = data.parent->data.process_owner;
108
} else {
109
ERR_PRINT("The root node can't be set to Inherit process mode, reverting to Pausable instead.");
110
data.process_mode = PROCESS_MODE_PAUSABLE;
111
data.process_owner = this;
112
}
113
} else {
114
data.process_owner = this;
115
}
116
117
{ // Update threaded process mode.
118
if (data.process_thread_group == PROCESS_THREAD_GROUP_INHERIT) {
119
if (data.parent) {
120
data.process_thread_group_owner = data.parent->data.process_thread_group_owner;
121
}
122
123
if (data.process_thread_group_owner) {
124
data.process_group = data.process_thread_group_owner->data.process_group;
125
} else {
126
data.process_group = &data.tree->default_process_group;
127
}
128
} else {
129
data.process_thread_group_owner = this;
130
_add_process_group();
131
}
132
133
if (_is_any_processing()) {
134
_add_to_process_thread_group();
135
}
136
}
137
138
if (data.physics_interpolation_mode == PHYSICS_INTERPOLATION_MODE_INHERIT) {
139
bool interpolate = true; // Root node default is for interpolation to be on.
140
if (data.parent) {
141
interpolate = data.parent->is_physics_interpolated();
142
}
143
_propagate_physics_interpolated(interpolate);
144
}
145
146
// Update auto translate mode.
147
if (data.auto_translate_mode == AUTO_TRANSLATE_MODE_INHERIT && !data.parent) {
148
ERR_PRINT("The root node can't be set to Inherit auto translate mode, reverting to Always instead.");
149
data.auto_translate_mode = AUTO_TRANSLATE_MODE_ALWAYS;
150
}
151
data.is_auto_translate_dirty = true;
152
data.is_translation_domain_dirty = true;
153
154
if (data.input) {
155
add_to_group("_vp_input" + itos(get_viewport()->get_instance_id()));
156
}
157
if (data.shortcut_input) {
158
add_to_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
159
}
160
if (data.unhandled_input) {
161
add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
162
}
163
if (data.unhandled_key_input) {
164
add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
165
}
166
167
data.tree->nodes_in_tree_count++;
168
orphan_node_count--;
169
170
} break;
171
172
case NOTIFICATION_POST_ENTER_TREE: {
173
if (data.auto_translate_mode != AUTO_TRANSLATE_MODE_DISABLED) {
174
notification(NOTIFICATION_TRANSLATION_CHANGED);
175
}
176
} break;
177
178
case NOTIFICATION_EXIT_TREE: {
179
ERR_FAIL_NULL(get_viewport());
180
ERR_FAIL_NULL(data.tree);
181
182
if (data.tree->is_accessibility_supported() && !is_part_of_edited_scene()) {
183
if (data.accessibility_element.is_valid()) {
184
DisplayServer::get_singleton()->accessibility_free_element(data.accessibility_element);
185
data.accessibility_element = RID();
186
}
187
data.tree->_accessibility_notify_change(this, true);
188
if (data.parent) {
189
data.tree->_accessibility_notify_change(data.parent);
190
} else {
191
data.tree->_accessibility_notify_change(get_window()); // Root node.
192
}
193
}
194
195
data.tree->nodes_in_tree_count--;
196
orphan_node_count++;
197
198
if (data.input) {
199
remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id()));
200
}
201
if (data.shortcut_input) {
202
remove_from_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
203
}
204
if (data.unhandled_input) {
205
remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
206
}
207
if (data.unhandled_key_input) {
208
remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
209
}
210
211
// Remove from processing first.
212
if (_is_any_processing()) {
213
_remove_from_process_thread_group();
214
}
215
// Remove the process group.
216
if (data.process_thread_group_owner == this) {
217
_remove_process_group();
218
}
219
data.process_thread_group_owner = nullptr;
220
data.process_owner = nullptr;
221
222
if (data.path_cache) {
223
memdelete(data.path_cache);
224
data.path_cache = nullptr;
225
}
226
} break;
227
228
case NOTIFICATION_SUSPENDED:
229
case NOTIFICATION_PAUSED: {
230
if (is_physics_interpolated_and_enabled() && is_inside_tree()) {
231
reset_physics_interpolation();
232
}
233
} break;
234
235
case NOTIFICATION_PATH_RENAMED: {
236
if (data.path_cache) {
237
memdelete(data.path_cache);
238
data.path_cache = nullptr;
239
}
240
} break;
241
242
case NOTIFICATION_READY: {
243
if (GDVIRTUAL_IS_OVERRIDDEN(_input)) {
244
set_process_input(true);
245
}
246
247
if (GDVIRTUAL_IS_OVERRIDDEN(_shortcut_input)) {
248
set_process_shortcut_input(true);
249
}
250
251
if (GDVIRTUAL_IS_OVERRIDDEN(_unhandled_input)) {
252
set_process_unhandled_input(true);
253
}
254
255
if (GDVIRTUAL_IS_OVERRIDDEN(_unhandled_key_input)) {
256
set_process_unhandled_key_input(true);
257
}
258
259
if (GDVIRTUAL_IS_OVERRIDDEN(_process)) {
260
set_process(true);
261
}
262
if (GDVIRTUAL_IS_OVERRIDDEN(_physics_process)) {
263
set_physics_process(true);
264
}
265
266
GDVIRTUAL_CALL(_ready);
267
} break;
268
269
case NOTIFICATION_PREDELETE: {
270
if (data.tree && !Thread::is_main_thread()) {
271
cancel_free();
272
ERR_PRINT("Attempted to free a node that is currently added to the SceneTree from a thread. This is not permitted, use queue_free() instead. Node has not been freed.");
273
return;
274
}
275
276
if (data.owner) {
277
_clean_up_owner();
278
}
279
280
while (!data.owned.is_empty()) {
281
Node *n = data.owned.back()->get();
282
n->_clean_up_owner(); // This will change data.owned. So it's impossible to loop over the list in the usual manner.
283
}
284
285
if (data.parent) {
286
data.parent->remove_child(this);
287
}
288
289
// kill children as cleanly as possible
290
while (data.children.size()) {
291
Node *child = data.children.last()->value; // begin from the end because its faster and more consistent with creation
292
memdelete(child);
293
}
294
} break;
295
296
case NOTIFICATION_TRANSLATION_CHANGED: {
297
if (data.tree) {
298
data.is_auto_translate_dirty = true;
299
}
300
} break;
301
}
302
}
303
304
void Node::_propagate_ready() {
305
data.ready_notified = true;
306
data.blocked++;
307
for (KeyValue<StringName, Node *> &K : data.children) {
308
K.value->_propagate_ready();
309
}
310
311
data.blocked--;
312
313
notification(NOTIFICATION_POST_ENTER_TREE);
314
315
if (data.ready_first) {
316
data.ready_first = false;
317
notification(NOTIFICATION_READY);
318
emit_signal(SceneStringName(ready));
319
}
320
}
321
322
void Node::_propagate_enter_tree() {
323
// this needs to happen to all children before any enter_tree
324
325
if (data.parent) {
326
data.tree = data.parent->data.tree;
327
data.depth = data.parent->data.depth + 1;
328
} else {
329
data.depth = 1;
330
}
331
332
data.viewport = Object::cast_to<Viewport>(this);
333
if (!data.viewport && data.parent) {
334
data.viewport = data.parent->data.viewport;
335
}
336
337
for (KeyValue<StringName, GroupData> &E : data.grouped) {
338
E.value.group = data.tree->add_to_group(E.key, this);
339
}
340
341
notification(NOTIFICATION_ENTER_TREE);
342
343
GDVIRTUAL_CALL(_enter_tree);
344
345
emit_signal(SceneStringName(tree_entered));
346
347
data.tree->node_added(this);
348
349
if (data.parent) {
350
Variant c = this;
351
const Variant *cptr = &c;
352
data.parent->emit_signalp(SNAME("child_entered_tree"), &cptr, 1);
353
}
354
355
data.blocked++;
356
//block while adding children
357
358
for (KeyValue<StringName, Node *> &K : data.children) {
359
if (!K.value->is_inside_tree()) { // could have been added in enter_tree
360
K.value->_propagate_enter_tree();
361
}
362
}
363
364
data.blocked--;
365
366
#ifdef DEBUG_ENABLED
367
SceneDebugger::add_to_cache(data.scene_file_path, this);
368
#endif
369
// enter groups
370
}
371
372
void Node::_propagate_after_exit_tree() {
373
// Clear owner if it was not part of the pruned branch
374
if (data.owner) {
375
if (!data.owner->is_ancestor_of(this)) {
376
_clean_up_owner();
377
}
378
}
379
380
data.blocked++;
381
382
for (HashMap<StringName, Node *>::Iterator I = data.children.last(); I; --I) {
383
I->value->_propagate_after_exit_tree();
384
}
385
386
data.blocked--;
387
388
emit_signal(SceneStringName(tree_exited));
389
}
390
391
void Node::_propagate_exit_tree() {
392
//block while removing children
393
394
#ifdef DEBUG_ENABLED
395
if (!data.scene_file_path.is_empty()) {
396
// Only remove if file path is set (optimization).
397
SceneDebugger::remove_from_cache(data.scene_file_path, this);
398
}
399
#endif
400
data.blocked++;
401
402
for (HashMap<StringName, Node *>::Iterator I = data.children.last(); I; --I) {
403
I->value->_propagate_exit_tree();
404
}
405
406
data.blocked--;
407
408
GDVIRTUAL_CALL(_exit_tree);
409
410
emit_signal(SceneStringName(tree_exiting));
411
412
notification(NOTIFICATION_EXIT_TREE, true);
413
if (data.tree) {
414
data.tree->node_removed(this);
415
}
416
417
if (data.parent) {
418
Variant c = this;
419
const Variant *cptr = &c;
420
data.parent->emit_signalp(SNAME("child_exiting_tree"), &cptr, 1);
421
}
422
423
// exit groups
424
for (KeyValue<StringName, GroupData> &E : data.grouped) {
425
data.tree->remove_from_group(E.key, this);
426
E.value.group = nullptr;
427
}
428
429
data.viewport = nullptr;
430
431
if (data.tree) {
432
data.tree->tree_changed();
433
}
434
435
data.ready_notified = false;
436
data.tree = nullptr;
437
data.depth = -1;
438
}
439
440
void Node::_propagate_physics_interpolated(bool p_interpolated) {
441
switch (data.physics_interpolation_mode) {
442
case PHYSICS_INTERPOLATION_MODE_INHERIT:
443
// Keep the parent p_interpolated.
444
break;
445
case PHYSICS_INTERPOLATION_MODE_OFF: {
446
p_interpolated = false;
447
} break;
448
case PHYSICS_INTERPOLATION_MODE_ON: {
449
p_interpolated = true;
450
} break;
451
}
452
453
// No change? No need to propagate further.
454
if (data.physics_interpolated == p_interpolated) {
455
return;
456
}
457
458
data.physics_interpolated = p_interpolated;
459
460
// Allow a call to the RenderingServer etc. in derived classes.
461
_physics_interpolated_changed();
462
463
update_configuration_warnings();
464
465
data.blocked++;
466
for (KeyValue<StringName, Node *> &K : data.children) {
467
K.value->_propagate_physics_interpolated(p_interpolated);
468
}
469
data.blocked--;
470
}
471
472
void Node::_propagate_physics_interpolation_reset_requested(bool p_requested) {
473
if (is_physics_interpolated()) {
474
data.physics_interpolation_reset_requested = p_requested;
475
}
476
477
data.blocked++;
478
for (KeyValue<StringName, Node *> &K : data.children) {
479
K.value->_propagate_physics_interpolation_reset_requested(p_requested);
480
}
481
data.blocked--;
482
}
483
484
void Node::move_child(Node *p_child, int p_index) {
485
ERR_FAIL_COND_MSG(data.tree && !Thread::is_main_thread(), "Moving child node positions inside the SceneTree is only allowed from the main thread. Use call_deferred(\"move_child\",child,index).");
486
ERR_FAIL_NULL(p_child);
487
ERR_FAIL_COND_MSG(p_child->data.parent != this, "Child is not a child of this node.");
488
489
_update_children_cache();
490
// We need to check whether node is internal and move it only in the relevant node range.
491
if (p_child->data.internal_mode == INTERNAL_MODE_FRONT) {
492
if (p_index < 0) {
493
p_index += data.internal_children_front_count_cache;
494
}
495
ERR_FAIL_INDEX_MSG(p_index, data.internal_children_front_count_cache, vformat("Invalid new child index: %d. Child is internal.", p_index));
496
_move_child(p_child, p_index);
497
} else if (p_child->data.internal_mode == INTERNAL_MODE_BACK) {
498
if (p_index < 0) {
499
p_index += data.internal_children_back_count_cache;
500
}
501
ERR_FAIL_INDEX_MSG(p_index, data.internal_children_back_count_cache, vformat("Invalid new child index: %d. Child is internal.", p_index));
502
_move_child(p_child, (int)data.children_cache.size() - data.internal_children_back_count_cache + p_index);
503
} else {
504
if (p_index < 0) {
505
p_index += get_child_count(false);
506
}
507
ERR_FAIL_INDEX_MSG(p_index, (int)data.children_cache.size() + 1 - data.internal_children_front_count_cache - data.internal_children_back_count_cache, vformat("Invalid new child index: %d.", p_index));
508
_move_child(p_child, p_index + data.internal_children_front_count_cache);
509
}
510
}
511
512
void Node::_move_child(Node *p_child, int p_index, bool p_ignore_end) {
513
ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, `move_child()` failed. Consider using `move_child.call_deferred(child, index)` instead (or `popup.call_deferred()` if this is from a popup).");
514
515
// Specifying one place beyond the end
516
// means the same as moving to the last index
517
if (!p_ignore_end) { // p_ignore_end is a little hack to make back internal children work properly.
518
if (p_child->data.internal_mode == INTERNAL_MODE_FRONT) {
519
if (p_index == data.internal_children_front_count_cache) {
520
p_index--;
521
}
522
} else if (p_child->data.internal_mode == INTERNAL_MODE_BACK) {
523
if (p_index == (int)data.children_cache.size()) {
524
p_index--;
525
}
526
} else {
527
if (p_index == (int)data.children_cache.size() - data.internal_children_back_count_cache) {
528
p_index--;
529
}
530
}
531
}
532
533
int child_index = p_child->get_index();
534
535
if (child_index == p_index) {
536
return; //do nothing
537
}
538
539
int motion_from = MIN(p_index, child_index);
540
int motion_to = MAX(p_index, child_index);
541
542
data.children_cache.remove_at(child_index);
543
data.children_cache.insert(p_index, p_child);
544
545
if (data.tree) {
546
data.tree->tree_changed();
547
}
548
549
data.blocked++;
550
//new pos first
551
for (int i = motion_from; i <= motion_to; i++) {
552
if (data.children_cache[i]->data.internal_mode == INTERNAL_MODE_DISABLED) {
553
data.children_cache[i]->data.index = i - data.internal_children_front_count_cache;
554
} else if (data.children_cache[i]->data.internal_mode == INTERNAL_MODE_BACK) {
555
data.children_cache[i]->data.index = i - data.internal_children_front_count_cache - data.external_children_count_cache;
556
} else {
557
data.children_cache[i]->data.index = i;
558
}
559
}
560
// notification second
561
move_child_notify(p_child);
562
notification(NOTIFICATION_CHILD_ORDER_CHANGED);
563
emit_signal(SNAME("child_order_changed"));
564
p_child->_propagate_groups_dirty();
565
566
data.blocked--;
567
}
568
569
void Node::_propagate_groups_dirty() {
570
for (const KeyValue<StringName, GroupData> &E : data.grouped) {
571
if (E.value.group) {
572
E.value.group->changed = true;
573
}
574
}
575
576
for (KeyValue<StringName, Node *> &K : data.children) {
577
K.value->_propagate_groups_dirty();
578
}
579
}
580
581
void Node::add_child_notify(Node *p_child) {
582
// to be used when not wanted
583
}
584
585
void Node::remove_child_notify(Node *p_child) {
586
// to be used when not wanted
587
}
588
589
void Node::move_child_notify(Node *p_child) {
590
// to be used when not wanted
591
}
592
593
void Node::owner_changed_notify() {
594
}
595
596
void Node::_physics_interpolated_changed() {}
597
598
void Node::set_physics_process(bool p_process) {
599
ERR_THREAD_GUARD
600
if (data.physics_process == p_process) {
601
return;
602
}
603
604
if (!is_inside_tree()) {
605
data.physics_process = p_process;
606
return;
607
}
608
609
if (_is_any_processing()) {
610
_remove_from_process_thread_group();
611
}
612
613
data.physics_process = p_process;
614
615
if (_is_any_processing()) {
616
_add_to_process_thread_group();
617
}
618
}
619
620
bool Node::is_physics_processing() const {
621
return data.physics_process;
622
}
623
624
void Node::set_physics_process_internal(bool p_process_internal) {
625
ERR_THREAD_GUARD
626
if (data.physics_process_internal == p_process_internal) {
627
return;
628
}
629
630
if (!is_inside_tree()) {
631
data.physics_process_internal = p_process_internal;
632
return;
633
}
634
635
if (_is_any_processing()) {
636
_remove_from_process_thread_group();
637
}
638
639
data.physics_process_internal = p_process_internal;
640
641
if (_is_any_processing()) {
642
_add_to_process_thread_group();
643
}
644
}
645
646
bool Node::is_physics_processing_internal() const {
647
return data.physics_process_internal;
648
}
649
650
void Node::set_process_mode(ProcessMode p_mode) {
651
ERR_THREAD_GUARD
652
if (data.process_mode == p_mode) {
653
return;
654
}
655
656
if (!is_inside_tree()) {
657
data.process_mode = p_mode;
658
return;
659
}
660
661
bool prev_can_process = can_process();
662
bool prev_enabled = _is_enabled();
663
664
if (p_mode == PROCESS_MODE_INHERIT) {
665
if (data.parent) {
666
data.process_owner = data.parent->data.process_owner;
667
} else {
668
ERR_FAIL_MSG("The root node can't be set to Inherit process mode.");
669
}
670
} else {
671
data.process_owner = this;
672
}
673
674
data.process_mode = p_mode;
675
676
bool next_can_process = can_process();
677
bool next_enabled = _is_enabled();
678
679
int pause_notification = 0;
680
681
if (prev_can_process && !next_can_process) {
682
pause_notification = NOTIFICATION_PAUSED;
683
} else if (!prev_can_process && next_can_process) {
684
pause_notification = NOTIFICATION_UNPAUSED;
685
}
686
687
int enabled_notification = 0;
688
689
if (prev_enabled && !next_enabled) {
690
enabled_notification = NOTIFICATION_DISABLED;
691
} else if (!prev_enabled && next_enabled) {
692
enabled_notification = NOTIFICATION_ENABLED;
693
}
694
695
_propagate_process_owner(data.process_owner, pause_notification, enabled_notification);
696
697
#ifdef TOOLS_ENABLED
698
// This is required for the editor to update the visibility of disabled nodes
699
// It's very expensive during runtime to change, so editor-only
700
if (Engine::get_singleton()->is_editor_hint()) {
701
data.tree->emit_signal(SNAME("tree_process_mode_changed"));
702
}
703
704
_emit_editor_state_changed();
705
#endif
706
}
707
708
void Node::_propagate_pause_notification(bool p_enable) {
709
bool prev_can_process = _can_process(!p_enable);
710
bool next_can_process = _can_process(p_enable);
711
712
if (prev_can_process && !next_can_process) {
713
notification(NOTIFICATION_PAUSED);
714
} else if (!prev_can_process && next_can_process) {
715
notification(NOTIFICATION_UNPAUSED);
716
}
717
718
data.blocked++;
719
for (KeyValue<StringName, Node *> &K : data.children) {
720
K.value->_propagate_pause_notification(p_enable);
721
}
722
data.blocked--;
723
}
724
725
void Node::_propagate_suspend_notification(bool p_enable) {
726
notification(p_enable ? NOTIFICATION_SUSPENDED : NOTIFICATION_UNSUSPENDED);
727
728
data.blocked++;
729
for (KeyValue<StringName, Node *> &KV : data.children) {
730
KV.value->_propagate_suspend_notification(p_enable);
731
}
732
data.blocked--;
733
}
734
735
Node::ProcessMode Node::get_process_mode() const {
736
return data.process_mode;
737
}
738
739
void Node::_propagate_process_owner(Node *p_owner, int p_pause_notification, int p_enabled_notification) {
740
data.process_owner = p_owner;
741
742
if (p_pause_notification != 0) {
743
notification(p_pause_notification);
744
}
745
746
if (p_enabled_notification != 0) {
747
notification(p_enabled_notification);
748
}
749
750
data.blocked++;
751
for (KeyValue<StringName, Node *> &K : data.children) {
752
Node *c = K.value;
753
if (c->data.process_mode == PROCESS_MODE_INHERIT) {
754
c->_propagate_process_owner(p_owner, p_pause_notification, p_enabled_notification);
755
}
756
}
757
data.blocked--;
758
}
759
760
void Node::set_multiplayer_authority(int p_peer_id, bool p_recursive) {
761
ERR_THREAD_GUARD
762
data.multiplayer_authority = p_peer_id;
763
764
if (p_recursive) {
765
for (KeyValue<StringName, Node *> &K : data.children) {
766
K.value->set_multiplayer_authority(p_peer_id, true);
767
}
768
}
769
}
770
771
int Node::get_multiplayer_authority() const {
772
return data.multiplayer_authority;
773
}
774
775
bool Node::is_multiplayer_authority() const {
776
ERR_FAIL_COND_V(!is_inside_tree(), false);
777
778
Ref<MultiplayerAPI> api = get_multiplayer();
779
return api.is_valid() && (api->get_unique_id() == data.multiplayer_authority);
780
}
781
782
/***** RPC CONFIG ********/
783
784
void Node::rpc_config(const StringName &p_method, const Variant &p_config) {
785
ERR_THREAD_GUARD
786
if (data.rpc_config.get_type() != Variant::DICTIONARY) {
787
data.rpc_config = Dictionary();
788
}
789
Dictionary node_config = data.rpc_config;
790
if (p_config.get_type() == Variant::NIL) {
791
node_config.erase(p_method);
792
} else {
793
ERR_FAIL_COND(p_config.get_type() != Variant::DICTIONARY);
794
node_config[p_method] = p_config;
795
}
796
}
797
798
const Variant Node::get_node_rpc_config() const {
799
return data.rpc_config;
800
}
801
802
/***** RPC FUNCTIONS ********/
803
804
Error Node::_rpc_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
805
if (p_argcount < 1) {
806
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
807
r_error.expected = 1;
808
return ERR_INVALID_PARAMETER;
809
}
810
811
if (!p_args[0]->is_string()) {
812
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
813
r_error.argument = 0;
814
r_error.expected = Variant::STRING_NAME;
815
return ERR_INVALID_PARAMETER;
816
}
817
818
StringName method = (*p_args[0]).operator StringName();
819
820
Error err = rpcp(0, method, &p_args[1], p_argcount - 1);
821
r_error.error = Callable::CallError::CALL_OK;
822
return err;
823
}
824
825
Error Node::_rpc_id_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
826
if (p_argcount < 2) {
827
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
828
r_error.expected = 2;
829
return ERR_INVALID_PARAMETER;
830
}
831
832
if (p_args[0]->get_type() != Variant::INT) {
833
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
834
r_error.argument = 0;
835
r_error.expected = Variant::INT;
836
return ERR_INVALID_PARAMETER;
837
}
838
839
if (!p_args[1]->is_string()) {
840
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
841
r_error.argument = 1;
842
r_error.expected = Variant::STRING_NAME;
843
return ERR_INVALID_PARAMETER;
844
}
845
846
int peer_id = *p_args[0];
847
StringName method = (*p_args[1]).operator StringName();
848
849
Error err = rpcp(peer_id, method, &p_args[2], p_argcount - 2);
850
r_error.error = Callable::CallError::CALL_OK;
851
return err;
852
}
853
854
Error Node::rpcp(int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) {
855
ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
856
857
Ref<MultiplayerAPI> api = get_multiplayer();
858
if (api.is_null()) {
859
return ERR_UNCONFIGURED;
860
}
861
return api->rpcp(this, p_peer_id, p_method, p_arg, p_argcount);
862
}
863
864
Ref<MultiplayerAPI> Node::get_multiplayer() const {
865
if (!is_inside_tree()) {
866
return Ref<MultiplayerAPI>();
867
}
868
return data.tree->get_multiplayer(get_path());
869
}
870
871
//////////// end of rpc
872
873
bool Node::can_process_notification(int p_what) const {
874
switch (p_what) {
875
case NOTIFICATION_PHYSICS_PROCESS:
876
return data.physics_process;
877
case NOTIFICATION_PROCESS:
878
return data.process;
879
case NOTIFICATION_INTERNAL_PROCESS:
880
return data.process_internal;
881
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS:
882
return data.physics_process_internal;
883
}
884
885
return true;
886
}
887
888
bool Node::can_process() const {
889
ERR_FAIL_COND_V(!is_inside_tree(), false);
890
return !data.tree->is_suspended() && _can_process(data.tree->is_paused());
891
}
892
893
bool Node::_can_process(bool p_paused) const {
894
ProcessMode process_mode;
895
896
if (data.process_mode == PROCESS_MODE_INHERIT) {
897
if (!data.process_owner) {
898
process_mode = PROCESS_MODE_PAUSABLE;
899
} else {
900
process_mode = data.process_owner->data.process_mode;
901
}
902
} else {
903
process_mode = data.process_mode;
904
}
905
906
// The owner can't be set to inherit, must be a bug.
907
ERR_FAIL_COND_V(process_mode == PROCESS_MODE_INHERIT, false);
908
909
if (process_mode == PROCESS_MODE_DISABLED) {
910
return false;
911
} else if (process_mode == PROCESS_MODE_ALWAYS) {
912
return true;
913
}
914
915
if (p_paused) {
916
return process_mode == PROCESS_MODE_WHEN_PAUSED;
917
} else {
918
return process_mode == PROCESS_MODE_PAUSABLE;
919
}
920
}
921
922
void Node::set_physics_interpolation_mode(PhysicsInterpolationMode p_mode) {
923
ERR_THREAD_GUARD
924
if (data.physics_interpolation_mode == p_mode) {
925
return;
926
}
927
928
data.physics_interpolation_mode = p_mode;
929
930
bool interpolate = true; // Default for root node.
931
932
switch (p_mode) {
933
case PHYSICS_INTERPOLATION_MODE_INHERIT: {
934
if (is_inside_tree() && data.parent) {
935
interpolate = data.parent->is_physics_interpolated();
936
}
937
} break;
938
case PHYSICS_INTERPOLATION_MODE_OFF: {
939
interpolate = false;
940
} break;
941
case PHYSICS_INTERPOLATION_MODE_ON: {
942
interpolate = true;
943
} break;
944
}
945
946
_propagate_physics_interpolated(interpolate);
947
948
// Auto-reset on changing interpolation mode.
949
if (is_physics_interpolated() && is_inside_tree()) {
950
propagate_notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
951
}
952
}
953
954
void Node::reset_physics_interpolation() {
955
if (SceneTree::is_fti_enabled() && is_inside_tree()) {
956
propagate_notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
957
958
// If `reset_physics_interpolation()` is called explicitly by the user
959
// (e.g. from scripts) then we prevent deferred auto-resets taking place.
960
// The user is trusted to call reset in the right order, and auto-reset
961
// will interfere with their control of prev / curr, so should be turned off.
962
_propagate_physics_interpolation_reset_requested(false);
963
}
964
}
965
966
bool Node::_is_enabled() const {
967
ProcessMode process_mode;
968
969
if (data.process_mode == PROCESS_MODE_INHERIT) {
970
if (!data.process_owner) {
971
process_mode = PROCESS_MODE_PAUSABLE;
972
} else {
973
process_mode = data.process_owner->data.process_mode;
974
}
975
} else {
976
process_mode = data.process_mode;
977
}
978
979
return (process_mode != PROCESS_MODE_DISABLED);
980
}
981
982
bool Node::is_enabled() const {
983
ERR_FAIL_COND_V(!is_inside_tree(), false);
984
return _is_enabled();
985
}
986
987
double Node::get_physics_process_delta_time() const {
988
if (data.tree) {
989
return data.tree->get_physics_process_time();
990
} else {
991
return 0;
992
}
993
}
994
995
double Node::get_process_delta_time() const {
996
if (data.tree) {
997
return data.tree->get_process_time();
998
} else {
999
return 0;
1000
}
1001
}
1002
1003
void Node::set_process(bool p_process) {
1004
ERR_THREAD_GUARD
1005
if (data.process == p_process) {
1006
return;
1007
}
1008
1009
if (!is_inside_tree()) {
1010
data.process = p_process;
1011
return;
1012
}
1013
1014
if (_is_any_processing()) {
1015
_remove_from_process_thread_group();
1016
}
1017
1018
data.process = p_process;
1019
1020
if (_is_any_processing()) {
1021
_add_to_process_thread_group();
1022
}
1023
}
1024
1025
bool Node::is_processing() const {
1026
return data.process;
1027
}
1028
1029
void Node::set_process_internal(bool p_process_internal) {
1030
ERR_THREAD_GUARD
1031
if (data.process_internal == p_process_internal) {
1032
return;
1033
}
1034
1035
if (!is_inside_tree()) {
1036
data.process_internal = p_process_internal;
1037
return;
1038
}
1039
1040
if (_is_any_processing()) {
1041
_remove_from_process_thread_group();
1042
}
1043
1044
data.process_internal = p_process_internal;
1045
1046
if (_is_any_processing()) {
1047
_add_to_process_thread_group();
1048
}
1049
}
1050
1051
void Node::_add_process_group() {
1052
data.tree->_add_process_group(this);
1053
}
1054
1055
void Node::_remove_process_group() {
1056
data.tree->_remove_process_group(this);
1057
}
1058
1059
void Node::_remove_from_process_thread_group() {
1060
data.tree->_remove_node_from_process_group(this, data.process_thread_group_owner);
1061
}
1062
1063
void Node::_add_to_process_thread_group() {
1064
data.tree->_add_node_to_process_group(this, data.process_thread_group_owner);
1065
}
1066
1067
void Node::_remove_tree_from_process_thread_group() {
1068
if (!is_inside_tree()) {
1069
return; // May not be initialized yet.
1070
}
1071
1072
for (KeyValue<StringName, Node *> &K : data.children) {
1073
if (K.value->data.process_thread_group != PROCESS_THREAD_GROUP_INHERIT) {
1074
continue;
1075
}
1076
1077
K.value->_remove_tree_from_process_thread_group();
1078
}
1079
1080
if (_is_any_processing()) {
1081
_remove_from_process_thread_group();
1082
}
1083
}
1084
1085
void Node::_add_tree_to_process_thread_group(Node *p_owner) {
1086
if (_is_any_processing()) {
1087
_add_to_process_thread_group();
1088
}
1089
1090
data.process_thread_group_owner = p_owner;
1091
if (p_owner != nullptr) {
1092
data.process_group = p_owner->data.process_group;
1093
} else {
1094
data.process_group = &data.tree->default_process_group;
1095
}
1096
1097
for (KeyValue<StringName, Node *> &K : data.children) {
1098
if (K.value->data.process_thread_group != PROCESS_THREAD_GROUP_INHERIT) {
1099
continue;
1100
}
1101
1102
K.value->_add_to_process_thread_group();
1103
}
1104
}
1105
bool Node::is_processing_internal() const {
1106
return data.process_internal;
1107
}
1108
1109
void Node::set_process_thread_group_order(int p_order) {
1110
ERR_THREAD_GUARD
1111
if (data.process_thread_group_order == p_order) {
1112
return;
1113
}
1114
1115
data.process_thread_group_order = p_order;
1116
1117
// Not yet in the tree (or not a group owner, in whose case this is pointless but harmless); trivial update.
1118
if (!is_inside_tree() || data.process_thread_group_owner != this) {
1119
return;
1120
}
1121
1122
data.tree->process_groups_dirty = true;
1123
}
1124
1125
int Node::get_process_thread_group_order() const {
1126
return data.process_thread_group_order;
1127
}
1128
1129
void Node::set_process_priority(int p_priority) {
1130
ERR_THREAD_GUARD
1131
if (data.process_priority == p_priority) {
1132
return;
1133
}
1134
if (!is_inside_tree()) {
1135
// Not yet in the tree; trivial update.
1136
data.process_priority = p_priority;
1137
return;
1138
}
1139
1140
if (_is_any_processing()) {
1141
_remove_from_process_thread_group();
1142
}
1143
1144
data.process_priority = p_priority;
1145
1146
if (_is_any_processing()) {
1147
_add_to_process_thread_group();
1148
}
1149
}
1150
1151
int Node::get_process_priority() const {
1152
return data.process_priority;
1153
}
1154
1155
void Node::set_physics_process_priority(int p_priority) {
1156
ERR_THREAD_GUARD
1157
if (data.physics_process_priority == p_priority) {
1158
return;
1159
}
1160
if (!is_inside_tree()) {
1161
// Not yet in the tree; trivial update.
1162
data.physics_process_priority = p_priority;
1163
return;
1164
}
1165
1166
if (_is_any_processing()) {
1167
_remove_from_process_thread_group();
1168
}
1169
1170
data.physics_process_priority = p_priority;
1171
1172
if (_is_any_processing()) {
1173
_add_to_process_thread_group();
1174
}
1175
}
1176
1177
int Node::get_physics_process_priority() const {
1178
return data.physics_process_priority;
1179
}
1180
1181
void Node::set_process_thread_group(ProcessThreadGroup p_mode) {
1182
ERR_FAIL_COND_MSG(data.tree && !Thread::is_main_thread(), "Changing the process thread group can only be done from the main thread. Use call_deferred(\"set_process_thread_group\",mode).");
1183
if (data.process_thread_group == p_mode) {
1184
return;
1185
}
1186
1187
if (!is_inside_tree()) {
1188
// Not yet in the tree; trivial update.
1189
data.process_thread_group = p_mode;
1190
return;
1191
}
1192
1193
_remove_tree_from_process_thread_group();
1194
if (data.process_thread_group != PROCESS_THREAD_GROUP_INHERIT) {
1195
_remove_process_group();
1196
}
1197
1198
data.process_thread_group = p_mode;
1199
1200
if (p_mode == PROCESS_THREAD_GROUP_INHERIT) {
1201
if (data.parent) {
1202
data.process_thread_group_owner = data.parent->data.process_thread_group_owner;
1203
} else {
1204
data.process_thread_group_owner = nullptr;
1205
}
1206
} else {
1207
data.process_thread_group_owner = this;
1208
_add_process_group();
1209
}
1210
1211
_add_tree_to_process_thread_group(data.process_thread_group_owner);
1212
1213
notify_property_list_changed();
1214
}
1215
1216
Node::ProcessThreadGroup Node::get_process_thread_group() const {
1217
return data.process_thread_group;
1218
}
1219
1220
void Node::set_process_thread_messages(BitField<ProcessThreadMessages> p_flags) {
1221
ERR_THREAD_GUARD
1222
if (data.process_thread_messages == p_flags) {
1223
return;
1224
}
1225
1226
data.process_thread_messages = p_flags;
1227
}
1228
1229
BitField<Node::ProcessThreadMessages> Node::get_process_thread_messages() const {
1230
return data.process_thread_messages;
1231
}
1232
1233
void Node::set_process_input(bool p_enable) {
1234
ERR_THREAD_GUARD
1235
if (p_enable == data.input) {
1236
return;
1237
}
1238
1239
data.input = p_enable;
1240
if (!is_inside_tree()) {
1241
return;
1242
}
1243
1244
if (p_enable) {
1245
add_to_group("_vp_input" + itos(get_viewport()->get_instance_id()));
1246
} else {
1247
remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id()));
1248
}
1249
}
1250
1251
bool Node::is_processing_input() const {
1252
return data.input;
1253
}
1254
1255
void Node::set_process_shortcut_input(bool p_enable) {
1256
ERR_THREAD_GUARD
1257
if (p_enable == data.shortcut_input) {
1258
return;
1259
}
1260
data.shortcut_input = p_enable;
1261
if (!is_inside_tree()) {
1262
return;
1263
}
1264
1265
if (p_enable) {
1266
add_to_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
1267
} else {
1268
remove_from_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
1269
}
1270
}
1271
1272
bool Node::is_processing_shortcut_input() const {
1273
return data.shortcut_input;
1274
}
1275
1276
void Node::set_process_unhandled_input(bool p_enable) {
1277
ERR_THREAD_GUARD
1278
if (p_enable == data.unhandled_input) {
1279
return;
1280
}
1281
data.unhandled_input = p_enable;
1282
if (!is_inside_tree()) {
1283
return;
1284
}
1285
1286
if (p_enable) {
1287
add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
1288
} else {
1289
remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
1290
}
1291
}
1292
1293
bool Node::is_processing_unhandled_input() const {
1294
return data.unhandled_input;
1295
}
1296
1297
void Node::set_process_unhandled_key_input(bool p_enable) {
1298
ERR_THREAD_GUARD
1299
if (p_enable == data.unhandled_key_input) {
1300
return;
1301
}
1302
data.unhandled_key_input = p_enable;
1303
if (!is_inside_tree()) {
1304
return;
1305
}
1306
1307
if (p_enable) {
1308
add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
1309
} else {
1310
remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
1311
}
1312
}
1313
1314
bool Node::is_processing_unhandled_key_input() const {
1315
return data.unhandled_key_input;
1316
}
1317
1318
void Node::set_auto_translate_mode(AutoTranslateMode p_mode) {
1319
ERR_THREAD_GUARD
1320
if (data.auto_translate_mode == p_mode) {
1321
return;
1322
}
1323
1324
if (p_mode == AUTO_TRANSLATE_MODE_INHERIT && data.tree && !data.parent) {
1325
ERR_FAIL_MSG("The root node can't be set to Inherit auto translate mode.");
1326
}
1327
1328
data.auto_translate_mode = p_mode;
1329
data.is_auto_translating = p_mode != AUTO_TRANSLATE_MODE_DISABLED;
1330
data.is_auto_translate_dirty = true;
1331
1332
propagate_notification(NOTIFICATION_TRANSLATION_CHANGED);
1333
}
1334
1335
Node::AutoTranslateMode Node::get_auto_translate_mode() const {
1336
return data.auto_translate_mode;
1337
}
1338
1339
bool Node::can_auto_translate() const {
1340
ERR_READ_THREAD_GUARD_V(false);
1341
if (!data.is_auto_translate_dirty || data.auto_translate_mode != AUTO_TRANSLATE_MODE_INHERIT) {
1342
return data.is_auto_translating;
1343
}
1344
1345
data.is_auto_translate_dirty = false;
1346
1347
Node *parent = data.parent;
1348
while (parent) {
1349
if (parent->data.auto_translate_mode == AUTO_TRANSLATE_MODE_INHERIT) {
1350
parent = parent->data.parent;
1351
continue;
1352
}
1353
1354
data.is_auto_translating = parent->data.auto_translate_mode == AUTO_TRANSLATE_MODE_ALWAYS;
1355
break;
1356
}
1357
1358
return data.is_auto_translating;
1359
}
1360
1361
StringName Node::get_translation_domain() const {
1362
ERR_READ_THREAD_GUARD_V(StringName());
1363
1364
if (data.is_translation_domain_inherited && data.is_translation_domain_dirty) {
1365
const_cast<Node *>(this)->_translation_domain = data.parent ? data.parent->get_translation_domain() : StringName();
1366
data.is_translation_domain_dirty = false;
1367
}
1368
return _translation_domain;
1369
}
1370
1371
void Node::set_translation_domain(const StringName &p_domain) {
1372
ERR_THREAD_GUARD
1373
1374
if (!data.is_translation_domain_inherited && _translation_domain == p_domain) {
1375
return;
1376
}
1377
1378
_translation_domain = p_domain;
1379
data.is_translation_domain_inherited = false;
1380
data.is_translation_domain_dirty = false;
1381
_propagate_translation_domain_dirty();
1382
}
1383
1384
void Node::set_translation_domain_inherited() {
1385
ERR_THREAD_GUARD
1386
1387
if (data.is_translation_domain_inherited) {
1388
return;
1389
}
1390
data.is_translation_domain_inherited = true;
1391
data.is_translation_domain_dirty = true;
1392
_propagate_translation_domain_dirty();
1393
}
1394
1395
void Node::_propagate_translation_domain_dirty() {
1396
for (KeyValue<StringName, Node *> &K : data.children) {
1397
Node *child = K.value;
1398
if (child->data.is_translation_domain_inherited) {
1399
child->data.is_translation_domain_dirty = true;
1400
child->_propagate_translation_domain_dirty();
1401
}
1402
}
1403
1404
if (is_inside_tree() && data.auto_translate_mode != AUTO_TRANSLATE_MODE_DISABLED) {
1405
notification(NOTIFICATION_TRANSLATION_CHANGED);
1406
}
1407
}
1408
1409
StringName Node::get_name() const {
1410
return data.name;
1411
}
1412
1413
void Node::_set_name_nocheck(const StringName &p_name) {
1414
data.name = p_name;
1415
}
1416
1417
void Node::set_name(const StringName &p_name) {
1418
ERR_FAIL_COND_MSG(data.tree && !Thread::is_main_thread(), "Changing the name to nodes inside the SceneTree is only allowed from the main thread. Use `set_name.call_deferred(new_name)`.");
1419
ERR_FAIL_COND(p_name.is_empty());
1420
1421
const StringName old_name = data.name;
1422
if (data.unique_name_in_owner && data.owner) {
1423
_release_unique_name_in_owner();
1424
}
1425
1426
{
1427
const String input_name_str = String(p_name);
1428
const String validated_node_name_string = input_name_str.validate_node_name();
1429
if (input_name_str == validated_node_name_string) {
1430
data.name = p_name;
1431
} else {
1432
data.name = StringName(validated_node_name_string);
1433
}
1434
}
1435
1436
if (data.parent) {
1437
data.parent->_validate_child_name(this, true);
1438
bool success = data.parent->data.children.replace_key(old_name, data.name);
1439
ERR_FAIL_COND_MSG(!success, "Renaming child in hashtable failed, this is a bug.");
1440
}
1441
1442
if (data.unique_name_in_owner && data.owner) {
1443
_acquire_unique_name_in_owner();
1444
}
1445
1446
propagate_notification(NOTIFICATION_PATH_RENAMED);
1447
1448
if (is_inside_tree()) {
1449
emit_signal(SNAME("renamed"));
1450
data.tree->node_renamed(this);
1451
data.tree->tree_changed();
1452
}
1453
}
1454
1455
// Returns a clear description of this node depending on what is available. Useful for error messages.
1456
String Node::get_description() const {
1457
String description;
1458
if (is_inside_tree()) {
1459
description = String(get_path());
1460
} else {
1461
description = get_name();
1462
if (description.is_empty()) {
1463
description = get_class();
1464
}
1465
}
1466
return description;
1467
}
1468
1469
static SafeRefCount node_hrcr_count;
1470
1471
void Node::init_node_hrcr() {
1472
node_hrcr_count.init(1);
1473
}
1474
1475
#ifdef TOOLS_ENABLED
1476
String Node::validate_child_name(Node *p_child) {
1477
StringName name = p_child->data.name;
1478
_generate_serial_child_name(p_child, name);
1479
return name;
1480
}
1481
1482
String Node::prevalidate_child_name(Node *p_child, StringName p_name) {
1483
_generate_serial_child_name(p_child, p_name);
1484
return p_name;
1485
}
1486
#endif
1487
1488
String Node::adjust_name_casing(const String &p_name) {
1489
switch (GLOBAL_GET("editor/naming/node_name_casing").operator int()) {
1490
case NAME_CASING_PASCAL_CASE:
1491
return p_name.to_pascal_case();
1492
case NAME_CASING_CAMEL_CASE:
1493
return p_name.to_camel_case();
1494
case NAME_CASING_SNAKE_CASE:
1495
return p_name.to_snake_case();
1496
case NAME_CASING_KEBAB_CASE:
1497
return p_name.to_kebab_case();
1498
}
1499
return p_name;
1500
}
1501
1502
void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) {
1503
/* Make sure the name is unique */
1504
1505
if (p_force_human_readable) {
1506
//this approach to autoset node names is human readable but very slow
1507
1508
StringName name = p_child->data.name;
1509
_generate_serial_child_name(p_child, name);
1510
p_child->data.name = name;
1511
1512
} else {
1513
//this approach to autoset node names is fast but not as readable
1514
//it's the default and reserves the '@' character for unique names.
1515
1516
bool unique = true;
1517
1518
if (p_child->data.name == StringName()) {
1519
//new unique name must be assigned
1520
unique = false;
1521
} else {
1522
const Node *const *existing = data.children.getptr(p_child->data.name);
1523
unique = !existing || *existing == p_child;
1524
}
1525
1526
if (!unique) {
1527
ERR_FAIL_COND(!node_hrcr_count.ref());
1528
// Optimized version of the code below:
1529
// String name = "@" + String(p_child->get_name()) + "@" + itos(node_hrcr_count.get());
1530
uint32_t c = node_hrcr_count.get();
1531
String cn = p_child->get_class_name().operator String();
1532
const char32_t *cn_ptr = cn.ptr();
1533
uint32_t cn_length = cn.length();
1534
uint32_t c_chars = String::num_characters(c);
1535
uint32_t len = 2 + cn_length + c_chars;
1536
char32_t *str = (char32_t *)alloca(sizeof(char32_t) * (len + 1));
1537
uint32_t idx = 0;
1538
str[idx++] = '@';
1539
for (uint32_t i = 0; i < cn_length; i++) {
1540
str[idx++] = cn_ptr[i];
1541
}
1542
str[idx++] = '@';
1543
idx += c_chars;
1544
ERR_FAIL_COND(idx != len);
1545
str[idx] = 0;
1546
while (c) {
1547
str[--idx] = '0' + (c % 10);
1548
c /= 10;
1549
}
1550
p_child->data.name = String(str);
1551
}
1552
}
1553
}
1554
1555
// Return s + 1 as if it were an integer
1556
String increase_numeric_string(const String &s) {
1557
String res = s;
1558
bool carry = res.length() > 0;
1559
1560
for (int i = res.length() - 1; i >= 0; i--) {
1561
if (!carry) {
1562
break;
1563
}
1564
char32_t n = s[i];
1565
if (n == '9') { // keep carry as true: 9 + 1
1566
res[i] = '0';
1567
} else {
1568
res[i] = s[i] + 1;
1569
carry = false;
1570
}
1571
}
1572
1573
if (carry) {
1574
res = "1" + res;
1575
}
1576
1577
return res;
1578
}
1579
1580
void Node::_generate_serial_child_name(const Node *p_child, StringName &name) const {
1581
if (name == StringName()) {
1582
// No name and a new name is needed, create one.
1583
1584
name = p_child->get_class();
1585
}
1586
1587
const Node *const *existing = data.children.getptr(name);
1588
if (!existing || *existing == p_child) { // Unused, or is current node.
1589
return;
1590
}
1591
1592
// Extract trailing number
1593
String name_string = name;
1594
String nums;
1595
for (int i = name_string.length() - 1; i >= 0; i--) {
1596
char32_t n = name_string[i];
1597
if (is_digit(n)) {
1598
nums = String::chr(name_string[i]) + nums;
1599
} else {
1600
break;
1601
}
1602
}
1603
1604
String nnsep = _get_name_num_separator();
1605
int name_last_index = name_string.length() - nnsep.length() - nums.length();
1606
1607
// Assign the base name + separator to name if we have numbers preceded by a separator
1608
if (nums.length() > 0 && name_string.substr(name_last_index, nnsep.length()) == nnsep) {
1609
name_string = name_string.substr(0, name_last_index + nnsep.length());
1610
} else {
1611
nums = "";
1612
}
1613
1614
for (;;) {
1615
StringName attempt = name_string + nums;
1616
1617
existing = data.children.getptr(attempt);
1618
bool exists = existing != nullptr && *existing != p_child;
1619
1620
if (!exists) {
1621
name = attempt;
1622
return;
1623
} else {
1624
if (nums.length() == 0) {
1625
// Name was undecorated so skip to 2 for a more natural result
1626
nums = "2";
1627
name_string += nnsep; // Add separator because nums.length() > 0 was false
1628
} else {
1629
nums = increase_numeric_string(nums);
1630
}
1631
}
1632
}
1633
}
1634
1635
Node::InternalMode Node::get_internal_mode() const {
1636
return data.internal_mode;
1637
}
1638
1639
void Node::_add_child_nocheck(Node *p_child, const StringName &p_name, InternalMode p_internal_mode) {
1640
//add a child node quickly, without name validation
1641
1642
p_child->data.name = p_name;
1643
data.children.insert(p_name, p_child);
1644
1645
p_child->data.internal_mode = p_internal_mode;
1646
switch (p_internal_mode) {
1647
case INTERNAL_MODE_FRONT: {
1648
p_child->data.index = data.internal_children_front_count_cache++;
1649
} break;
1650
case INTERNAL_MODE_BACK: {
1651
p_child->data.index = data.internal_children_back_count_cache++;
1652
} break;
1653
case INTERNAL_MODE_DISABLED: {
1654
p_child->data.index = data.external_children_count_cache++;
1655
} break;
1656
}
1657
1658
p_child->data.parent = this;
1659
1660
if (!data.children_cache_dirty && p_internal_mode == INTERNAL_MODE_DISABLED && data.internal_children_back_count_cache == 0) {
1661
// Special case, also add to the cached children array since its cheap.
1662
data.children_cache.push_back(p_child);
1663
} else {
1664
data.children_cache_dirty = true;
1665
}
1666
1667
p_child->notification(NOTIFICATION_PARENTED);
1668
1669
if (data.tree) {
1670
p_child->_set_tree(data.tree);
1671
}
1672
1673
/* Notify */
1674
add_child_notify(p_child);
1675
notification(NOTIFICATION_CHILD_ORDER_CHANGED);
1676
emit_signal(SNAME("child_order_changed"));
1677
}
1678
1679
void Node::add_child(Node *p_child, bool p_force_readable_name, InternalMode p_internal) {
1680
ERR_FAIL_COND_MSG(data.tree && !Thread::is_main_thread(), "Adding children to a node inside the SceneTree is only allowed from the main thread. Use call_deferred(\"add_child\",node).");
1681
1682
ERR_THREAD_GUARD
1683
ERR_FAIL_NULL(p_child);
1684
ERR_FAIL_COND_MSG(p_child == this, vformat("Can't add child '%s' to itself.", p_child->get_name())); // adding to itself!
1685
ERR_FAIL_COND_MSG(p_child->data.parent, vformat("Can't add child '%s' to '%s', already has a parent '%s'.", p_child->get_name(), get_name(), p_child->data.parent->get_name())); //Fail if node has a parent
1686
#ifdef DEBUG_ENABLED
1687
ERR_FAIL_COND_MSG(p_child->is_ancestor_of(this), vformat("Can't add child '%s' to '%s' as it would result in a cyclic dependency since '%s' is already a parent of '%s'.", p_child->get_name(), get_name(), p_child->get_name(), get_name()));
1688
#endif
1689
ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, `add_child()` failed. Consider using `add_child.call_deferred(child)` instead.");
1690
1691
_validate_child_name(p_child, p_force_readable_name);
1692
1693
#ifdef DEBUG_ENABLED
1694
if (p_child->data.owner && !p_child->data.owner->is_ancestor_of(p_child)) {
1695
// Owner of p_child should be ancestor of p_child.
1696
WARN_PRINT(vformat("Adding '%s' as child to '%s' will make owner '%s' inconsistent. Consider unsetting the owner beforehand.", p_child->get_name(), get_name(), p_child->data.owner->get_name()));
1697
}
1698
#endif // DEBUG_ENABLED
1699
1700
_add_child_nocheck(p_child, p_child->data.name, p_internal);
1701
}
1702
1703
void Node::add_sibling(Node *p_sibling, bool p_force_readable_name) {
1704
ERR_FAIL_COND_MSG(data.tree && !Thread::is_main_thread(), "Adding a sibling to a node inside the SceneTree is only allowed from the main thread. Use call_deferred(\"add_sibling\",node).");
1705
ERR_FAIL_NULL(p_sibling);
1706
ERR_FAIL_COND_MSG(p_sibling == this, vformat("Can't add sibling '%s' to itself.", p_sibling->get_name())); // adding to itself!
1707
ERR_FAIL_NULL(data.parent);
1708
ERR_FAIL_COND_MSG(data.parent->data.blocked > 0, "Parent node is busy setting up children, `add_sibling()` failed. Consider using `add_sibling.call_deferred(sibling)` instead.");
1709
1710
data.parent->add_child(p_sibling, p_force_readable_name, data.internal_mode);
1711
data.parent->_update_children_cache();
1712
data.parent->_move_child(p_sibling, get_index() + 1);
1713
}
1714
1715
void Node::remove_child(Node *p_child) {
1716
ERR_FAIL_COND_MSG(data.tree && !Thread::is_main_thread(), "Removing children from a node inside the SceneTree is only allowed from the main thread. Use call_deferred(\"remove_child\",node).");
1717
ERR_FAIL_NULL(p_child);
1718
ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy adding/removing children, `remove_child()` can't be called at this time. Consider using `remove_child.call_deferred(child)` instead.");
1719
ERR_FAIL_COND(p_child->data.parent != this);
1720
1721
/**
1722
* Do not change the data.internal_children*cache counters here.
1723
* Because if nodes are re-added, the indices can remain
1724
* greater-than-everything indices and children added remain
1725
* properly ordered.
1726
*
1727
* All children indices and counters will be updated next time the
1728
* cache is re-generated.
1729
*/
1730
1731
data.blocked++;
1732
p_child->_set_tree(nullptr);
1733
1734
remove_child_notify(p_child);
1735
p_child->notification(NOTIFICATION_UNPARENTED);
1736
1737
data.blocked--;
1738
1739
data.children_cache_dirty = true;
1740
bool success = data.children.erase(p_child->data.name);
1741
ERR_FAIL_COND_MSG(!success, "Children name does not match parent name in hashtable, this is a bug.");
1742
1743
p_child->data.parent = nullptr;
1744
p_child->data.index = -1;
1745
1746
notification(NOTIFICATION_CHILD_ORDER_CHANGED);
1747
emit_signal(SNAME("child_order_changed"));
1748
1749
if (data.tree) {
1750
p_child->_propagate_after_exit_tree();
1751
}
1752
}
1753
1754
void Node::_update_children_cache_impl() const {
1755
// Assign children
1756
data.children_cache.resize(data.children.size());
1757
int idx = 0;
1758
for (const KeyValue<StringName, Node *> &K : data.children) {
1759
data.children_cache[idx] = K.value;
1760
idx++;
1761
}
1762
// Sort them
1763
data.children_cache.sort_custom<ComparatorByIndex>();
1764
// Update indices
1765
data.external_children_count_cache = 0;
1766
data.internal_children_back_count_cache = 0;
1767
data.internal_children_front_count_cache = 0;
1768
1769
for (uint32_t i = 0; i < data.children_cache.size(); i++) {
1770
switch (data.children_cache[i]->data.internal_mode) {
1771
case INTERNAL_MODE_DISABLED: {
1772
data.children_cache[i]->data.index = data.external_children_count_cache++;
1773
} break;
1774
case INTERNAL_MODE_FRONT: {
1775
data.children_cache[i]->data.index = data.internal_children_front_count_cache++;
1776
} break;
1777
case INTERNAL_MODE_BACK: {
1778
data.children_cache[i]->data.index = data.internal_children_back_count_cache++;
1779
} break;
1780
}
1781
}
1782
data.children_cache_dirty = false;
1783
}
1784
1785
int Node::get_child_count(bool p_include_internal) const {
1786
ERR_THREAD_GUARD_V(0);
1787
if (p_include_internal) {
1788
return data.children.size();
1789
}
1790
1791
_update_children_cache();
1792
return data.children_cache.size() - data.internal_children_front_count_cache - data.internal_children_back_count_cache;
1793
}
1794
1795
Node *Node::get_child(int p_index, bool p_include_internal) const {
1796
ERR_THREAD_GUARD_V(nullptr);
1797
_update_children_cache();
1798
1799
if (p_include_internal) {
1800
if (p_index < 0) {
1801
p_index += data.children_cache.size();
1802
}
1803
ERR_FAIL_INDEX_V(p_index, (int)data.children_cache.size(), nullptr);
1804
return data.children_cache[p_index];
1805
} else {
1806
if (p_index < 0) {
1807
p_index += (int)data.children_cache.size() - data.internal_children_front_count_cache - data.internal_children_back_count_cache;
1808
}
1809
ERR_FAIL_INDEX_V(p_index, (int)data.children_cache.size() - data.internal_children_front_count_cache - data.internal_children_back_count_cache, nullptr);
1810
p_index += data.internal_children_front_count_cache;
1811
return data.children_cache[p_index];
1812
}
1813
}
1814
1815
TypedArray<Node> Node::get_children(bool p_include_internal) const {
1816
ERR_THREAD_GUARD_V(TypedArray<Node>());
1817
TypedArray<Node> arr;
1818
int cc = get_child_count(p_include_internal);
1819
arr.resize(cc);
1820
for (int i = 0; i < cc; i++) {
1821
arr[i] = get_child(i, p_include_internal);
1822
}
1823
1824
return arr;
1825
}
1826
1827
Node *Node::_get_child_by_name(const StringName &p_name) const {
1828
const Node *const *node = data.children.getptr(p_name);
1829
if (node) {
1830
return const_cast<Node *>(*node);
1831
} else {
1832
return nullptr;
1833
}
1834
}
1835
1836
Node *Node::get_node_or_null(const NodePath &p_path) const {
1837
ERR_THREAD_GUARD_V(nullptr);
1838
if (p_path.is_empty()) {
1839
return nullptr;
1840
}
1841
1842
ERR_FAIL_COND_V_MSG(!data.tree && p_path.is_absolute(), nullptr, "Can't use get_node() with absolute paths from outside the active scene tree.");
1843
1844
Node *current = nullptr;
1845
Node *root = nullptr;
1846
1847
if (!p_path.is_absolute()) {
1848
current = const_cast<Node *>(this); //start from this
1849
} else {
1850
root = const_cast<Node *>(this);
1851
while (root->data.parent) {
1852
root = root->data.parent; //start from root
1853
}
1854
}
1855
1856
for (int i = 0; i < p_path.get_name_count(); i++) {
1857
StringName name = p_path.get_name(i);
1858
Node *next = nullptr;
1859
1860
if (name == SNAME(".")) {
1861
next = current;
1862
1863
} else if (name == SNAME("..")) {
1864
if (current == nullptr || !current->data.parent) {
1865
return nullptr;
1866
}
1867
1868
next = current->data.parent;
1869
} else if (current == nullptr) {
1870
if (name == root->get_name()) {
1871
next = root;
1872
}
1873
1874
} else if (name.is_node_unique_name()) {
1875
Node **unique = current->data.owned_unique_nodes.getptr(name);
1876
if (!unique && current->data.owner) {
1877
unique = current->data.owner->data.owned_unique_nodes.getptr(name);
1878
}
1879
if (!unique) {
1880
return nullptr;
1881
}
1882
next = *unique;
1883
} else {
1884
next = nullptr;
1885
const Node *const *node = current->data.children.getptr(name);
1886
if (node) {
1887
next = const_cast<Node *>(*node);
1888
} else {
1889
return nullptr;
1890
}
1891
}
1892
current = next;
1893
}
1894
1895
return current;
1896
}
1897
1898
Node *Node::get_node(const NodePath &p_path) const {
1899
Node *node = get_node_or_null(p_path);
1900
1901
if (unlikely(!node)) {
1902
const String desc = get_description();
1903
if (p_path.is_absolute()) {
1904
ERR_FAIL_V_MSG(nullptr,
1905
vformat(R"(Node not found: "%s" (absolute path attempted from "%s").)", p_path, desc));
1906
} else {
1907
ERR_FAIL_V_MSG(nullptr,
1908
vformat(R"(Node not found: "%s" (relative to "%s").)", p_path, desc));
1909
}
1910
}
1911
1912
return node;
1913
}
1914
1915
bool Node::has_node(const NodePath &p_path) const {
1916
return get_node_or_null(p_path) != nullptr;
1917
}
1918
1919
// Finds the first child node (in tree order) whose name matches the given pattern.
1920
// Can be recursive or not, and limited to owned nodes.
1921
Node *Node::find_child(const String &p_pattern, bool p_recursive, bool p_owned) const {
1922
ERR_THREAD_GUARD_V(nullptr);
1923
ERR_FAIL_COND_V(p_pattern.is_empty(), nullptr);
1924
_update_children_cache();
1925
Node *const *cptr = data.children_cache.ptr();
1926
int ccount = data.children_cache.size();
1927
for (int i = 0; i < ccount; i++) {
1928
if (p_owned && !cptr[i]->data.owner) {
1929
continue;
1930
}
1931
if (cptr[i]->data.name.operator String().match(p_pattern)) {
1932
return cptr[i];
1933
}
1934
1935
if (!p_recursive) {
1936
continue;
1937
}
1938
1939
Node *ret = cptr[i]->find_child(p_pattern, true, p_owned);
1940
if (ret) {
1941
return ret;
1942
}
1943
}
1944
return nullptr;
1945
}
1946
1947
// Finds child nodes based on their name using pattern matching, or class name,
1948
// or both (either pattern or type can be left empty).
1949
// Can be recursive or not, and limited to owned nodes.
1950
TypedArray<Node> Node::find_children(const String &p_pattern, const String &p_type, bool p_recursive, bool p_owned) const {
1951
ERR_THREAD_GUARD_V(TypedArray<Node>());
1952
TypedArray<Node> ret;
1953
ERR_FAIL_COND_V(p_pattern.is_empty() && p_type.is_empty(), ret);
1954
_update_children_cache();
1955
Node *const *cptr = data.children_cache.ptr();
1956
int ccount = data.children_cache.size();
1957
for (int i = 0; i < ccount; i++) {
1958
if (p_owned && !cptr[i]->data.owner) {
1959
continue;
1960
}
1961
1962
if (p_pattern.is_empty() || cptr[i]->data.name.operator String().match(p_pattern)) {
1963
if (p_type.is_empty() || cptr[i]->is_class(p_type)) {
1964
ret.append(cptr[i]);
1965
} else if (cptr[i]->get_script_instance()) {
1966
Ref<Script> scr = cptr[i]->get_script_instance()->get_script();
1967
while (scr.is_valid()) {
1968
if ((ScriptServer::is_global_class(p_type) && ScriptServer::get_global_class_path(p_type) == scr->get_path()) || p_type == scr->get_path()) {
1969
ret.append(cptr[i]);
1970
break;
1971
}
1972
1973
scr = scr->get_base_script();
1974
}
1975
}
1976
}
1977
1978
if (p_recursive) {
1979
ret.append_array(cptr[i]->find_children(p_pattern, p_type, true, p_owned));
1980
}
1981
}
1982
1983
return ret;
1984
}
1985
1986
void Node::reparent(Node *p_parent, bool p_keep_global_transform) {
1987
ERR_THREAD_GUARD
1988
ERR_FAIL_NULL(p_parent);
1989
ERR_FAIL_NULL_MSG(data.parent, "Node needs a parent to be reparented.");
1990
ERR_FAIL_COND_MSG(p_parent == this, vformat("Can't reparent '%s' to itself.", p_parent->get_name()));
1991
1992
if (p_parent == data.parent) {
1993
return;
1994
}
1995
1996
bool preserve_owner = data.owner && (data.owner == p_parent || data.owner->is_ancestor_of(p_parent));
1997
Node *owner_temp = data.owner;
1998
LocalVector<Node *> common_parents;
1999
2000
// If the new parent is related to the owner, find all children of the reparented node who have the same owner so that we can reassign them.
2001
if (preserve_owner) {
2002
LocalVector<Node *> to_visit;
2003
2004
to_visit.push_back(this);
2005
common_parents.push_back(this);
2006
2007
while (to_visit.size() > 0) {
2008
Node *check = to_visit[to_visit.size() - 1];
2009
to_visit.resize(to_visit.size() - 1);
2010
2011
for (int i = 0; i < check->get_child_count(false); i++) {
2012
Node *child = check->get_child(i, false);
2013
to_visit.push_back(child);
2014
if (child->data.owner == owner_temp) {
2015
common_parents.push_back(child);
2016
}
2017
}
2018
}
2019
}
2020
2021
data.parent->remove_child(this);
2022
p_parent->add_child(this);
2023
2024
// Reassign the old owner to those found nodes.
2025
if (preserve_owner) {
2026
for (Node *E : common_parents) {
2027
E->set_owner(owner_temp);
2028
}
2029
}
2030
}
2031
2032
Node *Node::get_parent() const {
2033
return data.parent;
2034
}
2035
2036
Node *Node::find_parent(const String &p_pattern) const {
2037
ERR_THREAD_GUARD_V(nullptr);
2038
Node *p = data.parent;
2039
while (p) {
2040
if (p->data.name.operator String().match(p_pattern)) {
2041
return p;
2042
}
2043
p = p->data.parent;
2044
}
2045
2046
return nullptr;
2047
}
2048
2049
Window *Node::get_window() const {
2050
ERR_THREAD_GUARD_V(nullptr);
2051
Viewport *vp = get_viewport();
2052
if (vp) {
2053
return vp->get_base_window();
2054
}
2055
return nullptr;
2056
}
2057
2058
Window *Node::get_non_popup_window() const {
2059
Window *w = get_window();
2060
while (w && w->is_popup()) {
2061
w = w->get_parent_visible_window();
2062
}
2063
return w;
2064
}
2065
2066
Window *Node::get_last_exclusive_window() const {
2067
Window *w = get_window();
2068
while (w && w->get_exclusive_child()) {
2069
w = w->get_exclusive_child();
2070
}
2071
2072
return w;
2073
}
2074
2075
bool Node::is_ancestor_of(const Node *p_node) const {
2076
ERR_FAIL_NULL_V(p_node, false);
2077
Node *p = p_node->data.parent;
2078
while (p) {
2079
if (p == this) {
2080
return true;
2081
}
2082
p = p->data.parent;
2083
}
2084
2085
return false;
2086
}
2087
2088
bool Node::is_greater_than(const Node *p_node) const {
2089
ERR_FAIL_NULL_V(p_node, false);
2090
ERR_FAIL_COND_V(!data.tree, false);
2091
ERR_FAIL_COND_V(!p_node->data.tree, false);
2092
2093
ERR_FAIL_COND_V(data.depth < 0, false);
2094
ERR_FAIL_COND_V(p_node->data.depth < 0, false);
2095
2096
_update_children_cache();
2097
2098
int *this_stack = (int *)alloca(sizeof(int) * data.depth);
2099
int *that_stack = (int *)alloca(sizeof(int) * p_node->data.depth);
2100
2101
const Node *n = this;
2102
2103
int idx = data.depth - 1;
2104
while (n) {
2105
ERR_FAIL_INDEX_V(idx, data.depth, false);
2106
this_stack[idx--] = n->get_index();
2107
n = n->data.parent;
2108
}
2109
2110
ERR_FAIL_COND_V(idx != -1, false);
2111
n = p_node;
2112
idx = p_node->data.depth - 1;
2113
while (n) {
2114
ERR_FAIL_INDEX_V(idx, p_node->data.depth, false);
2115
that_stack[idx--] = n->get_index();
2116
2117
n = n->data.parent;
2118
}
2119
ERR_FAIL_COND_V(idx != -1, false);
2120
idx = 0;
2121
2122
bool res;
2123
while (true) {
2124
// using -2 since out-of-tree or nonroot nodes have -1
2125
int this_idx = (idx >= data.depth) ? -2 : this_stack[idx];
2126
int that_idx = (idx >= p_node->data.depth) ? -2 : that_stack[idx];
2127
2128
if (this_idx > that_idx) {
2129
res = true;
2130
break;
2131
} else if (this_idx < that_idx) {
2132
res = false;
2133
break;
2134
} else if (this_idx == -2) {
2135
res = false; // equal
2136
break;
2137
}
2138
idx++;
2139
}
2140
2141
return res;
2142
}
2143
2144
void Node::get_owned_by(Node *p_by, List<Node *> *p_owned) {
2145
if (data.owner == p_by) {
2146
p_owned->push_back(this);
2147
}
2148
2149
for (KeyValue<StringName, Node *> &K : data.children) {
2150
K.value->get_owned_by(p_by, p_owned);
2151
}
2152
}
2153
2154
void Node::_set_owner_nocheck(Node *p_owner) {
2155
if (data.owner == p_owner) {
2156
return;
2157
}
2158
2159
ERR_FAIL_COND(data.owner);
2160
data.owner = p_owner;
2161
data.owner->data.owned.push_back(this);
2162
data.OW = data.owner->data.owned.back();
2163
2164
owner_changed_notify();
2165
}
2166
2167
void Node::_release_unique_name_in_owner() {
2168
ERR_FAIL_NULL(data.owner); // Safety check.
2169
StringName key = StringName(UNIQUE_NODE_PREFIX + data.name.operator String());
2170
Node **which = data.owner->data.owned_unique_nodes.getptr(key);
2171
if (which == nullptr || *which != this) {
2172
return; // Ignore.
2173
}
2174
data.owner->data.owned_unique_nodes.erase(key);
2175
}
2176
2177
void Node::_acquire_unique_name_in_owner() {
2178
ERR_FAIL_NULL(data.owner); // Safety check.
2179
StringName key = StringName(UNIQUE_NODE_PREFIX + data.name.operator String());
2180
Node **which = data.owner->data.owned_unique_nodes.getptr(key);
2181
if (which != nullptr && *which != this) {
2182
String which_path = String(is_inside_tree() ? (*which)->get_path() : data.owner->get_path_to(*which));
2183
WARN_PRINT(vformat("Setting node name '%s' to be unique within scene for '%s', but it's already claimed by '%s'.\n'%s' is no longer set as having a unique name.",
2184
get_name(), is_inside_tree() ? get_path() : data.owner->get_path_to(this), which_path, which_path));
2185
data.unique_name_in_owner = false;
2186
return;
2187
}
2188
data.owner->data.owned_unique_nodes[key] = this;
2189
}
2190
2191
void Node::set_unique_name_in_owner(bool p_enabled) {
2192
ERR_MAIN_THREAD_GUARD
2193
if (data.unique_name_in_owner == p_enabled) {
2194
return;
2195
}
2196
2197
if (data.unique_name_in_owner && data.owner != nullptr) {
2198
_release_unique_name_in_owner();
2199
}
2200
data.unique_name_in_owner = p_enabled;
2201
2202
if (data.unique_name_in_owner && data.owner != nullptr) {
2203
_acquire_unique_name_in_owner();
2204
}
2205
2206
update_configuration_warnings();
2207
_emit_editor_state_changed();
2208
}
2209
2210
bool Node::is_unique_name_in_owner() const {
2211
return data.unique_name_in_owner;
2212
}
2213
2214
void Node::set_owner(Node *p_owner) {
2215
ERR_MAIN_THREAD_GUARD
2216
if (data.owner) {
2217
_clean_up_owner();
2218
}
2219
2220
ERR_FAIL_COND(p_owner == this);
2221
2222
if (!p_owner) {
2223
return;
2224
}
2225
2226
bool owner_valid = p_owner->is_ancestor_of(this);
2227
2228
ERR_FAIL_COND_MSG(!owner_valid, "Invalid owner. Owner must be an ancestor in the tree.");
2229
2230
_set_owner_nocheck(p_owner);
2231
2232
if (data.unique_name_in_owner) {
2233
_acquire_unique_name_in_owner();
2234
}
2235
2236
_emit_editor_state_changed();
2237
}
2238
2239
Node *Node::get_owner() const {
2240
return data.owner;
2241
}
2242
2243
void Node::_clean_up_owner() {
2244
ERR_FAIL_NULL(data.owner); // Safety check.
2245
2246
if (data.unique_name_in_owner) {
2247
_release_unique_name_in_owner();
2248
}
2249
data.owner->data.owned.erase(data.OW);
2250
data.owner = nullptr;
2251
data.OW = nullptr;
2252
}
2253
2254
Node *Node::find_common_parent_with(const Node *p_node) const {
2255
if (this == p_node) {
2256
return const_cast<Node *>(p_node);
2257
}
2258
2259
HashSet<const Node *> visited;
2260
2261
const Node *n = this;
2262
2263
while (n) {
2264
visited.insert(n);
2265
n = n->data.parent;
2266
}
2267
2268
const Node *common_parent = p_node;
2269
2270
while (common_parent) {
2271
if (visited.has(common_parent)) {
2272
break;
2273
}
2274
common_parent = common_parent->data.parent;
2275
}
2276
2277
if (!common_parent) {
2278
return nullptr;
2279
}
2280
2281
return const_cast<Node *>(common_parent);
2282
}
2283
2284
NodePath Node::get_path_to(const Node *p_node, bool p_use_unique_path) const {
2285
ERR_FAIL_NULL_V(p_node, NodePath());
2286
2287
if (this == p_node) {
2288
return NodePath(".");
2289
}
2290
2291
HashSet<const Node *> visited;
2292
2293
const Node *n = this;
2294
2295
while (n) {
2296
visited.insert(n);
2297
n = n->data.parent;
2298
}
2299
2300
const Node *common_parent = p_node;
2301
2302
while (common_parent) {
2303
if (visited.has(common_parent)) {
2304
break;
2305
}
2306
common_parent = common_parent->data.parent;
2307
}
2308
2309
ERR_FAIL_NULL_V(common_parent, NodePath()); //nodes not in the same tree
2310
2311
visited.clear();
2312
2313
Vector<StringName> path;
2314
StringName up = String("..");
2315
2316
if (p_use_unique_path) {
2317
n = p_node;
2318
2319
bool is_detected = false;
2320
while (n != common_parent) {
2321
if (n->is_unique_name_in_owner() && n->get_owner() == get_owner()) {
2322
path.push_back(UNIQUE_NODE_PREFIX + String(n->get_name()));
2323
is_detected = true;
2324
break;
2325
}
2326
path.push_back(n->get_name());
2327
n = n->data.parent;
2328
}
2329
2330
if (!is_detected) {
2331
n = this;
2332
2333
String detected_name;
2334
int up_count = 0;
2335
while (n != common_parent) {
2336
if (n->is_unique_name_in_owner() && n->get_owner() == get_owner()) {
2337
detected_name = n->get_name();
2338
up_count = 0;
2339
}
2340
up_count++;
2341
n = n->data.parent;
2342
}
2343
2344
for (int i = 0; i < up_count; i++) {
2345
path.push_back(up);
2346
}
2347
2348
if (!detected_name.is_empty()) {
2349
path.push_back(UNIQUE_NODE_PREFIX + detected_name);
2350
}
2351
}
2352
} else {
2353
n = p_node;
2354
2355
while (n != common_parent) {
2356
path.push_back(n->get_name());
2357
n = n->data.parent;
2358
}
2359
2360
n = this;
2361
2362
while (n != common_parent) {
2363
path.push_back(up);
2364
n = n->data.parent;
2365
}
2366
}
2367
2368
path.reverse();
2369
2370
return NodePath(path, false);
2371
}
2372
2373
NodePath Node::get_path() const {
2374
ERR_FAIL_COND_V_MSG(!is_inside_tree(), NodePath(), "Cannot get path of node as it is not in a scene tree.");
2375
2376
if (data.path_cache) {
2377
return *data.path_cache;
2378
}
2379
2380
const Node *n = this;
2381
2382
Vector<StringName> path;
2383
2384
while (n) {
2385
path.push_back(n->get_name());
2386
n = n->data.parent;
2387
}
2388
2389
path.reverse();
2390
2391
data.path_cache = memnew(NodePath(path, true));
2392
2393
return *data.path_cache;
2394
}
2395
2396
bool Node::is_in_group(const StringName &p_identifier) const {
2397
ERR_THREAD_GUARD_V(false);
2398
return data.grouped.has(p_identifier);
2399
}
2400
2401
void Node::add_to_group(const StringName &p_identifier, bool p_persistent) {
2402
ERR_THREAD_GUARD
2403
ERR_FAIL_COND_MSG(p_identifier.is_empty(), vformat("Cannot add node '%s' to a group with an empty name.", get_name()));
2404
2405
if (data.grouped.has(p_identifier)) {
2406
return;
2407
}
2408
2409
GroupData gd;
2410
2411
if (data.tree) {
2412
gd.group = data.tree->add_to_group(p_identifier, this);
2413
} else {
2414
gd.group = nullptr;
2415
}
2416
2417
gd.persistent = p_persistent;
2418
2419
data.grouped[p_identifier] = gd;
2420
if (p_persistent) {
2421
_emit_editor_state_changed();
2422
}
2423
}
2424
2425
void Node::remove_from_group(const StringName &p_identifier) {
2426
ERR_THREAD_GUARD
2427
HashMap<StringName, GroupData>::Iterator E = data.grouped.find(p_identifier);
2428
2429
if (!E) {
2430
return;
2431
}
2432
2433
#ifdef TOOLS_ENABLED
2434
bool persistent = E->value.persistent;
2435
#endif
2436
2437
if (data.tree) {
2438
data.tree->remove_from_group(E->key, this);
2439
}
2440
2441
data.grouped.remove(E);
2442
2443
#ifdef TOOLS_ENABLED
2444
if (persistent) {
2445
_emit_editor_state_changed();
2446
}
2447
#endif
2448
}
2449
2450
TypedArray<StringName> Node::_get_groups() const {
2451
TypedArray<StringName> groups;
2452
List<GroupInfo> gi;
2453
get_groups(&gi);
2454
for (const GroupInfo &E : gi) {
2455
groups.push_back(E.name);
2456
}
2457
2458
return groups;
2459
}
2460
2461
void Node::get_groups(List<GroupInfo> *p_groups) const {
2462
ERR_THREAD_GUARD
2463
for (const KeyValue<StringName, GroupData> &E : data.grouped) {
2464
GroupInfo gi;
2465
gi.name = E.key;
2466
gi.persistent = E.value.persistent;
2467
p_groups->push_back(gi);
2468
}
2469
}
2470
2471
int Node::get_persistent_group_count() const {
2472
ERR_THREAD_GUARD_V(0);
2473
int count = 0;
2474
2475
for (const KeyValue<StringName, GroupData> &E : data.grouped) {
2476
if (E.value.persistent) {
2477
count += 1;
2478
}
2479
}
2480
2481
return count;
2482
}
2483
2484
void Node::print_tree_pretty() {
2485
print_line(_get_tree_string_pretty("", true));
2486
}
2487
2488
void Node::print_tree() {
2489
print_line(_get_tree_string(this));
2490
}
2491
2492
String Node::_get_tree_string_pretty(const String &p_prefix, bool p_last) {
2493
String new_prefix = p_last ? String::utf8(" â”–â•´") : String::utf8(" â” â•´");
2494
_update_children_cache();
2495
String return_tree = p_prefix + new_prefix + String(get_name()) + "\n";
2496
for (uint32_t i = 0; i < data.children_cache.size(); i++) {
2497
new_prefix = p_last ? String::utf8(" ") : String::utf8(" ┃ ");
2498
return_tree += data.children_cache[i]->_get_tree_string_pretty(p_prefix + new_prefix, i == data.children_cache.size() - 1);
2499
}
2500
return return_tree;
2501
}
2502
2503
String Node::get_tree_string_pretty() {
2504
return _get_tree_string_pretty("", true);
2505
}
2506
2507
String Node::_get_tree_string(const Node *p_node) {
2508
_update_children_cache();
2509
String return_tree = String(p_node->get_path_to(this)) + "\n";
2510
for (uint32_t i = 0; i < data.children_cache.size(); i++) {
2511
return_tree += data.children_cache[i]->_get_tree_string(p_node);
2512
}
2513
return return_tree;
2514
}
2515
2516
String Node::get_tree_string() {
2517
return _get_tree_string(this);
2518
}
2519
2520
void Node::_propagate_reverse_notification(int p_notification) {
2521
data.blocked++;
2522
2523
for (HashMap<StringName, Node *>::Iterator I = data.children.last(); I; --I) {
2524
I->value->_propagate_reverse_notification(p_notification);
2525
}
2526
2527
notification(p_notification, true);
2528
data.blocked--;
2529
}
2530
2531
void Node::_propagate_deferred_notification(int p_notification, bool p_reverse) {
2532
ERR_FAIL_COND(!is_inside_tree());
2533
2534
data.blocked++;
2535
2536
if (!p_reverse) {
2537
MessageQueue::get_singleton()->push_notification(this, p_notification);
2538
}
2539
2540
for (KeyValue<StringName, Node *> &K : data.children) {
2541
K.value->_propagate_deferred_notification(p_notification, p_reverse);
2542
}
2543
2544
if (p_reverse) {
2545
MessageQueue::get_singleton()->push_notification(this, p_notification);
2546
}
2547
2548
data.blocked--;
2549
}
2550
2551
void Node::propagate_notification(int p_notification) {
2552
ERR_THREAD_GUARD
2553
data.blocked++;
2554
notification(p_notification);
2555
2556
for (KeyValue<StringName, Node *> &K : data.children) {
2557
K.value->propagate_notification(p_notification);
2558
}
2559
data.blocked--;
2560
}
2561
2562
void Node::propagate_call(const StringName &p_method, const Array &p_args, const bool p_parent_first) {
2563
ERR_THREAD_GUARD
2564
data.blocked++;
2565
2566
if (p_parent_first && has_method(p_method)) {
2567
callv(p_method, p_args);
2568
}
2569
2570
for (KeyValue<StringName, Node *> &K : data.children) {
2571
K.value->propagate_call(p_method, p_args, p_parent_first);
2572
}
2573
2574
if (!p_parent_first && has_method(p_method)) {
2575
callv(p_method, p_args);
2576
}
2577
2578
data.blocked--;
2579
}
2580
2581
void Node::_propagate_replace_owner(Node *p_owner, Node *p_by_owner) {
2582
if (get_owner() == p_owner) {
2583
set_owner(p_by_owner);
2584
}
2585
2586
data.blocked++;
2587
for (KeyValue<StringName, Node *> &K : data.children) {
2588
K.value->_propagate_replace_owner(p_owner, p_by_owner);
2589
}
2590
data.blocked--;
2591
}
2592
2593
Ref<Tween> Node::create_tween() {
2594
ERR_THREAD_GUARD_V(Ref<Tween>());
2595
2596
SceneTree *tree = data.tree;
2597
if (!tree) {
2598
tree = SceneTree::get_singleton();
2599
}
2600
ERR_FAIL_NULL_V_MSG(tree, Ref<Tween>(), "No available SceneTree to create the Tween.");
2601
2602
Ref<Tween> tween = tree->create_tween();
2603
tween->bind_node(this);
2604
return tween;
2605
}
2606
2607
void Node::set_scene_file_path(const String &p_scene_file_path) {
2608
ERR_THREAD_GUARD
2609
data.scene_file_path = p_scene_file_path;
2610
_emit_editor_state_changed();
2611
}
2612
2613
String Node::get_scene_file_path() const {
2614
return data.scene_file_path;
2615
}
2616
2617
void Node::set_editor_description(const String &p_editor_description) {
2618
ERR_THREAD_GUARD
2619
if (data.editor_description == p_editor_description) {
2620
return;
2621
}
2622
2623
data.editor_description = p_editor_description;
2624
emit_signal(SNAME("editor_description_changed"), this);
2625
}
2626
2627
String Node::get_editor_description() const {
2628
return data.editor_description;
2629
}
2630
2631
void Node::set_editable_instance(Node *p_node, bool p_editable) {
2632
ERR_THREAD_GUARD
2633
ERR_FAIL_NULL(p_node);
2634
ERR_FAIL_COND(!is_ancestor_of(p_node));
2635
if (!p_editable) {
2636
p_node->data.editable_instance = false;
2637
// Avoid this flag being needlessly saved;
2638
// also give more visual feedback if editable children are re-enabled
2639
set_display_folded(false);
2640
} else {
2641
p_node->data.editable_instance = true;
2642
}
2643
2644
p_node->_emit_editor_state_changed();
2645
}
2646
2647
bool Node::is_editable_instance(const Node *p_node) const {
2648
if (!p_node) {
2649
return false; // Easier, null is never editable. :)
2650
}
2651
ERR_FAIL_COND_V(!is_ancestor_of(p_node), false);
2652
return p_node->data.editable_instance;
2653
}
2654
2655
Node *Node::get_deepest_editable_node(Node *p_start_node) const {
2656
ERR_THREAD_GUARD_V(nullptr);
2657
ERR_FAIL_NULL_V(p_start_node, nullptr);
2658
ERR_FAIL_COND_V(!is_ancestor_of(p_start_node), p_start_node);
2659
2660
Node const *iterated_item = p_start_node;
2661
Node *node = p_start_node;
2662
2663
while (iterated_item->get_owner() && iterated_item->get_owner() != this) {
2664
if (!is_editable_instance(iterated_item->get_owner())) {
2665
node = iterated_item->get_owner();
2666
}
2667
2668
iterated_item = iterated_item->get_owner();
2669
}
2670
2671
return node;
2672
}
2673
2674
#ifdef TOOLS_ENABLED
2675
void Node::set_property_pinned(const String &p_property, bool p_pinned) {
2676
ERR_THREAD_GUARD
2677
bool current_pinned = false;
2678
Array pinned = get_meta("_edit_pinned_properties_", Array());
2679
StringName psa = get_property_store_alias(p_property);
2680
current_pinned = pinned.has(psa);
2681
2682
if (current_pinned != p_pinned) {
2683
if (p_pinned) {
2684
pinned.append(psa);
2685
} else {
2686
pinned.erase(psa);
2687
}
2688
}
2689
2690
if (pinned.is_empty()) {
2691
remove_meta("_edit_pinned_properties_");
2692
} else {
2693
set_meta("_edit_pinned_properties_", pinned);
2694
}
2695
}
2696
2697
bool Node::is_property_pinned(const StringName &p_property) const {
2698
Array pinned = get_meta("_edit_pinned_properties_", Array());
2699
StringName psa = get_property_store_alias(p_property);
2700
return pinned.has(psa);
2701
}
2702
2703
StringName Node::get_property_store_alias(const StringName &p_property) const {
2704
return p_property;
2705
}
2706
2707
bool Node::is_part_of_edited_scene() const {
2708
return Engine::get_singleton()->is_editor_hint() && is_inside_tree() && data.tree->get_edited_scene_root() &&
2709
data.tree->get_edited_scene_root()->get_parent()->is_ancestor_of(this);
2710
}
2711
#endif
2712
2713
void Node::get_storable_properties(HashSet<StringName> &r_storable_properties) const {
2714
ERR_THREAD_GUARD
2715
List<PropertyInfo> property_list;
2716
get_property_list(&property_list);
2717
for (const PropertyInfo &pi : property_list) {
2718
if ((pi.usage & PROPERTY_USAGE_STORAGE)) {
2719
r_storable_properties.insert(pi.name);
2720
}
2721
}
2722
}
2723
2724
String Node::to_string() {
2725
// Keep this method in sync with `Object::to_string`.
2726
ERR_THREAD_GUARD_V(String());
2727
if (get_script_instance()) {
2728
bool valid;
2729
String ret = get_script_instance()->to_string(&valid);
2730
if (valid) {
2731
return ret;
2732
}
2733
}
2734
if (_get_extension() && _get_extension()->to_string) {
2735
String ret;
2736
GDExtensionBool is_valid;
2737
_get_extension()->to_string(_get_extension_instance(), &is_valid, &ret);
2738
if (is_valid) {
2739
return ret;
2740
}
2741
}
2742
return (get_name() ? String(get_name()) + ":" : "") + Object::to_string();
2743
}
2744
2745
void Node::set_scene_instance_state(const Ref<SceneState> &p_state) {
2746
ERR_THREAD_GUARD
2747
data.instance_state = p_state;
2748
}
2749
2750
Ref<SceneState> Node::get_scene_instance_state() const {
2751
return data.instance_state;
2752
}
2753
2754
void Node::set_scene_inherited_state(const Ref<SceneState> &p_state) {
2755
ERR_THREAD_GUARD
2756
data.inherited_state = p_state;
2757
_emit_editor_state_changed();
2758
}
2759
2760
Ref<SceneState> Node::get_scene_inherited_state() const {
2761
return data.inherited_state;
2762
}
2763
2764
void Node::set_scene_instance_load_placeholder(bool p_enable) {
2765
data.use_placeholder = p_enable;
2766
}
2767
2768
bool Node::get_scene_instance_load_placeholder() const {
2769
return data.use_placeholder;
2770
}
2771
2772
Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) const {
2773
ERR_THREAD_GUARD_V(nullptr);
2774
Node *node = nullptr;
2775
2776
bool instantiated = false;
2777
2778
if (Object::cast_to<InstancePlaceholder>(this)) {
2779
const InstancePlaceholder *ip = Object::cast_to<const InstancePlaceholder>(this);
2780
InstancePlaceholder *nip = memnew(InstancePlaceholder);
2781
nip->set_instance_path(ip->get_instance_path());
2782
node = nip;
2783
2784
} else if ((p_flags & DUPLICATE_USE_INSTANTIATION) && !get_scene_file_path().is_empty()) {
2785
Ref<PackedScene> res = ResourceLoader::load(get_scene_file_path());
2786
ERR_FAIL_COND_V(res.is_null(), nullptr);
2787
PackedScene::GenEditState edit_state = PackedScene::GEN_EDIT_STATE_DISABLED;
2788
#ifdef TOOLS_ENABLED
2789
if (p_flags & DUPLICATE_FROM_EDITOR) {
2790
edit_state = PackedScene::GEN_EDIT_STATE_INSTANCE;
2791
}
2792
#endif
2793
node = res->instantiate(edit_state);
2794
ERR_FAIL_NULL_V(node, nullptr);
2795
node->set_scene_instance_load_placeholder(get_scene_instance_load_placeholder());
2796
2797
instantiated = true;
2798
2799
} else {
2800
Object *obj = ClassDB::instantiate(get_class());
2801
ERR_FAIL_NULL_V(obj, nullptr);
2802
node = Object::cast_to<Node>(obj);
2803
if (!node) {
2804
memdelete(obj);
2805
}
2806
ERR_FAIL_NULL_V(node, nullptr);
2807
}
2808
2809
if (!get_scene_file_path().is_empty()) { //an instance
2810
node->set_scene_file_path(get_scene_file_path());
2811
node->data.editable_instance = data.editable_instance;
2812
}
2813
2814
List<const Node *> hidden_roots;
2815
List<const Node *> node_tree;
2816
node_tree.push_front(this);
2817
2818
if (instantiated) {
2819
// Since nodes in the instantiated hierarchy won't be duplicated explicitly, we need to make an inventory
2820
// of all the nodes in the tree of the instantiated scene in order to transfer the values of the properties
2821
2822
Vector<const Node *> instance_roots;
2823
instance_roots.push_back(this);
2824
2825
for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) {
2826
for (int i = 0; i < N->get()->get_child_count(false); ++i) {
2827
Node *descendant = N->get()->get_child(i, false);
2828
2829
// Skip nodes not really belonging to the instantiated hierarchy; they'll be processed normally later
2830
// but remember non-instantiated nodes that are hidden below instantiated ones
2831
if (!instance_roots.has(descendant->get_owner())) {
2832
if (descendant->get_parent() && descendant->get_parent() != this && descendant->data.owner != descendant->get_parent()) {
2833
hidden_roots.push_back(descendant);
2834
}
2835
continue;
2836
}
2837
2838
node_tree.push_back(descendant);
2839
2840
if (!descendant->get_scene_file_path().is_empty() && instance_roots.has(descendant->get_owner())) {
2841
instance_roots.push_back(descendant);
2842
}
2843
}
2844
}
2845
}
2846
2847
if (get_name() != String()) {
2848
node->set_name(get_name());
2849
}
2850
2851
#ifdef TOOLS_ENABLED
2852
if ((p_flags & DUPLICATE_FROM_EDITOR) && r_duplimap) {
2853
r_duplimap->insert(this, node);
2854
}
2855
#endif
2856
2857
if (p_flags & DUPLICATE_GROUPS) {
2858
List<GroupInfo> gi;
2859
get_groups(&gi);
2860
for (const GroupInfo &E : gi) {
2861
#ifdef TOOLS_ENABLED
2862
if ((p_flags & DUPLICATE_FROM_EDITOR) && !E.persistent) {
2863
continue;
2864
}
2865
#endif
2866
2867
node->add_to_group(E.name, E.persistent);
2868
}
2869
}
2870
2871
for (int i = 0; i < get_child_count(false); i++) {
2872
if (instantiated && get_child(i, false)->data.owner == this) {
2873
continue; //part of instance
2874
}
2875
2876
Node *dup = get_child(i, false)->_duplicate(p_flags, r_duplimap);
2877
if (!dup) {
2878
memdelete(node);
2879
return nullptr;
2880
}
2881
2882
node->add_child(dup);
2883
if (i < node->get_child_count(false) - 1) {
2884
node->move_child(dup, i);
2885
}
2886
}
2887
2888
for (const Node *&E : hidden_roots) {
2889
Node *parent = node->get_node(get_path_to(E->data.parent));
2890
if (!parent) {
2891
memdelete(node);
2892
return nullptr;
2893
}
2894
2895
Node *dup = E->_duplicate(p_flags, r_duplimap);
2896
if (!dup) {
2897
memdelete(node);
2898
return nullptr;
2899
}
2900
2901
parent->add_child(dup);
2902
int pos = E->get_index(false);
2903
2904
if (pos < parent->get_child_count(false) - 1) {
2905
parent->move_child(dup, pos);
2906
}
2907
}
2908
return node;
2909
}
2910
2911
Node *Node::duplicate(int p_flags) const {
2912
ERR_THREAD_GUARD_V(nullptr);
2913
Node *dupe = _duplicate(p_flags);
2914
2915
ERR_FAIL_NULL_V_MSG(dupe, nullptr, "Failed to duplicate node.");
2916
2917
_duplicate_properties(this, this, dupe, p_flags);
2918
2919
if (p_flags & DUPLICATE_SIGNALS) {
2920
_duplicate_signals(this, dupe);
2921
}
2922
2923
return dupe;
2924
}
2925
2926
#ifdef TOOLS_ENABLED
2927
Node *Node::duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap) const {
2928
return duplicate_from_editor(r_duplimap, HashMap<Ref<Resource>, Ref<Resource>>());
2929
}
2930
2931
Node *Node::duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const {
2932
int flags = DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS | DUPLICATE_USE_INSTANTIATION | DUPLICATE_FROM_EDITOR;
2933
Node *dupe = _duplicate(flags, &r_duplimap);
2934
2935
ERR_FAIL_NULL_V_MSG(dupe, nullptr, "Failed to duplicate node.");
2936
2937
_duplicate_properties(this, this, dupe, flags);
2938
2939
// This is used by SceneTreeDock's paste functionality. When pasting to foreign scene, resources are duplicated.
2940
if (!p_resource_remap.is_empty()) {
2941
remap_node_resources(dupe, p_resource_remap);
2942
}
2943
2944
// Duplication of signals must happen after all the node descendants have been copied,
2945
// because re-targeting of connections from some descendant to another is not possible
2946
// if the emitter node comes later in tree order than the receiver
2947
_duplicate_signals(this, dupe);
2948
2949
return dupe;
2950
}
2951
2952
void Node::remap_node_resources(Node *p_node, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const {
2953
List<PropertyInfo> props;
2954
p_node->get_property_list(&props);
2955
2956
for (const PropertyInfo &E : props) {
2957
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
2958
continue;
2959
}
2960
2961
Variant v = p_node->get(E.name);
2962
if (v.is_ref_counted()) {
2963
Ref<Resource> res = v;
2964
if (res.is_valid()) {
2965
if (p_resource_remap.has(res)) {
2966
p_node->set(E.name, p_resource_remap[res]);
2967
remap_nested_resources(res, p_resource_remap);
2968
}
2969
}
2970
}
2971
}
2972
2973
for (int i = 0; i < p_node->get_child_count(); i++) {
2974
remap_node_resources(p_node->get_child(i), p_resource_remap);
2975
}
2976
}
2977
2978
void Node::remap_nested_resources(Ref<Resource> p_resource, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const {
2979
List<PropertyInfo> props;
2980
p_resource->get_property_list(&props);
2981
2982
for (const PropertyInfo &E : props) {
2983
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
2984
continue;
2985
}
2986
2987
Variant v = p_resource->get(E.name);
2988
if (v.is_ref_counted()) {
2989
Ref<Resource> res = v;
2990
if (res.is_valid()) {
2991
if (p_resource_remap.has(res)) {
2992
p_resource->set(E.name, p_resource_remap[res]);
2993
remap_nested_resources(res, p_resource_remap);
2994
}
2995
}
2996
}
2997
}
2998
}
2999
3000
void Node::_emit_editor_state_changed() {
3001
// This is required for the SceneTreeEditor to properly keep track of when an update is needed.
3002
// This signal might be expensive and not needed for anything outside of the editor.
3003
if (Engine::get_singleton()->is_editor_hint()) {
3004
emit_signal(SNAME("editor_state_changed"));
3005
}
3006
}
3007
#endif
3008
3009
// Duplicate node's properties.
3010
// This has to be called after nodes have been duplicated since there might be properties
3011
// of type Node that can be updated properly only if duplicated node tree is complete.
3012
void Node::_duplicate_properties(const Node *p_root, const Node *p_original, Node *p_copy, int p_flags) const {
3013
List<PropertyInfo> props;
3014
p_original->get_property_list(&props);
3015
const StringName &script_property_name = CoreStringName(script);
3016
if (p_flags & DUPLICATE_SCRIPTS) {
3017
bool is_valid = false;
3018
Variant scr = p_original->get(script_property_name, &is_valid);
3019
if (is_valid) {
3020
p_copy->set(script_property_name, scr);
3021
}
3022
}
3023
for (const PropertyInfo &E : props) {
3024
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
3025
continue;
3026
}
3027
const StringName name = E.name;
3028
3029
if (name == script_property_name) {
3030
continue;
3031
}
3032
3033
Variant value = p_original->get(name);
3034
// To keep classic behavior, because, in contrast, nowadays a resource would be duplicated.
3035
if (value.get_type() != Variant::OBJECT) {
3036
value = value.duplicate(true);
3037
}
3038
3039
if (E.usage & PROPERTY_USAGE_ALWAYS_DUPLICATE) {
3040
Resource *res = Object::cast_to<Resource>(value);
3041
if (res) { // Duplicate only if it's a resource
3042
p_copy->set(name, res->duplicate());
3043
}
3044
} else {
3045
if (value.get_type() == Variant::OBJECT) {
3046
Node *property_node = Object::cast_to<Node>(value);
3047
Variant out_value = value;
3048
if (property_node && (p_root == property_node || p_root->is_ancestor_of(property_node))) {
3049
out_value = p_copy->get_node_or_null(p_original->get_path_to(property_node));
3050
}
3051
p_copy->set(name, out_value);
3052
} else if (value.get_type() == Variant::ARRAY) {
3053
Array arr = value;
3054
if (arr.get_typed_builtin() == Variant::OBJECT) {
3055
for (int i = 0; i < arr.size(); i++) {
3056
Node *property_node = Object::cast_to<Node>(arr[i]);
3057
if (property_node && (p_root == property_node || p_root->is_ancestor_of(property_node))) {
3058
arr[i] = p_copy->get_node_or_null(p_original->get_path_to(property_node));
3059
}
3060
}
3061
}
3062
p_copy->set(name, arr);
3063
} else {
3064
p_copy->set(name, value);
3065
}
3066
}
3067
}
3068
3069
for (int i = 0; i < p_original->get_child_count(false); i++) {
3070
Node *copy_child = p_copy->get_child(i, false);
3071
ERR_FAIL_NULL_MSG(copy_child, "Child node disappeared while duplicating.");
3072
_duplicate_properties(p_root, p_original->get_child(i, false), copy_child, p_flags);
3073
}
3074
}
3075
3076
// Duplication of signals must happen after all the node descendants have been copied,
3077
// because re-targeting of connections from some descendant to another is not possible
3078
// if the emitter node comes later in tree order than the receiver
3079
void Node::_duplicate_signals(const Node *p_original, Node *p_copy) const {
3080
if ((this != p_original) && !(p_original->is_ancestor_of(this))) {
3081
return;
3082
}
3083
3084
List<const Node *> process_list;
3085
process_list.push_back(this);
3086
while (!process_list.is_empty()) {
3087
const Node *n = process_list.front()->get();
3088
process_list.pop_front();
3089
3090
List<Connection> conns;
3091
n->get_all_signal_connections(&conns);
3092
3093
for (const Connection &E : conns) {
3094
if (E.flags & CONNECT_PERSIST) {
3095
//user connected
3096
NodePath p = p_original->get_path_to(n);
3097
Node *copy = p_copy->get_node(p);
3098
3099
Node *target = Object::cast_to<Node>(E.callable.get_object());
3100
if (!target) {
3101
continue;
3102
}
3103
3104
NodePath ptarget = p_original->get_path_to(target);
3105
if (ptarget.is_empty()) {
3106
continue;
3107
}
3108
3109
Node *copytarget = target;
3110
3111
// Attempt to find a path to the duplicate target, if it seems it's not part
3112
// of the duplicated and not yet parented hierarchy then at least try to connect
3113
// to the same target as the original
3114
3115
if (p_copy->has_node(ptarget)) {
3116
copytarget = p_copy->get_node(ptarget);
3117
}
3118
3119
if (copy && copytarget && E.callable.get_method() != StringName()) {
3120
Callable copy_callable = Callable(copytarget, E.callable.get_method());
3121
if (!copy->is_connected(E.signal.get_name(), copy_callable)) {
3122
int unbound_arg_count = E.callable.get_unbound_arguments_count();
3123
if (unbound_arg_count > 0) {
3124
copy_callable = copy_callable.unbind(unbound_arg_count);
3125
}
3126
if (E.callable.get_bound_arguments_count() > 0) {
3127
copy_callable = copy_callable.bindv(E.callable.get_bound_arguments());
3128
}
3129
copy->connect(E.signal.get_name(), copy_callable, E.flags);
3130
}
3131
}
3132
}
3133
}
3134
3135
for (int i = 0; i < n->get_child_count(); i++) {
3136
process_list.push_back(n->get_child(i));
3137
}
3138
}
3139
}
3140
3141
static void find_owned_by(Node *p_by, Node *p_node, List<Node *> *p_owned) {
3142
if (p_node->get_owner() == p_by) {
3143
p_owned->push_back(p_node);
3144
}
3145
3146
for (int i = 0; i < p_node->get_child_count(); i++) {
3147
find_owned_by(p_by, p_node->get_child(i), p_owned);
3148
}
3149
}
3150
3151
void Node::replace_by(Node *p_node, bool p_keep_groups) {
3152
ERR_THREAD_GUARD
3153
ERR_FAIL_NULL(p_node);
3154
ERR_FAIL_COND(p_node->data.parent);
3155
3156
List<Node *> owned = data.owned;
3157
List<Node *> owned_by_owner;
3158
Node *owner = (data.owner == this) ? p_node : data.owner;
3159
3160
if (p_keep_groups) {
3161
List<GroupInfo> groups;
3162
get_groups(&groups);
3163
3164
for (const GroupInfo &E : groups) {
3165
p_node->add_to_group(E.name, E.persistent);
3166
}
3167
}
3168
3169
_replace_connections_target(p_node);
3170
3171
if (data.owner) {
3172
for (int i = 0; i < get_child_count(); i++) {
3173
find_owned_by(data.owner, get_child(i), &owned_by_owner);
3174
}
3175
3176
_clean_up_owner();
3177
}
3178
3179
Node *parent = data.parent;
3180
int index_in_parent = get_index(false);
3181
3182
if (data.parent) {
3183
parent->remove_child(this);
3184
parent->add_child(p_node);
3185
parent->move_child(p_node, index_in_parent);
3186
}
3187
3188
emit_signal(SNAME("replacing_by"), p_node);
3189
3190
while (get_child_count()) {
3191
Node *child = get_child(0);
3192
remove_child(child);
3193
if (!child->is_internal()) {
3194
// Add the custom children to the p_node.
3195
Node *child_owner = child->get_owner() == this ? p_node : child->get_owner();
3196
child->set_owner(nullptr);
3197
p_node->add_child(child);
3198
child->set_owner(child_owner);
3199
}
3200
}
3201
3202
p_node->set_owner(owner);
3203
for (Node *E : owned) {
3204
if (E->data.owner != p_node) {
3205
E->set_owner(p_node);
3206
}
3207
}
3208
3209
for (Node *E : owned_by_owner) {
3210
if (E->data.owner != owner) {
3211
E->set_owner(owner);
3212
}
3213
}
3214
3215
p_node->set_scene_file_path(get_scene_file_path());
3216
}
3217
3218
void Node::_replace_connections_target(Node *p_new_target) {
3219
List<Connection> cl;
3220
get_signals_connected_to_this(&cl);
3221
3222
for (const Connection &c : cl) {
3223
if (c.flags & CONNECT_PERSIST) {
3224
c.signal.get_object()->disconnect(c.signal.get_name(), Callable(this, c.callable.get_method()));
3225
bool valid = p_new_target->has_method(c.callable.get_method()) || Ref<Script>(p_new_target->get_script()).is_null() || Ref<Script>(p_new_target->get_script())->has_method(c.callable.get_method());
3226
ERR_CONTINUE_MSG(!valid, vformat("Attempt to connect signal '%s.%s' to nonexistent method '%s.%s'.", c.signal.get_object()->get_class(), c.signal.get_name(), c.callable.get_object()->get_class(), c.callable.get_method()));
3227
c.signal.get_object()->connect(c.signal.get_name(), Callable(p_new_target, c.callable.get_method()), c.flags);
3228
}
3229
}
3230
}
3231
3232
bool Node::has_node_and_resource(const NodePath &p_path) const {
3233
ERR_THREAD_GUARD_V(false);
3234
if (!has_node(p_path)) {
3235
return false;
3236
}
3237
Ref<Resource> res;
3238
Vector<StringName> leftover_path;
3239
Node *node = get_node_and_resource(p_path, res, leftover_path, false);
3240
3241
return node;
3242
}
3243
3244
Array Node::_get_node_and_resource(const NodePath &p_path) {
3245
Ref<Resource> res;
3246
Vector<StringName> leftover_path;
3247
Node *node = get_node_and_resource(p_path, res, leftover_path, false);
3248
Array result;
3249
3250
if (node) {
3251
result.push_back(node);
3252
} else {
3253
result.push_back(Variant());
3254
}
3255
3256
if (res.is_valid()) {
3257
result.push_back(res);
3258
} else {
3259
result.push_back(Variant());
3260
}
3261
3262
result.push_back(NodePath(Vector<StringName>(), leftover_path, false));
3263
3264
return result;
3265
}
3266
3267
Node *Node::get_node_and_resource(const NodePath &p_path, Ref<Resource> &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property) const {
3268
ERR_THREAD_GUARD_V(nullptr);
3269
r_res = Ref<Resource>();
3270
r_leftover_subpath = Vector<StringName>();
3271
Node *node = get_node_or_null(p_path);
3272
if (!node) {
3273
return nullptr;
3274
}
3275
3276
if (p_path.get_subname_count()) {
3277
int j = 0;
3278
// If not p_last_is_property, we shouldn't consider the last one as part of the resource
3279
for (; j < p_path.get_subname_count() - (int)p_last_is_property; j++) {
3280
bool is_valid = false;
3281
Variant new_res_v = j == 0 ? node->get(p_path.get_subname(j), &is_valid) : r_res->get(p_path.get_subname(j), &is_valid);
3282
3283
if (!is_valid) { // Found nothing on that path
3284
return nullptr;
3285
}
3286
3287
Ref<Resource> new_res = new_res_v;
3288
3289
if (new_res.is_null()) { // No longer a resource, assume property
3290
break;
3291
}
3292
3293
r_res = new_res;
3294
}
3295
for (; j < p_path.get_subname_count(); j++) {
3296
// Put the rest of the subpath in the leftover path
3297
r_leftover_subpath.push_back(p_path.get_subname(j));
3298
}
3299
}
3300
3301
return node;
3302
}
3303
3304
void Node::_set_tree(SceneTree *p_tree) {
3305
SceneTree *tree_changed_a = nullptr;
3306
SceneTree *tree_changed_b = nullptr;
3307
3308
//ERR_FAIL_COND(p_scene && data.parent && !data.parent->data.scene); //nobug if both are null
3309
3310
if (data.tree) {
3311
_propagate_exit_tree();
3312
3313
tree_changed_a = data.tree;
3314
}
3315
3316
data.tree = p_tree;
3317
3318
if (data.tree) {
3319
_propagate_enter_tree();
3320
if (!data.parent || data.parent->data.ready_notified) { // No parent (root) or parent ready
3321
_propagate_ready(); //reverse_notification(NOTIFICATION_READY);
3322
}
3323
3324
tree_changed_b = data.tree;
3325
}
3326
3327
if (tree_changed_a) {
3328
tree_changed_a->tree_changed();
3329
}
3330
if (tree_changed_b) {
3331
tree_changed_b->tree_changed();
3332
}
3333
}
3334
3335
#ifdef DEBUG_ENABLED
3336
static HashMap<ObjectID, List<String>> _print_orphan_nodes_map;
3337
3338
static void _print_orphan_nodes_routine(Object *p_obj) {
3339
Node *n = Object::cast_to<Node>(p_obj);
3340
if (!n) {
3341
return;
3342
}
3343
3344
if (n->is_inside_tree()) {
3345
return;
3346
}
3347
3348
Node *p = n;
3349
while (p->get_parent()) {
3350
p = p->get_parent();
3351
}
3352
3353
String path;
3354
if (p == n) {
3355
path = n->get_name();
3356
} else {
3357
path = String(p->get_name()) + "/" + String(p->get_path_to(n));
3358
}
3359
3360
String source;
3361
Variant script = n->get_script();
3362
if (!script.is_null()) {
3363
Resource *obj = Object::cast_to<Resource>(script);
3364
source = obj->get_path();
3365
}
3366
3367
List<String> info_strings;
3368
info_strings.push_back(path);
3369
info_strings.push_back(n->get_class());
3370
info_strings.push_back(source);
3371
3372
_print_orphan_nodes_map[p_obj->get_instance_id()] = info_strings;
3373
}
3374
#endif // DEBUG_ENABLED
3375
3376
void Node::print_orphan_nodes() {
3377
#ifdef DEBUG_ENABLED
3378
// Make sure it's empty.
3379
_print_orphan_nodes_map.clear();
3380
3381
// Collect and print information about orphan nodes.
3382
ObjectDB::debug_objects(_print_orphan_nodes_routine);
3383
3384
for (const KeyValue<ObjectID, List<String>> &E : _print_orphan_nodes_map) {
3385
print_line(itos(E.key) + " - Stray Node: " + E.value.get(0) + " (Type: " + E.value.get(1) + ") (Source:" + E.value.get(2) + ")");
3386
}
3387
3388
// Flush it after use.
3389
_print_orphan_nodes_map.clear();
3390
#endif
3391
}
3392
TypedArray<int> Node::get_orphan_node_ids() {
3393
TypedArray<int> ret;
3394
#ifdef DEBUG_ENABLED
3395
// Make sure it's empty.
3396
_print_orphan_nodes_map.clear();
3397
3398
// Collect and return information about orphan nodes.
3399
ObjectDB::debug_objects(_print_orphan_nodes_routine);
3400
3401
for (const KeyValue<ObjectID, List<String>> &E : _print_orphan_nodes_map) {
3402
ret.push_back(E.key);
3403
}
3404
3405
// Flush it after use.
3406
_print_orphan_nodes_map.clear();
3407
#endif
3408
return ret;
3409
}
3410
3411
void Node::queue_free() {
3412
// There are users which instantiate multiple scene trees for their games.
3413
// Use the node's own tree to handle its deletion when relevant.
3414
if (data.tree) {
3415
data.tree->queue_delete(this);
3416
} else {
3417
SceneTree *tree = SceneTree::get_singleton();
3418
ERR_FAIL_NULL_MSG(tree, "Can't queue free a node when no SceneTree is available.");
3419
tree->queue_delete(this);
3420
}
3421
}
3422
3423
void Node::set_import_path(const NodePath &p_import_path) {
3424
#ifdef TOOLS_ENABLED
3425
data.import_path = p_import_path;
3426
#endif
3427
}
3428
3429
NodePath Node::get_import_path() const {
3430
#ifdef TOOLS_ENABLED
3431
return data.import_path;
3432
#else
3433
return NodePath();
3434
#endif
3435
}
3436
3437
#ifdef TOOLS_ENABLED
3438
static void _add_nodes_to_options(const Node *p_base, const Node *p_node, List<String> *r_options) {
3439
if (p_node != p_base && !p_node->get_owner()) {
3440
return;
3441
}
3442
if (p_node->is_unique_name_in_owner() && p_node->get_owner() == p_base) {
3443
String n = "%" + p_node->get_name();
3444
r_options->push_back(n.quote());
3445
}
3446
String n = String(p_base->get_path_to(p_node));
3447
r_options->push_back(n.quote());
3448
for (int i = 0; i < p_node->get_child_count(); i++) {
3449
_add_nodes_to_options(p_base, p_node->get_child(i), r_options);
3450
}
3451
}
3452
3453
void Node::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
3454
const String pf = p_function;
3455
if (p_idx == 0 && (pf == "has_node" || pf == "get_node" || pf == "get_node_or_null")) {
3456
_add_nodes_to_options(this, this, r_options);
3457
} else if (p_idx == 0 && (pf == "add_to_group" || pf == "remove_from_group" || pf == "is_in_group")) {
3458
HashMap<StringName, String> global_groups = ProjectSettings::get_singleton()->get_global_groups_list();
3459
for (const KeyValue<StringName, String> &E : global_groups) {
3460
r_options->push_back(E.key.operator String().quote());
3461
}
3462
}
3463
Object::get_argument_options(p_function, p_idx, r_options);
3464
}
3465
#endif
3466
3467
void Node::clear_internal_tree_resource_paths() {
3468
clear_internal_resource_paths();
3469
for (KeyValue<StringName, Node *> &K : data.children) {
3470
K.value->clear_internal_tree_resource_paths();
3471
}
3472
}
3473
3474
PackedStringArray Node::get_accessibility_configuration_warnings() const {
3475
ERR_THREAD_GUARD_V(PackedStringArray());
3476
PackedStringArray ret;
3477
3478
Vector<String> warnings;
3479
if (GDVIRTUAL_CALL(_get_accessibility_configuration_warnings, warnings)) {
3480
ret.append_array(warnings);
3481
}
3482
3483
return ret;
3484
}
3485
3486
PackedStringArray Node::get_configuration_warnings() const {
3487
ERR_THREAD_GUARD_V(PackedStringArray());
3488
PackedStringArray ret;
3489
3490
Vector<String> warnings;
3491
if (GDVIRTUAL_CALL(_get_configuration_warnings, warnings)) {
3492
ret.append_array(warnings);
3493
}
3494
3495
return ret;
3496
}
3497
3498
void Node::update_configuration_warnings() {
3499
ERR_THREAD_GUARD
3500
#ifdef TOOLS_ENABLED
3501
if (!data.tree) {
3502
return;
3503
}
3504
if (data.tree->get_edited_scene_root() && (data.tree->get_edited_scene_root() == this || data.tree->get_edited_scene_root()->is_ancestor_of(this))) {
3505
data.tree->emit_signal(SceneStringName(node_configuration_warning_changed), this);
3506
}
3507
#endif
3508
}
3509
3510
void Node::set_display_folded(bool p_folded) {
3511
ERR_THREAD_GUARD
3512
data.display_folded = p_folded;
3513
}
3514
3515
bool Node::is_displayed_folded() const {
3516
return data.display_folded;
3517
}
3518
3519
bool Node::is_ready() const {
3520
return !data.ready_first;
3521
}
3522
3523
void Node::request_ready() {
3524
ERR_THREAD_GUARD
3525
data.ready_first = true;
3526
}
3527
3528
void Node::_call_input(const Ref<InputEvent> &p_event) {
3529
if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
3530
GDVIRTUAL_CALL(_input, p_event);
3531
}
3532
if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
3533
return;
3534
}
3535
input(p_event);
3536
}
3537
3538
void Node::_call_shortcut_input(const Ref<InputEvent> &p_event) {
3539
if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
3540
GDVIRTUAL_CALL(_shortcut_input, p_event);
3541
}
3542
if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
3543
return;
3544
}
3545
shortcut_input(p_event);
3546
}
3547
3548
void Node::_call_unhandled_input(const Ref<InputEvent> &p_event) {
3549
if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
3550
GDVIRTUAL_CALL(_unhandled_input, p_event);
3551
}
3552
if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
3553
return;
3554
}
3555
unhandled_input(p_event);
3556
}
3557
3558
void Node::_call_unhandled_key_input(const Ref<InputEvent> &p_event) {
3559
if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
3560
GDVIRTUAL_CALL(_unhandled_key_input, p_event);
3561
}
3562
if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
3563
return;
3564
}
3565
unhandled_key_input(p_event);
3566
}
3567
3568
void Node::_validate_property(PropertyInfo &p_property) const {
3569
if ((p_property.name == "process_thread_group_order" || p_property.name == "process_thread_messages") && data.process_thread_group == PROCESS_THREAD_GROUP_INHERIT) {
3570
p_property.usage = 0;
3571
}
3572
}
3573
3574
void Node::input(const Ref<InputEvent> &p_event) {
3575
}
3576
3577
void Node::shortcut_input(const Ref<InputEvent> &p_key_event) {
3578
}
3579
3580
void Node::unhandled_input(const Ref<InputEvent> &p_event) {
3581
}
3582
3583
void Node::unhandled_key_input(const Ref<InputEvent> &p_key_event) {
3584
}
3585
3586
Variant Node::_call_deferred_thread_group_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
3587
if (p_argcount < 1) {
3588
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
3589
r_error.expected = 1;
3590
return Variant();
3591
}
3592
3593
if (!p_args[0]->is_string()) {
3594
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
3595
r_error.argument = 0;
3596
r_error.expected = Variant::STRING_NAME;
3597
return Variant();
3598
}
3599
3600
r_error.error = Callable::CallError::CALL_OK;
3601
3602
StringName method = *p_args[0];
3603
3604
call_deferred_thread_groupp(method, &p_args[1], p_argcount - 1, true);
3605
3606
return Variant();
3607
}
3608
3609
Variant Node::_call_thread_safe_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
3610
if (p_argcount < 1) {
3611
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
3612
r_error.expected = 1;
3613
return Variant();
3614
}
3615
3616
if (!p_args[0]->is_string()) {
3617
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
3618
r_error.argument = 0;
3619
r_error.expected = Variant::STRING_NAME;
3620
return Variant();
3621
}
3622
3623
r_error.error = Callable::CallError::CALL_OK;
3624
3625
StringName method = *p_args[0];
3626
3627
call_thread_safep(method, &p_args[1], p_argcount - 1, true);
3628
3629
return Variant();
3630
}
3631
3632
void Node::call_deferred_thread_groupp(const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) {
3633
ERR_FAIL_COND(!is_inside_tree());
3634
SceneTree::ProcessGroup *pg = (SceneTree::ProcessGroup *)data.process_group;
3635
pg->call_queue.push_callp(this, p_method, p_args, p_argcount, p_show_error);
3636
}
3637
3638
void Node::set_deferred_thread_group(const StringName &p_property, const Variant &p_value) {
3639
ERR_FAIL_COND(!is_inside_tree());
3640
SceneTree::ProcessGroup *pg = (SceneTree::ProcessGroup *)data.process_group;
3641
pg->call_queue.push_set(this, p_property, p_value);
3642
}
3643
3644
void Node::notify_deferred_thread_group(int p_notification) {
3645
ERR_FAIL_COND(!is_inside_tree());
3646
SceneTree::ProcessGroup *pg = (SceneTree::ProcessGroup *)data.process_group;
3647
pg->call_queue.push_notification(this, p_notification);
3648
}
3649
3650
void Node::call_thread_safep(const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) {
3651
if (is_accessible_from_caller_thread()) {
3652
Callable::CallError ce;
3653
callp(p_method, p_args, p_argcount, ce);
3654
if (p_show_error && ce.error != Callable::CallError::CALL_OK) {
3655
ERR_FAIL_MSG("Error calling method from 'call_threadp': " + Variant::get_call_error_text(this, p_method, p_args, p_argcount, ce) + ".");
3656
}
3657
} else {
3658
call_deferred_thread_groupp(p_method, p_args, p_argcount, p_show_error);
3659
}
3660
}
3661
3662
void Node::set_thread_safe(const StringName &p_property, const Variant &p_value) {
3663
if (is_accessible_from_caller_thread()) {
3664
set(p_property, p_value);
3665
} else {
3666
set_deferred_thread_group(p_property, p_value);
3667
}
3668
}
3669
3670
void Node::notify_thread_safe(int p_notification) {
3671
if (is_accessible_from_caller_thread()) {
3672
notification(p_notification);
3673
} else {
3674
notify_deferred_thread_group(p_notification);
3675
}
3676
}
3677
3678
RID Node::get_focused_accessibility_element() const {
3679
RID id;
3680
if (GDVIRTUAL_CALL(_get_focused_accessibility_element, id)) {
3681
return id;
3682
} else {
3683
return get_accessibility_element();
3684
}
3685
}
3686
3687
void Node::queue_accessibility_update() {
3688
if (is_inside_tree() && !is_part_of_edited_scene()) {
3689
data.tree->_accessibility_notify_change(this);
3690
}
3691
}
3692
3693
RID Node::get_accessibility_element() const {
3694
if (is_part_of_edited_scene()) {
3695
return RID();
3696
}
3697
if (unlikely(data.accessibility_element.is_null())) {
3698
Window *w = get_non_popup_window();
3699
if (w && w->get_window_id() != DisplayServer::INVALID_WINDOW_ID && get_window()->is_visible()) {
3700
data.accessibility_element = DisplayServer::get_singleton()->accessibility_create_element(w->get_window_id(), DisplayServer::ROLE_CONTAINER);
3701
}
3702
}
3703
return data.accessibility_element;
3704
}
3705
3706
void Node::_bind_methods() {
3707
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/naming/node_name_num_separator", PROPERTY_HINT_ENUM, "None,Space,Underscore,Dash"), 0);
3708
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/naming/node_name_casing", PROPERTY_HINT_ENUM, "PascalCase,camelCase,snake_case,kebab-case"), NAME_CASING_PASCAL_CASE);
3709
3710
ClassDB::bind_static_method("Node", D_METHOD("print_orphan_nodes"), &Node::print_orphan_nodes);
3711
ClassDB::bind_static_method("Node", D_METHOD("get_orphan_node_ids"), &Node::get_orphan_node_ids);
3712
ClassDB::bind_method(D_METHOD("add_sibling", "sibling", "force_readable_name"), &Node::add_sibling, DEFVAL(false));
3713
3714
ClassDB::bind_method(D_METHOD("set_name", "name"), &Node::set_name);
3715
ClassDB::bind_method(D_METHOD("get_name"), &Node::get_name);
3716
ClassDB::bind_method(D_METHOD("add_child", "node", "force_readable_name", "internal"), &Node::add_child, DEFVAL(false), DEFVAL(0));
3717
ClassDB::bind_method(D_METHOD("remove_child", "node"), &Node::remove_child);
3718
ClassDB::bind_method(D_METHOD("reparent", "new_parent", "keep_global_transform"), &Node::reparent, DEFVAL(true));
3719
ClassDB::bind_method(D_METHOD("get_child_count", "include_internal"), &Node::get_child_count, DEFVAL(false)); // Note that the default value bound for include_internal is false, while the method is declared with true. This is because internal nodes are irrelevant for GDSCript.
3720
ClassDB::bind_method(D_METHOD("get_children", "include_internal"), &Node::get_children, DEFVAL(false));
3721
ClassDB::bind_method(D_METHOD("get_child", "idx", "include_internal"), &Node::get_child, DEFVAL(false));
3722
ClassDB::bind_method(D_METHOD("has_node", "path"), &Node::has_node);
3723
ClassDB::bind_method(D_METHOD("get_node", "path"), &Node::get_node);
3724
ClassDB::bind_method(D_METHOD("get_node_or_null", "path"), &Node::get_node_or_null);
3725
ClassDB::bind_method(D_METHOD("get_parent"), &Node::get_parent);
3726
ClassDB::bind_method(D_METHOD("find_child", "pattern", "recursive", "owned"), &Node::find_child, DEFVAL(true), DEFVAL(true));
3727
ClassDB::bind_method(D_METHOD("find_children", "pattern", "type", "recursive", "owned"), &Node::find_children, DEFVAL(""), DEFVAL(true), DEFVAL(true));
3728
ClassDB::bind_method(D_METHOD("find_parent", "pattern"), &Node::find_parent);
3729
ClassDB::bind_method(D_METHOD("has_node_and_resource", "path"), &Node::has_node_and_resource);
3730
ClassDB::bind_method(D_METHOD("get_node_and_resource", "path"), &Node::_get_node_and_resource);
3731
3732
ClassDB::bind_method(D_METHOD("is_inside_tree"), &Node::is_inside_tree);
3733
ClassDB::bind_method(D_METHOD("is_part_of_edited_scene"), &Node::is_part_of_edited_scene);
3734
ClassDB::bind_method(D_METHOD("is_ancestor_of", "node"), &Node::is_ancestor_of);
3735
ClassDB::bind_method(D_METHOD("is_greater_than", "node"), &Node::is_greater_than);
3736
ClassDB::bind_method(D_METHOD("get_path"), &Node::get_path);
3737
ClassDB::bind_method(D_METHOD("get_path_to", "node", "use_unique_path"), &Node::get_path_to, DEFVAL(false));
3738
ClassDB::bind_method(D_METHOD("add_to_group", "group", "persistent"), &Node::add_to_group, DEFVAL(false));
3739
ClassDB::bind_method(D_METHOD("remove_from_group", "group"), &Node::remove_from_group);
3740
ClassDB::bind_method(D_METHOD("is_in_group", "group"), &Node::is_in_group);
3741
ClassDB::bind_method(D_METHOD("move_child", "child_node", "to_index"), &Node::move_child);
3742
ClassDB::bind_method(D_METHOD("get_groups"), &Node::_get_groups);
3743
ClassDB::bind_method(D_METHOD("set_owner", "owner"), &Node::set_owner);
3744
ClassDB::bind_method(D_METHOD("get_owner"), &Node::get_owner);
3745
ClassDB::bind_method(D_METHOD("get_index", "include_internal"), &Node::get_index, DEFVAL(false));
3746
ClassDB::bind_method(D_METHOD("print_tree"), &Node::print_tree);
3747
ClassDB::bind_method(D_METHOD("print_tree_pretty"), &Node::print_tree_pretty);
3748
ClassDB::bind_method(D_METHOD("get_tree_string"), &Node::get_tree_string);
3749
ClassDB::bind_method(D_METHOD("get_tree_string_pretty"), &Node::get_tree_string_pretty);
3750
ClassDB::bind_method(D_METHOD("set_scene_file_path", "scene_file_path"), &Node::set_scene_file_path);
3751
ClassDB::bind_method(D_METHOD("get_scene_file_path"), &Node::get_scene_file_path);
3752
ClassDB::bind_method(D_METHOD("propagate_notification", "what"), &Node::propagate_notification);
3753
ClassDB::bind_method(D_METHOD("propagate_call", "method", "args", "parent_first"), &Node::propagate_call, DEFVAL(Array()), DEFVAL(false));
3754
ClassDB::bind_method(D_METHOD("set_physics_process", "enable"), &Node::set_physics_process);
3755
ClassDB::bind_method(D_METHOD("get_physics_process_delta_time"), &Node::get_physics_process_delta_time);
3756
ClassDB::bind_method(D_METHOD("is_physics_processing"), &Node::is_physics_processing);
3757
ClassDB::bind_method(D_METHOD("get_process_delta_time"), &Node::get_process_delta_time);
3758
ClassDB::bind_method(D_METHOD("set_process", "enable"), &Node::set_process);
3759
ClassDB::bind_method(D_METHOD("set_process_priority", "priority"), &Node::set_process_priority);
3760
ClassDB::bind_method(D_METHOD("get_process_priority"), &Node::get_process_priority);
3761
ClassDB::bind_method(D_METHOD("set_physics_process_priority", "priority"), &Node::set_physics_process_priority);
3762
ClassDB::bind_method(D_METHOD("get_physics_process_priority"), &Node::get_physics_process_priority);
3763
ClassDB::bind_method(D_METHOD("is_processing"), &Node::is_processing);
3764
ClassDB::bind_method(D_METHOD("set_process_input", "enable"), &Node::set_process_input);
3765
ClassDB::bind_method(D_METHOD("is_processing_input"), &Node::is_processing_input);
3766
ClassDB::bind_method(D_METHOD("set_process_shortcut_input", "enable"), &Node::set_process_shortcut_input);
3767
ClassDB::bind_method(D_METHOD("is_processing_shortcut_input"), &Node::is_processing_shortcut_input);
3768
ClassDB::bind_method(D_METHOD("set_process_unhandled_input", "enable"), &Node::set_process_unhandled_input);
3769
ClassDB::bind_method(D_METHOD("is_processing_unhandled_input"), &Node::is_processing_unhandled_input);
3770
ClassDB::bind_method(D_METHOD("set_process_unhandled_key_input", "enable"), &Node::set_process_unhandled_key_input);
3771
ClassDB::bind_method(D_METHOD("is_processing_unhandled_key_input"), &Node::is_processing_unhandled_key_input);
3772
ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &Node::set_process_mode);
3773
ClassDB::bind_method(D_METHOD("get_process_mode"), &Node::get_process_mode);
3774
ClassDB::bind_method(D_METHOD("can_process"), &Node::can_process);
3775
3776
ClassDB::bind_method(D_METHOD("set_process_thread_group", "mode"), &Node::set_process_thread_group);
3777
ClassDB::bind_method(D_METHOD("get_process_thread_group"), &Node::get_process_thread_group);
3778
3779
ClassDB::bind_method(D_METHOD("set_process_thread_messages", "flags"), &Node::set_process_thread_messages);
3780
ClassDB::bind_method(D_METHOD("get_process_thread_messages"), &Node::get_process_thread_messages);
3781
3782
ClassDB::bind_method(D_METHOD("set_process_thread_group_order", "order"), &Node::set_process_thread_group_order);
3783
ClassDB::bind_method(D_METHOD("get_process_thread_group_order"), &Node::get_process_thread_group_order);
3784
3785
ClassDB::bind_method(D_METHOD("queue_accessibility_update"), &Node::queue_accessibility_update);
3786
ClassDB::bind_method(D_METHOD("get_accessibility_element"), &Node::get_accessibility_element);
3787
3788
ClassDB::bind_method(D_METHOD("set_display_folded", "fold"), &Node::set_display_folded);
3789
ClassDB::bind_method(D_METHOD("is_displayed_folded"), &Node::is_displayed_folded);
3790
3791
ClassDB::bind_method(D_METHOD("set_process_internal", "enable"), &Node::set_process_internal);
3792
ClassDB::bind_method(D_METHOD("is_processing_internal"), &Node::is_processing_internal);
3793
3794
ClassDB::bind_method(D_METHOD("set_physics_process_internal", "enable"), &Node::set_physics_process_internal);
3795
ClassDB::bind_method(D_METHOD("is_physics_processing_internal"), &Node::is_physics_processing_internal);
3796
3797
ClassDB::bind_method(D_METHOD("set_physics_interpolation_mode", "mode"), &Node::set_physics_interpolation_mode);
3798
ClassDB::bind_method(D_METHOD("get_physics_interpolation_mode"), &Node::get_physics_interpolation_mode);
3799
ClassDB::bind_method(D_METHOD("is_physics_interpolated"), &Node::is_physics_interpolated);
3800
ClassDB::bind_method(D_METHOD("is_physics_interpolated_and_enabled"), &Node::is_physics_interpolated_and_enabled);
3801
ClassDB::bind_method(D_METHOD("reset_physics_interpolation"), &Node::reset_physics_interpolation);
3802
3803
ClassDB::bind_method(D_METHOD("set_auto_translate_mode", "mode"), &Node::set_auto_translate_mode);
3804
ClassDB::bind_method(D_METHOD("get_auto_translate_mode"), &Node::get_auto_translate_mode);
3805
ClassDB::bind_method(D_METHOD("can_auto_translate"), &Node::can_auto_translate);
3806
ClassDB::bind_method(D_METHOD("set_translation_domain_inherited"), &Node::set_translation_domain_inherited);
3807
3808
ClassDB::bind_method(D_METHOD("get_window"), &Node::get_window);
3809
ClassDB::bind_method(D_METHOD("get_last_exclusive_window"), &Node::get_last_exclusive_window);
3810
ClassDB::bind_method(D_METHOD("get_tree"), &Node::get_tree);
3811
ClassDB::bind_method(D_METHOD("create_tween"), &Node::create_tween);
3812
3813
ClassDB::bind_method(D_METHOD("duplicate", "flags"), &Node::duplicate, DEFVAL(DUPLICATE_USE_INSTANTIATION | DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS));
3814
ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_groups"), &Node::replace_by, DEFVAL(false));
3815
3816
ClassDB::bind_method(D_METHOD("set_scene_instance_load_placeholder", "load_placeholder"), &Node::set_scene_instance_load_placeholder);
3817
ClassDB::bind_method(D_METHOD("get_scene_instance_load_placeholder"), &Node::get_scene_instance_load_placeholder);
3818
ClassDB::bind_method(D_METHOD("set_editable_instance", "node", "is_editable"), &Node::set_editable_instance);
3819
ClassDB::bind_method(D_METHOD("is_editable_instance", "node"), &Node::is_editable_instance);
3820
3821
ClassDB::bind_method(D_METHOD("get_viewport"), &Node::get_viewport);
3822
3823
ClassDB::bind_method(D_METHOD("queue_free"), &Node::queue_free);
3824
3825
ClassDB::bind_method(D_METHOD("request_ready"), &Node::request_ready);
3826
ClassDB::bind_method(D_METHOD("is_node_ready"), &Node::is_ready);
3827
3828
ClassDB::bind_method(D_METHOD("set_multiplayer_authority", "id", "recursive"), &Node::set_multiplayer_authority, DEFVAL(true));
3829
ClassDB::bind_method(D_METHOD("get_multiplayer_authority"), &Node::get_multiplayer_authority);
3830
3831
ClassDB::bind_method(D_METHOD("is_multiplayer_authority"), &Node::is_multiplayer_authority);
3832
3833
ClassDB::bind_method(D_METHOD("get_multiplayer"), &Node::get_multiplayer);
3834
ClassDB::bind_method(D_METHOD("rpc_config", "method", "config"), &Node::rpc_config);
3835
ClassDB::bind_method(D_METHOD("get_node_rpc_config"), &Node::_get_node_rpc_config_bind);
3836
3837
ClassDB::bind_method(D_METHOD("set_editor_description", "editor_description"), &Node::set_editor_description);
3838
ClassDB::bind_method(D_METHOD("get_editor_description"), &Node::get_editor_description);
3839
3840
ClassDB::bind_method(D_METHOD("_set_import_path", "import_path"), &Node::set_import_path);
3841
ClassDB::bind_method(D_METHOD("_get_import_path"), &Node::get_import_path);
3842
3843
ClassDB::bind_method(D_METHOD("set_unique_name_in_owner", "enable"), &Node::set_unique_name_in_owner);
3844
ClassDB::bind_method(D_METHOD("is_unique_name_in_owner"), &Node::is_unique_name_in_owner);
3845
3846
ClassDB::bind_method(D_METHOD("atr", "message", "context"), &Node::atr, DEFVAL(""));
3847
ClassDB::bind_method(D_METHOD("atr_n", "message", "plural_message", "n", "context"), &Node::atr_n, DEFVAL(""));
3848
3849
#ifdef TOOLS_ENABLED
3850
ClassDB::bind_method(D_METHOD("_set_property_pinned", "property", "pinned"), &Node::set_property_pinned);
3851
#endif
3852
3853
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "_import_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_import_path", "_get_import_path");
3854
3855
{
3856
MethodInfo mi;
3857
3858
mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
3859
3860
mi.name = "rpc";
3861
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc", &Node::_rpc_bind, mi);
3862
}
3863
3864
{
3865
MethodInfo mi;
3866
3867
mi.arguments.push_back(PropertyInfo(Variant::INT, "peer_id"));
3868
mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
3869
3870
mi.name = "rpc_id";
3871
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc_id", &Node::_rpc_id_bind, mi);
3872
}
3873
3874
ClassDB::bind_method(D_METHOD("update_configuration_warnings"), &Node::update_configuration_warnings);
3875
3876
{
3877
MethodInfo mi;
3878
mi.name = "call_deferred_thread_group";
3879
mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
3880
3881
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred_thread_group", &Node::_call_deferred_thread_group_bind, mi, varray(), false);
3882
}
3883
ClassDB::bind_method(D_METHOD("set_deferred_thread_group", "property", "value"), &Node::set_deferred_thread_group);
3884
ClassDB::bind_method(D_METHOD("notify_deferred_thread_group", "what"), &Node::notify_deferred_thread_group);
3885
3886
{
3887
MethodInfo mi;
3888
mi.name = "call_thread_safe";
3889
mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
3890
3891
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_thread_safe", &Node::_call_thread_safe_bind, mi, varray(), false);
3892
}
3893
ClassDB::bind_method(D_METHOD("set_thread_safe", "property", "value"), &Node::set_thread_safe);
3894
ClassDB::bind_method(D_METHOD("notify_thread_safe", "what"), &Node::notify_thread_safe);
3895
3896
BIND_CONSTANT(NOTIFICATION_ENTER_TREE);
3897
BIND_CONSTANT(NOTIFICATION_EXIT_TREE);
3898
BIND_CONSTANT(NOTIFICATION_MOVED_IN_PARENT);
3899
BIND_CONSTANT(NOTIFICATION_READY);
3900
BIND_CONSTANT(NOTIFICATION_PAUSED);
3901
BIND_CONSTANT(NOTIFICATION_UNPAUSED);
3902
BIND_CONSTANT(NOTIFICATION_PHYSICS_PROCESS);
3903
BIND_CONSTANT(NOTIFICATION_PROCESS);
3904
BIND_CONSTANT(NOTIFICATION_PARENTED);
3905
BIND_CONSTANT(NOTIFICATION_UNPARENTED);
3906
BIND_CONSTANT(NOTIFICATION_SCENE_INSTANTIATED);
3907
BIND_CONSTANT(NOTIFICATION_DRAG_BEGIN);
3908
BIND_CONSTANT(NOTIFICATION_DRAG_END);
3909
BIND_CONSTANT(NOTIFICATION_PATH_RENAMED);
3910
BIND_CONSTANT(NOTIFICATION_CHILD_ORDER_CHANGED);
3911
BIND_CONSTANT(NOTIFICATION_INTERNAL_PROCESS);
3912
BIND_CONSTANT(NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
3913
BIND_CONSTANT(NOTIFICATION_POST_ENTER_TREE);
3914
BIND_CONSTANT(NOTIFICATION_DISABLED);
3915
BIND_CONSTANT(NOTIFICATION_ENABLED);
3916
BIND_CONSTANT(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
3917
3918
BIND_CONSTANT(NOTIFICATION_EDITOR_PRE_SAVE);
3919
BIND_CONSTANT(NOTIFICATION_EDITOR_POST_SAVE);
3920
3921
BIND_CONSTANT(NOTIFICATION_WM_MOUSE_ENTER);
3922
BIND_CONSTANT(NOTIFICATION_WM_MOUSE_EXIT);
3923
BIND_CONSTANT(NOTIFICATION_WM_WINDOW_FOCUS_IN);
3924
BIND_CONSTANT(NOTIFICATION_WM_WINDOW_FOCUS_OUT);
3925
BIND_CONSTANT(NOTIFICATION_WM_CLOSE_REQUEST);
3926
BIND_CONSTANT(NOTIFICATION_WM_GO_BACK_REQUEST);
3927
BIND_CONSTANT(NOTIFICATION_WM_SIZE_CHANGED);
3928
BIND_CONSTANT(NOTIFICATION_WM_DPI_CHANGE);
3929
BIND_CONSTANT(NOTIFICATION_VP_MOUSE_ENTER);
3930
BIND_CONSTANT(NOTIFICATION_VP_MOUSE_EXIT);
3931
BIND_CONSTANT(NOTIFICATION_WM_POSITION_CHANGED);
3932
BIND_CONSTANT(NOTIFICATION_OS_MEMORY_WARNING);
3933
BIND_CONSTANT(NOTIFICATION_TRANSLATION_CHANGED);
3934
BIND_CONSTANT(NOTIFICATION_WM_ABOUT);
3935
BIND_CONSTANT(NOTIFICATION_CRASH);
3936
BIND_CONSTANT(NOTIFICATION_OS_IME_UPDATE);
3937
BIND_CONSTANT(NOTIFICATION_APPLICATION_RESUMED);
3938
BIND_CONSTANT(NOTIFICATION_APPLICATION_PAUSED);
3939
BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_IN);
3940
BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_OUT);
3941
BIND_CONSTANT(NOTIFICATION_TEXT_SERVER_CHANGED);
3942
3943
BIND_CONSTANT(NOTIFICATION_ACCESSIBILITY_UPDATE);
3944
BIND_CONSTANT(NOTIFICATION_ACCESSIBILITY_INVALIDATE);
3945
3946
BIND_ENUM_CONSTANT(PROCESS_MODE_INHERIT);
3947
BIND_ENUM_CONSTANT(PROCESS_MODE_PAUSABLE);
3948
BIND_ENUM_CONSTANT(PROCESS_MODE_WHEN_PAUSED);
3949
BIND_ENUM_CONSTANT(PROCESS_MODE_ALWAYS);
3950
BIND_ENUM_CONSTANT(PROCESS_MODE_DISABLED);
3951
3952
BIND_ENUM_CONSTANT(PROCESS_THREAD_GROUP_INHERIT);
3953
BIND_ENUM_CONSTANT(PROCESS_THREAD_GROUP_MAIN_THREAD);
3954
BIND_ENUM_CONSTANT(PROCESS_THREAD_GROUP_SUB_THREAD);
3955
3956
BIND_BITFIELD_FLAG(FLAG_PROCESS_THREAD_MESSAGES);
3957
BIND_BITFIELD_FLAG(FLAG_PROCESS_THREAD_MESSAGES_PHYSICS);
3958
BIND_BITFIELD_FLAG(FLAG_PROCESS_THREAD_MESSAGES_ALL);
3959
3960
BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_INHERIT);
3961
BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_ON);
3962
BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_OFF);
3963
3964
BIND_ENUM_CONSTANT(DUPLICATE_SIGNALS);
3965
BIND_ENUM_CONSTANT(DUPLICATE_GROUPS);
3966
BIND_ENUM_CONSTANT(DUPLICATE_SCRIPTS);
3967
BIND_ENUM_CONSTANT(DUPLICATE_USE_INSTANTIATION);
3968
3969
BIND_ENUM_CONSTANT(INTERNAL_MODE_DISABLED);
3970
BIND_ENUM_CONSTANT(INTERNAL_MODE_FRONT);
3971
BIND_ENUM_CONSTANT(INTERNAL_MODE_BACK);
3972
3973
BIND_ENUM_CONSTANT(AUTO_TRANSLATE_MODE_INHERIT);
3974
BIND_ENUM_CONSTANT(AUTO_TRANSLATE_MODE_ALWAYS);
3975
BIND_ENUM_CONSTANT(AUTO_TRANSLATE_MODE_DISABLED);
3976
3977
ADD_SIGNAL(MethodInfo("ready"));
3978
ADD_SIGNAL(MethodInfo("renamed"));
3979
ADD_SIGNAL(MethodInfo("tree_entered"));
3980
ADD_SIGNAL(MethodInfo("tree_exiting"));
3981
ADD_SIGNAL(MethodInfo("tree_exited"));
3982
ADD_SIGNAL(MethodInfo("child_entered_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
3983
ADD_SIGNAL(MethodInfo("child_exiting_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
3984
3985
ADD_SIGNAL(MethodInfo("child_order_changed"));
3986
ADD_SIGNAL(MethodInfo("replacing_by", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
3987
ADD_SIGNAL(MethodInfo("editor_description_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
3988
ADD_SIGNAL(MethodInfo("editor_state_changed"));
3989
3990
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_name", "get_name");
3991
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "unique_name_in_owner", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_unique_name_in_owner", "is_unique_name_in_owner");
3992
ADD_PROPERTY(PropertyInfo(Variant::STRING, "scene_file_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_scene_file_path", "get_scene_file_path");
3993
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_owner", "get_owner");
3994
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", PROPERTY_USAGE_NONE), "", "get_multiplayer");
3995
3996
ADD_GROUP("Process", "process_");
3997
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Inherit,Pausable,When Paused,Always,Disabled"), "set_process_mode", "get_process_mode");
3998
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_priority"), "set_process_priority", "get_process_priority");
3999
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_physics_priority"), "set_physics_process_priority", "get_physics_process_priority");
4000
4001
ADD_SUBGROUP("Thread Group", "process_thread");
4002
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread_group", PROPERTY_HINT_ENUM, "Inherit,Main Thread,Sub Thread"), "set_process_thread_group", "get_process_thread_group");
4003
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread_group_order"), "set_process_thread_group_order", "get_process_thread_group_order");
4004
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread_messages", PROPERTY_HINT_FLAGS, "Process,Physics Process"), "set_process_thread_messages", "get_process_thread_messages");
4005
4006
ADD_GROUP("Physics Interpolation", "physics_interpolation_");
4007
ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_interpolation_mode", PROPERTY_HINT_ENUM, "Inherit,On,Off"), "set_physics_interpolation_mode", "get_physics_interpolation_mode");
4008
4009
ADD_GROUP("Auto Translate", "auto_translate_");
4010
ADD_PROPERTY(PropertyInfo(Variant::INT, "auto_translate_mode", PROPERTY_HINT_ENUM, "Inherit,Always,Disabled"), "set_auto_translate_mode", "get_auto_translate_mode");
4011
4012
ADD_GROUP("Editor Description", "editor_");
4013
ADD_PROPERTY(PropertyInfo(Variant::STRING, "editor_description", PROPERTY_HINT_MULTILINE_TEXT), "set_editor_description", "get_editor_description");
4014
4015
GDVIRTUAL_BIND(_process, "delta");
4016
GDVIRTUAL_BIND(_physics_process, "delta");
4017
GDVIRTUAL_BIND(_enter_tree);
4018
GDVIRTUAL_BIND(_exit_tree);
4019
GDVIRTUAL_BIND(_ready);
4020
GDVIRTUAL_BIND(_get_configuration_warnings);
4021
GDVIRTUAL_BIND(_get_accessibility_configuration_warnings);
4022
GDVIRTUAL_BIND(_input, "event");
4023
GDVIRTUAL_BIND(_shortcut_input, "event");
4024
GDVIRTUAL_BIND(_unhandled_input, "event");
4025
GDVIRTUAL_BIND(_unhandled_key_input, "event");
4026
GDVIRTUAL_BIND(_get_focused_accessibility_element);
4027
}
4028
4029
String Node::_get_name_num_separator() {
4030
switch (GLOBAL_GET("editor/naming/node_name_num_separator").operator int()) {
4031
case 0:
4032
return "";
4033
case 1:
4034
return " ";
4035
case 2:
4036
return "_";
4037
case 3:
4038
return "-";
4039
}
4040
return " ";
4041
}
4042
4043
Node::Node() {
4044
orphan_node_count++;
4045
4046
// Default member initializer for bitfield is a C++20 extension, so:
4047
4048
data.process_mode = PROCESS_MODE_INHERIT;
4049
data.physics_interpolation_mode = PHYSICS_INTERPOLATION_MODE_INHERIT;
4050
4051
data.physics_process = false;
4052
data.process = false;
4053
4054
data.physics_process_internal = false;
4055
data.process_internal = false;
4056
4057
data.input = false;
4058
data.shortcut_input = false;
4059
data.unhandled_input = false;
4060
data.unhandled_key_input = false;
4061
4062
data.physics_interpolated = true;
4063
data.physics_interpolation_reset_requested = false;
4064
data.physics_interpolated_client_side = false;
4065
data.use_identity_transform = false;
4066
4067
data.use_placeholder = false;
4068
4069
data.display_folded = false;
4070
data.editable_instance = false;
4071
4072
data.ready_notified = false; // This is a small hack, so if a node is added during _ready() to the tree, it correctly gets the _ready() notification.
4073
data.ready_first = true;
4074
4075
data.auto_translate_mode = AUTO_TRANSLATE_MODE_INHERIT;
4076
data.is_auto_translating = true;
4077
data.is_auto_translate_dirty = true;
4078
4079
data.is_translation_domain_inherited = true;
4080
data.is_translation_domain_dirty = true;
4081
}
4082
4083
Node::~Node() {
4084
data.grouped.clear();
4085
data.owned.clear();
4086
data.children.clear();
4087
data.children_cache.clear();
4088
4089
ERR_FAIL_COND(data.parent);
4090
ERR_FAIL_COND(data.children_cache.size());
4091
4092
orphan_node_count--;
4093
}
4094
4095
////////////////////////////////
4096
// Multithreaded locked version of Object functions.
4097
4098
#ifdef DEBUG_ENABLED
4099
4100
void Node::set_script(const Variant &p_script) {
4101
ERR_THREAD_GUARD;
4102
Object::set_script(p_script);
4103
}
4104
4105
Variant Node::get_script() const {
4106
ERR_THREAD_GUARD_V(Variant());
4107
return Object::get_script();
4108
}
4109
4110
bool Node::has_meta(const StringName &p_name) const {
4111
ERR_THREAD_GUARD_V(false);
4112
return Object::has_meta(p_name);
4113
}
4114
4115
void Node::set_meta(const StringName &p_name, const Variant &p_value) {
4116
ERR_THREAD_GUARD;
4117
Object::set_meta(p_name, p_value);
4118
_emit_editor_state_changed();
4119
}
4120
4121
void Node::remove_meta(const StringName &p_name) {
4122
ERR_THREAD_GUARD;
4123
Object::remove_meta(p_name);
4124
_emit_editor_state_changed();
4125
}
4126
4127
Variant Node::get_meta(const StringName &p_name, const Variant &p_default) const {
4128
ERR_THREAD_GUARD_V(Variant());
4129
return Object::get_meta(p_name, p_default);
4130
}
4131
4132
void Node::get_meta_list(List<StringName> *p_list) const {
4133
ERR_THREAD_GUARD;
4134
Object::get_meta_list(p_list);
4135
}
4136
4137
Error Node::emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount) {
4138
ERR_THREAD_GUARD_V(ERR_INVALID_PARAMETER);
4139
return Object::emit_signalp(p_name, p_args, p_argcount);
4140
}
4141
4142
bool Node::has_signal(const StringName &p_name) const {
4143
ERR_THREAD_GUARD_V(false);
4144
return Object::has_signal(p_name);
4145
}
4146
4147
void Node::get_signal_list(List<MethodInfo> *p_signals) const {
4148
ERR_THREAD_GUARD;
4149
Object::get_signal_list(p_signals);
4150
}
4151
4152
void Node::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
4153
ERR_THREAD_GUARD;
4154
Object::get_signal_connection_list(p_signal, p_connections);
4155
}
4156
4157
void Node::get_all_signal_connections(List<Connection> *p_connections) const {
4158
ERR_THREAD_GUARD;
4159
Object::get_all_signal_connections(p_connections);
4160
}
4161
4162
int Node::get_persistent_signal_connection_count() const {
4163
ERR_THREAD_GUARD_V(0);
4164
return Object::get_persistent_signal_connection_count();
4165
}
4166
4167
void Node::get_signals_connected_to_this(List<Connection> *p_connections) const {
4168
ERR_THREAD_GUARD;
4169
Object::get_signals_connected_to_this(p_connections);
4170
}
4171
4172
Error Node::connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags) {
4173
ERR_THREAD_GUARD_V(ERR_INVALID_PARAMETER);
4174
4175
Error retval = Object::connect(p_signal, p_callable, p_flags);
4176
#ifdef TOOLS_ENABLED
4177
if (p_flags & CONNECT_PERSIST) {
4178
_emit_editor_state_changed();
4179
}
4180
#endif
4181
4182
return retval;
4183
}
4184
4185
void Node::disconnect(const StringName &p_signal, const Callable &p_callable) {
4186
ERR_THREAD_GUARD;
4187
4188
#ifdef TOOLS_ENABLED
4189
// Already under thread guard, don't check again.
4190
int old_connection_count = Object::get_persistent_signal_connection_count();
4191
#endif
4192
4193
Object::disconnect(p_signal, p_callable);
4194
4195
#ifdef TOOLS_ENABLED
4196
int new_connection_count = Object::get_persistent_signal_connection_count();
4197
if (old_connection_count != new_connection_count) {
4198
_emit_editor_state_changed();
4199
}
4200
#endif
4201
}
4202
4203
bool Node::is_connected(const StringName &p_signal, const Callable &p_callable) const {
4204
ERR_THREAD_GUARD_V(false);
4205
return Object::is_connected(p_signal, p_callable);
4206
}
4207
4208
bool Node::has_connections(const StringName &p_signal) const {
4209
ERR_THREAD_GUARD_V(false);
4210
return Object::has_connections(p_signal);
4211
}
4212
4213
#endif
4214
4215