-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
/
node.cpp
4095 lines (3334 loc) · 120 KB
/
node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* node.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "node.h"
#include "core/config/project_settings.h"
#include "core/io/resource_loader.h"
#include "core/object/message_queue.h"
#include "core/object/script_language.h"
#include "core/string/print_string.h"
#include "instance_placeholder.h"
#include "scene/animation/tween.h"
#include "scene/debugger/scene_debugger.h"
#include "scene/main/multiplayer_api.h"
#include "scene/main/window.h"
#include "scene/resources/packed_scene.h"
#include "viewport.h"
#include <stdint.h>
int Node::orphan_node_count = 0;
thread_local Node *Node::current_process_thread_group = nullptr;
void Node::_notification(int p_notification) {
switch (p_notification) {
case NOTIFICATION_PROCESS: {
GDVIRTUAL_CALL(_process, get_process_delta_time());
} break;
case NOTIFICATION_PHYSICS_PROCESS: {
GDVIRTUAL_CALL(_physics_process, get_physics_process_delta_time());
} break;
case NOTIFICATION_ENTER_TREE: {
ERR_FAIL_NULL(get_viewport());
ERR_FAIL_NULL(get_tree());
// Update process mode.
if (data.process_mode == PROCESS_MODE_INHERIT) {
if (data.parent) {
data.process_owner = data.parent->data.process_owner;
} else {
ERR_PRINT("The root node can't be set to Inherit process mode, reverting to Pausable instead.");
data.process_mode = PROCESS_MODE_PAUSABLE;
data.process_owner = this;
}
} else {
data.process_owner = this;
}
{ // Update threaded process mode.
if (data.process_thread_group == PROCESS_THREAD_GROUP_INHERIT) {
if (data.parent) {
data.process_thread_group_owner = data.parent->data.process_thread_group_owner;
}
if (data.process_thread_group_owner) {
data.process_group = data.process_thread_group_owner->data.process_group;
} else {
data.process_group = &data.tree->default_process_group;
}
} else {
data.process_thread_group_owner = this;
_add_process_group();
}
if (_is_any_processing()) {
_add_to_process_thread_group();
}
}
if (data.physics_interpolation_mode == PHYSICS_INTERPOLATION_MODE_INHERIT) {
bool interpolate = true; // Root node default is for interpolation to be on.
if (data.parent) {
interpolate = data.parent->is_physics_interpolated();
}
_propagate_physics_interpolated(interpolate);
}
// Update auto translate mode.
if (data.auto_translate_mode == AUTO_TRANSLATE_MODE_INHERIT && !data.parent) {
ERR_PRINT("The root node can't be set to Inherit auto translate mode, reverting to Always instead.");
data.auto_translate_mode = AUTO_TRANSLATE_MODE_ALWAYS;
}
data.is_auto_translate_dirty = true;
data.is_translation_domain_dirty = true;
#ifdef TOOLS_ENABLED
// Don't translate UI elements when they're being edited.
if (is_part_of_edited_scene()) {
set_message_translation(false);
}
#endif
if (data.input) {
add_to_group("_vp_input" + itos(get_viewport()->get_instance_id()));
}
if (data.shortcut_input) {
add_to_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
}
if (data.unhandled_input) {
add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
}
if (data.unhandled_key_input) {
add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
}
get_tree()->nodes_in_tree_count++;
orphan_node_count--;
// Allow physics interpolated nodes to automatically reset when added to the tree
// (this is to save the user from doing this manually each time).
if (get_tree()->is_physics_interpolation_enabled()) {
_set_physics_interpolation_reset_requested(true);
}
} break;
case NOTIFICATION_POST_ENTER_TREE: {
if (data.auto_translate_mode != AUTO_TRANSLATE_MODE_DISABLED) {
notification(NOTIFICATION_TRANSLATION_CHANGED);
}
} break;
case NOTIFICATION_EXIT_TREE: {
ERR_FAIL_NULL(get_viewport());
ERR_FAIL_NULL(get_tree());
get_tree()->nodes_in_tree_count--;
orphan_node_count++;
if (data.input) {
remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id()));
}
if (data.shortcut_input) {
remove_from_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
}
if (data.unhandled_input) {
remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
}
if (data.unhandled_key_input) {
remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
}
// Remove from processing first.
if (_is_any_processing()) {
_remove_from_process_thread_group();
}
// Remove the process group.
if (data.process_thread_group_owner == this) {
_remove_process_group();
}
data.process_thread_group_owner = nullptr;
data.process_owner = nullptr;
if (data.path_cache) {
memdelete(data.path_cache);
data.path_cache = nullptr;
}
} break;
case NOTIFICATION_SUSPENDED:
case NOTIFICATION_PAUSED: {
if (is_physics_interpolated_and_enabled() && is_inside_tree()) {
reset_physics_interpolation();
}
} break;
case NOTIFICATION_PATH_RENAMED: {
if (data.path_cache) {
memdelete(data.path_cache);
data.path_cache = nullptr;
}
} break;
case NOTIFICATION_READY: {
if (GDVIRTUAL_IS_OVERRIDDEN(_input)) {
set_process_input(true);
}
if (GDVIRTUAL_IS_OVERRIDDEN(_shortcut_input)) {
set_process_shortcut_input(true);
}
if (GDVIRTUAL_IS_OVERRIDDEN(_unhandled_input)) {
set_process_unhandled_input(true);
}
if (GDVIRTUAL_IS_OVERRIDDEN(_unhandled_key_input)) {
set_process_unhandled_key_input(true);
}
if (GDVIRTUAL_IS_OVERRIDDEN(_process)) {
set_process(true);
}
if (GDVIRTUAL_IS_OVERRIDDEN(_physics_process)) {
set_physics_process(true);
}
GDVIRTUAL_CALL(_ready);
} break;
case NOTIFICATION_POSTINITIALIZE: {
data.in_constructor = false;
} break;
case NOTIFICATION_PREDELETE: {
if (data.inside_tree && !Thread::is_main_thread()) {
cancel_free();
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.");
return;
}
if (data.owner) {
_clean_up_owner();
}
while (!data.owned.is_empty()) {
Node *n = data.owned.back()->get();
n->_clean_up_owner(); // This will change data.owned. So it's impossible to loop over the list in the usual manner.
}
if (data.parent) {
data.parent->remove_child(this);
}
// kill children as cleanly as possible
while (data.children.size()) {
Node *child = data.children.last()->value; // begin from the end because its faster and more consistent with creation
memdelete(child);
}
} break;
case NOTIFICATION_TRANSLATION_CHANGED: {
if (data.inside_tree) {
data.is_auto_translate_dirty = true;
}
} break;
}
}
void Node::_propagate_ready() {
data.ready_notified = true;
data.blocked++;
for (KeyValue<StringName, Node *> &K : data.children) {
K.value->_propagate_ready();
}
data.blocked--;
notification(NOTIFICATION_POST_ENTER_TREE);
if (data.ready_first) {
data.ready_first = false;
notification(NOTIFICATION_READY);
emit_signal(SceneStringName(ready));
}
}
void Node::_propagate_enter_tree() {
// this needs to happen to all children before any enter_tree
if (data.parent) {
data.tree = data.parent->data.tree;
data.depth = data.parent->data.depth + 1;
} else {
data.depth = 1;
}
data.viewport = Object::cast_to<Viewport>(this);
if (!data.viewport && data.parent) {
data.viewport = data.parent->data.viewport;
}
data.inside_tree = true;
for (KeyValue<StringName, GroupData> &E : data.grouped) {
E.value.group = data.tree->add_to_group(E.key, this);
}
notification(NOTIFICATION_ENTER_TREE);
GDVIRTUAL_CALL(_enter_tree);
emit_signal(SceneStringName(tree_entered));
data.tree->node_added(this);
if (data.parent) {
Variant c = this;
const Variant *cptr = &c;
data.parent->emit_signalp(SNAME("child_entered_tree"), &cptr, 1);
}
data.blocked++;
//block while adding children
for (KeyValue<StringName, Node *> &K : data.children) {
if (!K.value->is_inside_tree()) { // could have been added in enter_tree
K.value->_propagate_enter_tree();
}
}
data.blocked--;
#ifdef DEBUG_ENABLED
SceneDebugger::add_to_cache(data.scene_file_path, this);
#endif
// enter groups
}
void Node::_propagate_after_exit_tree() {
// Clear owner if it was not part of the pruned branch
if (data.owner) {
bool found = false;
Node *parent = data.parent;
while (parent) {
if (parent == data.owner) {
found = true;
break;
}
parent = parent->data.parent;
}
if (!found) {
_clean_up_owner();
}
}
data.blocked++;
for (HashMap<StringName, Node *>::Iterator I = data.children.last(); I; --I) {
I->value->_propagate_after_exit_tree();
}
data.blocked--;
emit_signal(SceneStringName(tree_exited));
}
void Node::_propagate_exit_tree() {
//block while removing children
#ifdef DEBUG_ENABLED
if (!data.scene_file_path.is_empty()) {
// Only remove if file path is set (optimization).
SceneDebugger::remove_from_cache(data.scene_file_path, this);
}
#endif
data.blocked++;
for (HashMap<StringName, Node *>::Iterator I = data.children.last(); I; --I) {
I->value->_propagate_exit_tree();
}
data.blocked--;
GDVIRTUAL_CALL(_exit_tree);
emit_signal(SceneStringName(tree_exiting));
notification(NOTIFICATION_EXIT_TREE, true);
if (data.tree) {
data.tree->node_removed(this);
}
if (data.parent) {
Variant c = this;
const Variant *cptr = &c;
data.parent->emit_signalp(SNAME("child_exiting_tree"), &cptr, 1);
}
// exit groups
for (KeyValue<StringName, GroupData> &E : data.grouped) {
data.tree->remove_from_group(E.key, this);
E.value.group = nullptr;
}
data.viewport = nullptr;
if (data.tree) {
data.tree->tree_changed();
}
data.inside_tree = false;
data.ready_notified = false;
data.tree = nullptr;
data.depth = -1;
}
void Node::_propagate_physics_interpolated(bool p_interpolated) {
switch (data.physics_interpolation_mode) {
case PHYSICS_INTERPOLATION_MODE_INHERIT:
// Keep the parent p_interpolated.
break;
case PHYSICS_INTERPOLATION_MODE_OFF: {
p_interpolated = false;
} break;
case PHYSICS_INTERPOLATION_MODE_ON: {
p_interpolated = true;
} break;
}
// No change? No need to propagate further.
if (data.physics_interpolated == p_interpolated) {
return;
}
data.physics_interpolated = p_interpolated;
// Allow a call to the RenderingServer etc. in derived classes.
_physics_interpolated_changed();
data.blocked++;
for (KeyValue<StringName, Node *> &K : data.children) {
K.value->_propagate_physics_interpolated(p_interpolated);
}
data.blocked--;
}
void Node::_propagate_physics_interpolation_reset_requested(bool p_requested) {
if (is_physics_interpolated()) {
data.physics_interpolation_reset_requested = p_requested;
}
data.blocked++;
for (KeyValue<StringName, Node *> &K : data.children) {
K.value->_propagate_physics_interpolation_reset_requested(p_requested);
}
data.blocked--;
}
void Node::move_child(Node *p_child, int p_index) {
ERR_FAIL_COND_MSG(data.inside_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).");
ERR_FAIL_NULL(p_child);
ERR_FAIL_COND_MSG(p_child->data.parent != this, "Child is not a child of this node.");
_update_children_cache();
// We need to check whether node is internal and move it only in the relevant node range.
if (p_child->data.internal_mode == INTERNAL_MODE_FRONT) {
if (p_index < 0) {
p_index += data.internal_children_front_count_cache;
}
ERR_FAIL_INDEX_MSG(p_index, data.internal_children_front_count_cache, vformat("Invalid new child index: %d. Child is internal.", p_index));
_move_child(p_child, p_index);
} else if (p_child->data.internal_mode == INTERNAL_MODE_BACK) {
if (p_index < 0) {
p_index += data.internal_children_back_count_cache;
}
ERR_FAIL_INDEX_MSG(p_index, data.internal_children_back_count_cache, vformat("Invalid new child index: %d. Child is internal.", p_index));
_move_child(p_child, (int)data.children_cache.size() - data.internal_children_back_count_cache + p_index);
} else {
if (p_index < 0) {
p_index += get_child_count(false);
}
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));
_move_child(p_child, p_index + data.internal_children_front_count_cache);
}
}
void Node::_move_child(Node *p_child, int p_index, bool p_ignore_end) {
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).");
// Specifying one place beyond the end
// means the same as moving to the last index
if (!p_ignore_end) { // p_ignore_end is a little hack to make back internal children work properly.
if (p_child->data.internal_mode == INTERNAL_MODE_FRONT) {
if (p_index == data.internal_children_front_count_cache) {
p_index--;
}
} else if (p_child->data.internal_mode == INTERNAL_MODE_BACK) {
if (p_index == (int)data.children_cache.size()) {
p_index--;
}
} else {
if (p_index == (int)data.children_cache.size() - data.internal_children_back_count_cache) {
p_index--;
}
}
}
int child_index = p_child->get_index();
if (child_index == p_index) {
return; //do nothing
}
int motion_from = MIN(p_index, child_index);
int motion_to = MAX(p_index, child_index);
data.children_cache.remove_at(child_index);
data.children_cache.insert(p_index, p_child);
if (data.tree) {
data.tree->tree_changed();
}
data.blocked++;
//new pos first
for (int i = motion_from; i <= motion_to; i++) {
if (data.children_cache[i]->data.internal_mode == INTERNAL_MODE_DISABLED) {
data.children_cache[i]->data.index = i - data.internal_children_front_count_cache;
} else if (data.children_cache[i]->data.internal_mode == INTERNAL_MODE_BACK) {
data.children_cache[i]->data.index = i - data.internal_children_front_count_cache - data.external_children_count_cache;
} else {
data.children_cache[i]->data.index = i;
}
}
// notification second
move_child_notify(p_child);
notification(NOTIFICATION_CHILD_ORDER_CHANGED);
emit_signal(SNAME("child_order_changed"));
p_child->_propagate_groups_dirty();
data.blocked--;
}
void Node::_propagate_groups_dirty() {
for (const KeyValue<StringName, GroupData> &E : data.grouped) {
if (E.value.group) {
E.value.group->changed = true;
}
}
for (KeyValue<StringName, Node *> &K : data.children) {
K.value->_propagate_groups_dirty();
}
}
void Node::add_child_notify(Node *p_child) {
// to be used when not wanted
}
void Node::remove_child_notify(Node *p_child) {
// to be used when not wanted
}
void Node::move_child_notify(Node *p_child) {
// to be used when not wanted
}
void Node::owner_changed_notify() {
}
void Node::_physics_interpolated_changed() {}
void Node::set_physics_process(bool p_process) {
ERR_THREAD_GUARD
if (data.physics_process == p_process) {
return;
}
if (!is_inside_tree()) {
data.physics_process = p_process;
return;
}
if (_is_any_processing()) {
_remove_from_process_thread_group();
}
data.physics_process = p_process;
if (_is_any_processing()) {
_add_to_process_thread_group();
}
}
bool Node::is_physics_processing() const {
return data.physics_process;
}
void Node::set_physics_process_internal(bool p_process_internal) {
ERR_THREAD_GUARD
if (data.physics_process_internal == p_process_internal) {
return;
}
if (!is_inside_tree()) {
data.physics_process_internal = p_process_internal;
return;
}
if (_is_any_processing()) {
_remove_from_process_thread_group();
}
data.physics_process_internal = p_process_internal;
if (_is_any_processing()) {
_add_to_process_thread_group();
}
}
bool Node::is_physics_processing_internal() const {
return data.physics_process_internal;
}
void Node::set_process_mode(ProcessMode p_mode) {
ERR_THREAD_GUARD
if (data.process_mode == p_mode) {
return;
}
if (!is_inside_tree()) {
data.process_mode = p_mode;
return;
}
bool prev_can_process = can_process();
bool prev_enabled = _is_enabled();
if (p_mode == PROCESS_MODE_INHERIT) {
if (data.parent) {
data.process_owner = data.parent->data.process_owner;
} else {
ERR_FAIL_MSG("The root node can't be set to Inherit process mode.");
}
} else {
data.process_owner = this;
}
data.process_mode = p_mode;
bool next_can_process = can_process();
bool next_enabled = _is_enabled();
int pause_notification = 0;
if (prev_can_process && !next_can_process) {
pause_notification = NOTIFICATION_PAUSED;
} else if (!prev_can_process && next_can_process) {
pause_notification = NOTIFICATION_UNPAUSED;
}
int enabled_notification = 0;
if (prev_enabled && !next_enabled) {
enabled_notification = NOTIFICATION_DISABLED;
} else if (!prev_enabled && next_enabled) {
enabled_notification = NOTIFICATION_ENABLED;
}
_propagate_process_owner(data.process_owner, pause_notification, enabled_notification);
#ifdef TOOLS_ENABLED
// This is required for the editor to update the visibility of disabled nodes
// It's very expensive during runtime to change, so editor-only
if (Engine::get_singleton()->is_editor_hint()) {
get_tree()->emit_signal(SNAME("tree_process_mode_changed"));
}
_emit_editor_state_changed();
#endif
}
void Node::_propagate_pause_notification(bool p_enable) {
bool prev_can_process = _can_process(!p_enable);
bool next_can_process = _can_process(p_enable);
if (prev_can_process && !next_can_process) {
notification(NOTIFICATION_PAUSED);
} else if (!prev_can_process && next_can_process) {
notification(NOTIFICATION_UNPAUSED);
}
data.blocked++;
for (KeyValue<StringName, Node *> &K : data.children) {
K.value->_propagate_pause_notification(p_enable);
}
data.blocked--;
}
void Node::_propagate_suspend_notification(bool p_enable) {
notification(p_enable ? NOTIFICATION_SUSPENDED : NOTIFICATION_UNSUSPENDED);
data.blocked++;
for (KeyValue<StringName, Node *> &KV : data.children) {
KV.value->_propagate_suspend_notification(p_enable);
}
data.blocked--;
}
Node::ProcessMode Node::get_process_mode() const {
return data.process_mode;
}
void Node::_propagate_process_owner(Node *p_owner, int p_pause_notification, int p_enabled_notification) {
data.process_owner = p_owner;
if (p_pause_notification != 0) {
notification(p_pause_notification);
}
if (p_enabled_notification != 0) {
notification(p_enabled_notification);
}
data.blocked++;
for (KeyValue<StringName, Node *> &K : data.children) {
Node *c = K.value;
if (c->data.process_mode == PROCESS_MODE_INHERIT) {
c->_propagate_process_owner(p_owner, p_pause_notification, p_enabled_notification);
}
}
data.blocked--;
}
void Node::set_multiplayer_authority(int p_peer_id, bool p_recursive) {
ERR_THREAD_GUARD
data.multiplayer_authority = p_peer_id;
if (p_recursive) {
for (KeyValue<StringName, Node *> &K : data.children) {
K.value->set_multiplayer_authority(p_peer_id, true);
}
}
}
int Node::get_multiplayer_authority() const {
return data.multiplayer_authority;
}
bool Node::is_multiplayer_authority() const {
ERR_FAIL_COND_V(!is_inside_tree(), false);
Ref<MultiplayerAPI> api = get_multiplayer();
return api.is_valid() && (api->get_unique_id() == data.multiplayer_authority);
}
/***** RPC CONFIG ********/
void Node::rpc_config(const StringName &p_method, const Variant &p_config) {
ERR_THREAD_GUARD
if (data.rpc_config.get_type() != Variant::DICTIONARY) {
data.rpc_config = Dictionary();
}
Dictionary node_config = data.rpc_config;
if (p_config.get_type() == Variant::NIL) {
node_config.erase(p_method);
} else {
ERR_FAIL_COND(p_config.get_type() != Variant::DICTIONARY);
node_config[p_method] = p_config;
}
}
Variant Node::get_rpc_config() const {
return data.rpc_config;
}
/***** RPC FUNCTIONS ********/
Error Node::_rpc_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
if (p_argcount < 1) {
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
r_error.expected = 1;
return ERR_INVALID_PARAMETER;
}
if (!p_args[0]->is_string()) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING_NAME;
return ERR_INVALID_PARAMETER;
}
StringName method = (*p_args[0]).operator StringName();
Error err = rpcp(0, method, &p_args[1], p_argcount - 1);
r_error.error = Callable::CallError::CALL_OK;
return err;
}
Error Node::_rpc_id_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
if (p_argcount < 2) {
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
r_error.expected = 2;
return ERR_INVALID_PARAMETER;
}
if (p_args[0]->get_type() != Variant::INT) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::INT;
return ERR_INVALID_PARAMETER;
}
if (!p_args[1]->is_string()) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 1;
r_error.expected = Variant::STRING_NAME;
return ERR_INVALID_PARAMETER;
}
int peer_id = *p_args[0];
StringName method = (*p_args[1]).operator StringName();
Error err = rpcp(peer_id, method, &p_args[2], p_argcount - 2);
r_error.error = Callable::CallError::CALL_OK;
return err;
}
Error Node::rpcp(int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) {
ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
Ref<MultiplayerAPI> api = get_multiplayer();
if (api.is_null()) {
return ERR_UNCONFIGURED;
}
return api->rpcp(this, p_peer_id, p_method, p_arg, p_argcount);
}
Ref<MultiplayerAPI> Node::get_multiplayer() const {
if (!is_inside_tree()) {
return Ref<MultiplayerAPI>();
}
return get_tree()->get_multiplayer(get_path());
}
//////////// end of rpc
bool Node::can_process_notification(int p_what) const {
switch (p_what) {
case NOTIFICATION_PHYSICS_PROCESS:
return data.physics_process;
case NOTIFICATION_PROCESS:
return data.process;
case NOTIFICATION_INTERNAL_PROCESS:
return data.process_internal;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS:
return data.physics_process_internal;
}
return true;
}
bool Node::can_process() const {
ERR_FAIL_COND_V(!is_inside_tree(), false);
return !get_tree()->is_suspended() && _can_process(get_tree()->is_paused());
}
bool Node::_can_process(bool p_paused) const {
ProcessMode process_mode;
if (data.process_mode == PROCESS_MODE_INHERIT) {
if (!data.process_owner) {
process_mode = PROCESS_MODE_PAUSABLE;
} else {
process_mode = data.process_owner->data.process_mode;
}
} else {
process_mode = data.process_mode;
}
// The owner can't be set to inherit, must be a bug.
ERR_FAIL_COND_V(process_mode == PROCESS_MODE_INHERIT, false);
if (process_mode == PROCESS_MODE_DISABLED) {
return false;
} else if (process_mode == PROCESS_MODE_ALWAYS) {
return true;
}
if (p_paused) {
return process_mode == PROCESS_MODE_WHEN_PAUSED;
} else {
return process_mode == PROCESS_MODE_PAUSABLE;
}
}
void Node::set_physics_interpolation_mode(PhysicsInterpolationMode p_mode) {
ERR_THREAD_GUARD
if (data.physics_interpolation_mode == p_mode) {
return;
}
data.physics_interpolation_mode = p_mode;
bool interpolate = true; // Default for root node.
switch (p_mode) {
case PHYSICS_INTERPOLATION_MODE_INHERIT: {
if (is_inside_tree() && data.parent) {
interpolate = data.parent->is_physics_interpolated();
}
} break;
case PHYSICS_INTERPOLATION_MODE_OFF: {
interpolate = false;
} break;
case PHYSICS_INTERPOLATION_MODE_ON: {
interpolate = true;
} break;
}
// If swapping from interpolated to non-interpolated, use this as an extra means to cause a reset.
if (is_physics_interpolated() && !interpolate && is_inside_tree()) {
propagate_notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
}
_propagate_physics_interpolated(interpolate);
}
void Node::reset_physics_interpolation() {
if (is_inside_tree()) {
propagate_notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
// If `reset_physics_interpolation()` is called explicitly by the user
// (e.g. from scripts) then we prevent deferred auto-resets taking place.
// The user is trusted to call reset in the right order, and auto-reset
// will interfere with their control of prev / curr, so should be turned off.
_propagate_physics_interpolation_reset_requested(false);
}
}
bool Node::_is_enabled() const {
ProcessMode process_mode;
if (data.process_mode == PROCESS_MODE_INHERIT) {
if (!data.process_owner) {
process_mode = PROCESS_MODE_PAUSABLE;
} else {
process_mode = data.process_owner->data.process_mode;
}
} else {
process_mode = data.process_mode;
}
return (process_mode != PROCESS_MODE_DISABLED);
}
bool Node::is_enabled() const {
ERR_FAIL_COND_V(!is_inside_tree(), false);
return _is_enabled();
}
double Node::get_physics_process_delta_time() const {
if (data.tree) {
return data.tree->get_physics_process_time();
} else {
return 0;
}
}
double Node::get_process_delta_time() const {
if (data.tree) {
return data.tree->get_process_time();
} else {
return 0;
}
}
void Node::set_process(bool p_process) {
ERR_THREAD_GUARD
if (data.process == p_process) {
return;
}
if (!is_inside_tree()) {
data.process = p_process;
return;
}
if (_is_any_processing()) {
_remove_from_process_thread_group();
}
data.process = p_process;
if (_is_any_processing()) {
_add_to_process_thread_group();
}