Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/scene_tree.h
21218 views
1
/**************************************************************************/
2
/* scene_tree.h */
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
#pragma once
32
33
#include "core/object/ref_counted.h"
34
#include "core/os/main_loop.h"
35
#include "core/os/thread_safe.h"
36
#include "core/templates/paged_allocator.h"
37
#include "core/templates/self_list.h"
38
#include "scene/main/scene_tree_fti.h"
39
40
#include <cstdlib>
41
42
#undef Window
43
44
class ArrayMesh;
45
class PackedScene;
46
class InputEvent;
47
class Node;
48
#ifndef _3D_DISABLED
49
class Node3D;
50
#endif
51
class Window;
52
class Material;
53
class Mesh;
54
class MultiplayerAPI;
55
class SceneDebugger;
56
class Tween;
57
class Viewport;
58
59
class SceneTreeTimer : public RefCounted {
60
GDCLASS(SceneTreeTimer, RefCounted);
61
62
double time_left = 0.0;
63
bool process_always = true;
64
bool process_in_physics = false;
65
bool ignore_time_scale = false;
66
67
protected:
68
static void _bind_methods();
69
70
public:
71
void set_time_left(double p_time);
72
double get_time_left() const;
73
74
void set_process_always(bool p_process_always);
75
bool is_process_always();
76
77
void set_process_in_physics(bool p_process_in_physics);
78
bool is_process_in_physics();
79
80
void set_ignore_time_scale(bool p_ignore);
81
bool is_ignoring_time_scale();
82
83
void release_connections();
84
};
85
86
class SceneTree : public MainLoop {
87
_THREAD_SAFE_CLASS_
88
89
GDCLASS(SceneTree, MainLoop);
90
91
public:
92
typedef void (*IdleCallback)();
93
94
private:
95
CallQueue::Allocator *process_group_call_queue_allocator = nullptr;
96
97
struct ProcessGroup {
98
CallQueue call_queue;
99
Vector<Node *> nodes;
100
Vector<Node *> physics_nodes;
101
bool node_order_dirty = true;
102
bool physics_node_order_dirty = true;
103
bool removed = false;
104
Node *owner = nullptr;
105
uint64_t last_pass = 0;
106
};
107
108
struct ProcessGroupSort {
109
_FORCE_INLINE_ bool operator()(const ProcessGroup *p_left, const ProcessGroup *p_right) const;
110
};
111
112
PagedAllocator<ProcessGroup, true> group_allocator; // Allocate groups on pages, to enhance cache usage.
113
114
LocalVector<ProcessGroup *> process_groups;
115
bool process_groups_dirty = true;
116
LocalVector<ProcessGroup *> local_process_group_cache; // Used when processing to group what needs to
117
uint64_t process_last_pass = 1;
118
119
ProcessGroup default_process_group;
120
121
bool node_threading_disabled = false;
122
123
struct Group {
124
Vector<Node *> nodes;
125
bool changed = false;
126
};
127
128
#ifndef _3D_DISABLED
129
struct ClientPhysicsInterpolation {
130
SelfList<Node3D>::List _node_3d_list;
131
void physics_process();
132
} _client_physics_interpolation;
133
#endif
134
135
Window *root = nullptr;
136
137
double physics_process_time = 0.0;
138
double process_time = 0.0;
139
bool accept_quit = true;
140
bool quit_on_go_back = true;
141
142
#ifdef DEBUG_ENABLED
143
bool debug_collisions_hint = false;
144
bool debug_paths_hint = false;
145
bool debug_navigation_hint = false;
146
#endif
147
bool paused = false;
148
bool suspended = false;
149
150
HashMap<StringName, Group> group_map;
151
bool _quit = false;
152
153
// Static so we can get directly instead of via SceneTree pointer.
154
static bool _physics_interpolation_enabled;
155
156
// Note that physics interpolation is hard coded to OFF in the editor,
157
// therefore we have a second bool to enable e.g. configuration warnings
158
// to only take effect when the project is using physics interpolation.
159
static bool _physics_interpolation_enabled_in_project;
160
161
SceneTreeFTI scene_tree_fti;
162
163
StringName tree_changed_name = "tree_changed";
164
StringName node_added_name = "node_added";
165
StringName node_removed_name = "node_removed";
166
StringName node_renamed_name = "node_renamed";
167
168
int64_t current_frame = 0;
169
int nodes_in_tree_count = 0;
170
171
#ifdef TOOLS_ENABLED
172
Node *edited_scene_root = nullptr;
173
#endif
174
struct UGCall {
175
StringName group;
176
StringName call;
177
178
static uint32_t hash(const UGCall &p_val) {
179
return p_val.group.hash() ^ p_val.call.hash();
180
}
181
bool operator==(const UGCall &p_with) const { return group == p_with.group && call == p_with.call; }
182
bool operator<(const UGCall &p_with) const { return group == p_with.group ? call < p_with.call : group < p_with.group; }
183
};
184
185
// Safety for when a node is deleted while a group is being called.
186
187
int nodes_removed_on_group_call_lock = 0;
188
HashSet<Node *> nodes_removed_on_group_call; // Skip erased nodes.
189
190
List<ObjectID> delete_queue;
191
192
uint64_t accessibility_upd_per_sec = 0;
193
bool accessibility_force_update = true;
194
HashSet<ObjectID> accessibility_change_queue;
195
uint64_t accessibility_last_update = 0;
196
197
HashMap<UGCall, Vector<Variant>, UGCall> unique_group_calls;
198
bool ugc_locked = false;
199
void _flush_ugc();
200
201
_FORCE_INLINE_ void _update_group_order(Group &g);
202
203
TypedArray<Node> _get_nodes_in_group(const StringName &p_group);
204
205
Node *current_scene = nullptr;
206
ObjectID prev_scene_id;
207
ObjectID pending_new_scene_id;
208
209
Color debug_collisions_color;
210
Color debug_collision_contact_color;
211
Color debug_paths_color;
212
float debug_paths_width = 1.0f;
213
Ref<ArrayMesh> debug_contact_mesh;
214
Ref<Material> debug_paths_material;
215
Ref<Material> collision_material;
216
int collision_debug_contacts;
217
218
void _flush_scene_change();
219
220
List<Ref<SceneTreeTimer>> timers;
221
List<Ref<Tween>> tweens;
222
223
///network///
224
225
Ref<MultiplayerAPI> multiplayer;
226
HashMap<NodePath, Ref<MultiplayerAPI>> custom_multiplayers;
227
bool multiplayer_poll = true;
228
229
static SceneTree *singleton;
230
friend class Node;
231
232
void tree_changed();
233
void node_added(Node *p_node);
234
void node_removed(Node *p_node);
235
void node_renamed(Node *p_node);
236
void process_timers(double p_delta, bool p_physics_frame);
237
void process_tweens(double p_delta, bool p_physics_frame);
238
239
Group *add_to_group(const StringName &p_group, Node *p_node);
240
void remove_from_group(const StringName &p_group, Node *p_node);
241
242
void _process_group(ProcessGroup *p_group, bool p_physics);
243
void _process_groups_thread(uint32_t p_index, bool p_physics);
244
void _process(bool p_physics);
245
246
void _remove_process_group(Node *p_node);
247
void _add_process_group(Node *p_node);
248
void _remove_node_from_process_group(Node *p_node, Node *p_owner);
249
void _add_node_to_process_group(Node *p_node, Node *p_owner);
250
251
void _call_group_flags(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
252
void _call_group(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
253
254
void _flush_delete_queue();
255
// Optimization.
256
friend class CanvasItem;
257
friend class Node3D;
258
friend class Viewport;
259
260
SelfList<Node>::List xform_change_list;
261
262
#ifdef DEBUG_ENABLED // No live editor in release build.
263
friend class LiveEditor;
264
#endif
265
266
enum {
267
MAX_IDLE_CALLBACKS = 256
268
};
269
270
static IdleCallback idle_callbacks[MAX_IDLE_CALLBACKS];
271
static int idle_callback_count;
272
void _call_idle_callbacks();
273
274
void _main_window_focus_in();
275
void _main_window_close();
276
void _main_window_go_back();
277
278
enum CallInputType {
279
CALL_INPUT_TYPE_INPUT,
280
CALL_INPUT_TYPE_SHORTCUT_INPUT,
281
CALL_INPUT_TYPE_UNHANDLED_INPUT,
282
CALL_INPUT_TYPE_UNHANDLED_KEY_INPUT,
283
};
284
285
//used by viewport
286
void _call_input_pause(const StringName &p_group, CallInputType p_call_type, const Ref<InputEvent> &p_input, Viewport *p_viewport);
287
288
protected:
289
void _notification(int p_notification);
290
static void _bind_methods();
291
292
public:
293
enum {
294
NOTIFICATION_TRANSFORM_CHANGED = 2000
295
};
296
297
enum GroupCallFlags {
298
GROUP_CALL_DEFAULT = 0,
299
GROUP_CALL_REVERSE = 1,
300
GROUP_CALL_DEFERRED = 2,
301
GROUP_CALL_UNIQUE = 4,
302
};
303
304
_FORCE_INLINE_ Window *get_root() const { return root; }
305
306
void call_group_flagsp(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, const Variant **p_args, int p_argcount);
307
void notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification);
308
void set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value);
309
310
// `notify_group()` is immediate by default since Godot 4.0.
311
void notify_group(const StringName &p_group, int p_notification);
312
// `set_group()` is immediate by default since Godot 4.0.
313
void set_group(const StringName &p_group, const String &p_name, const Variant &p_value);
314
315
template <typename... VarArgs>
316
// `call_group()` is immediate by default since Godot 4.0.
317
void call_group(const StringName &p_group, const StringName &p_function, VarArgs... p_args) {
318
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
319
const Variant *argptrs[sizeof...(p_args) + 1];
320
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
321
argptrs[i] = &args[i];
322
}
323
call_group_flagsp(GROUP_CALL_DEFAULT, p_group, p_function, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
324
}
325
326
template <typename... VarArgs>
327
void call_group_flags(uint32_t p_flags, const StringName &p_group, const StringName &p_function, VarArgs... p_args) {
328
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
329
const Variant *argptrs[sizeof...(p_args) + 1];
330
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
331
argptrs[i] = &args[i];
332
}
333
call_group_flagsp(p_flags, p_group, p_function, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
334
}
335
336
void flush_transform_notifications();
337
338
bool is_accessibility_enabled() const;
339
bool is_accessibility_supported() const;
340
void _accessibility_force_update();
341
void _accessibility_notify_change(const Node *p_node, bool p_remove = false);
342
void _flush_accessibility_changes();
343
void _process_accessibility_changes(int p_window_id); // Effectively DisplayServer::WindowID
344
345
virtual void initialize() override;
346
347
virtual void iteration_prepare() override;
348
349
virtual bool physics_process(double p_time) override;
350
virtual void iteration_end() override;
351
virtual bool process(double p_time) override;
352
353
virtual void finalize() override;
354
355
bool is_auto_accept_quit() const;
356
void set_auto_accept_quit(bool p_enable);
357
358
bool is_quit_on_go_back() const;
359
void set_quit_on_go_back(bool p_enable);
360
361
void quit(int p_exit_code = EXIT_SUCCESS);
362
363
_FORCE_INLINE_ double get_physics_process_time() const { return physics_process_time; }
364
_FORCE_INLINE_ double get_process_time() const { return process_time; }
365
366
void set_pause(bool p_enabled);
367
bool is_paused() const;
368
void set_suspend(bool p_enabled);
369
bool is_suspended() const;
370
371
#ifdef DEBUG_ENABLED
372
void set_debug_collisions_hint(bool p_enabled);
373
bool is_debugging_collisions_hint() const;
374
375
void set_debug_paths_hint(bool p_enabled);
376
bool is_debugging_paths_hint() const;
377
378
void set_debug_navigation_hint(bool p_enabled);
379
bool is_debugging_navigation_hint() const;
380
#else
381
void set_debug_collisions_hint(bool p_enabled) {}
382
bool is_debugging_collisions_hint() const { return false; }
383
384
void set_debug_paths_hint(bool p_enabled) {}
385
bool is_debugging_paths_hint() const { return false; }
386
387
void set_debug_navigation_hint(bool p_enabled) {}
388
bool is_debugging_navigation_hint() const { return false; }
389
#endif
390
391
void set_debug_collisions_color(const Color &p_color);
392
Color get_debug_collisions_color() const;
393
394
void set_debug_collision_contact_color(const Color &p_color);
395
Color get_debug_collision_contact_color() const;
396
397
void set_debug_paths_color(const Color &p_color);
398
Color get_debug_paths_color() const;
399
400
void set_debug_paths_width(float p_width);
401
float get_debug_paths_width() const;
402
403
Ref<Material> get_debug_paths_material();
404
Ref<Material> get_debug_collision_material();
405
Ref<ArrayMesh> get_debug_contact_mesh();
406
407
int get_collision_debug_contact_count() { return collision_debug_contacts; }
408
409
int64_t get_frame() const;
410
411
int get_node_count() const;
412
413
void queue_delete(RequiredParam<Object> rp_object);
414
415
Vector<Node *> get_nodes_in_group(const StringName &p_group);
416
Node *get_first_node_in_group(const StringName &p_group);
417
bool has_group(const StringName &p_identifier) const;
418
int get_node_count_in_group(const StringName &p_group) const;
419
420
//void change_scene(const String& p_path);
421
//Node *get_loaded_scene();
422
423
void set_edited_scene_root(Node *p_node);
424
Node *get_edited_scene_root() const;
425
426
void set_current_scene(Node *p_scene);
427
Node *get_current_scene() const;
428
Error change_scene_to_file(const String &p_path);
429
Error change_scene_to_packed(RequiredParam<PackedScene> rp_scene);
430
Error change_scene_to_node(RequiredParam<Node> rp_node);
431
Error reload_current_scene();
432
void unload_current_scene();
433
434
RequiredResult<SceneTreeTimer> create_timer(double p_delay_sec, bool p_process_always = true, bool p_process_in_physics = false, bool p_ignore_time_scale = false);
435
RequiredResult<Tween> create_tween();
436
void remove_tween(const Ref<Tween> &p_tween);
437
TypedArray<Tween> get_processed_tweens();
438
439
//used by Main::start, don't use otherwise
440
void add_current_scene(Node *p_current);
441
442
static SceneTree *get_singleton() { return singleton; }
443
444
#ifdef TOOLS_ENABLED
445
void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
446
#endif
447
448
//network API
449
450
Ref<MultiplayerAPI> get_multiplayer(const NodePath &p_for_path = NodePath()) const;
451
void set_multiplayer(Ref<MultiplayerAPI> p_multiplayer, const NodePath &p_root_path = NodePath());
452
void set_multiplayer_poll_enabled(bool p_enabled);
453
bool is_multiplayer_poll_enabled() const;
454
455
static void add_idle_callback(IdleCallback p_callback);
456
457
void set_disable_node_threading(bool p_disable);
458
//default texture settings
459
460
void set_physics_interpolation_enabled(bool p_enabled);
461
bool is_physics_interpolation_enabled() const { return _physics_interpolation_enabled; }
462
463
// Different name to disambiguate fast static versions from the user bound versions.
464
static bool is_fti_enabled() { return _physics_interpolation_enabled; }
465
static bool is_fti_enabled_in_project() { return _physics_interpolation_enabled_in_project; }
466
467
#ifndef _3D_DISABLED
468
void client_physics_interpolation_add_node_3d(SelfList<Node3D> *p_elem);
469
void client_physics_interpolation_remove_node_3d(SelfList<Node3D> *p_elem);
470
#endif
471
472
SceneTreeFTI &get_scene_tree_fti() { return scene_tree_fti; }
473
474
SceneTree();
475
~SceneTree();
476
};
477
478
VARIANT_ENUM_CAST(SceneTree::GroupCallFlags);
479
480