godot-bevy 0.10.0

Bridge between Bevy ECS and Godot 4 for Rust-powered game development
Documentation
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
use super::node_type_checking_generated::{
    add_comprehensive_node_type_markers, add_node_type_markers_from_string,
    remove_comprehensive_node_type_markers,
};
use crate::plugins::core::SceneTreeComponentRegistry;
use crate::prelude::{GodotScene, main_thread_system};
use crate::{
    interop::GodotNodeHandle,
    plugins::collisions::{
        AREA_ENTERED, AREA_EXITED, BODY_ENTERED, BODY_EXITED, COLLISION_START_SIGNALS,
        CollisionMessageType, Collisions,
    },
};
use bevy_app::{App, First, Plugin, PreStartup};
use bevy_ecs::{
    component::Component,
    entity::Entity,
    message::{Message, MessageReader, MessageWriter, message_update_system},
    prelude::{Name, ReflectComponent, ReflectResource, Resource},
    schedule::IntoScheduleConfigs,
    system::{Commands, NonSendMut, Query, Res, ResMut, SystemParam},
};
use bevy_reflect::Reflect;
use godot::{
    builtin::GString,
    classes::{Engine, Node, SceneTree},
    meta::ToGodot,
    obj::{Gd, Inherits, InstanceId, Singleton},
    prelude::GodotConvert,
};
use std::collections::HashMap;
use std::marker::PhantomData;
use tracing::{debug, trace, warn};

/// A resource that maintains an O(1) lookup from Godot `InstanceId` to Bevy `Entity`.
///
/// This index is automatically maintained by the scene tree plugin as entities are
/// added and removed. Use this for efficient collision handling, signal routing,
/// and any scenario where you need to find the Bevy entity for a Godot node.
///
/// # Example
///
/// ```ignore
/// fn handle_collision(
///     index: Res<NodeEntityIndex>,
///     // ... other params
/// ) {
///     let colliding_instance_id = /* from collision event */;
///     if let Some(entity) = index.get(colliding_instance_id) {
///         // Do something with the entity
///     }
/// }
/// ```
#[derive(Resource, Default, Debug, Reflect)]
#[reflect(Resource)]
pub struct NodeEntityIndex {
    #[reflect(ignore)]
    index: HashMap<InstanceId, Entity>,
}

impl NodeEntityIndex {
    /// Look up the Bevy `Entity` for a Godot `InstanceId`.
    ///
    /// Returns `None` if no entity is registered for this instance ID.
    #[inline]
    pub fn get(&self, instance_id: InstanceId) -> Option<Entity> {
        self.index.get(&instance_id).copied()
    }

    /// Check if an entity exists for the given `InstanceId`.
    #[inline]
    pub fn contains(&self, instance_id: InstanceId) -> bool {
        self.index.contains_key(&instance_id)
    }

    /// Returns the number of entries in the index.
    #[inline]
    pub fn len(&self) -> usize {
        self.index.len()
    }

    /// Returns true if the index is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.index.is_empty()
    }

    /// Insert a mapping from `InstanceId` to `Entity`.
    ///
    /// This is called internally by the scene tree plugin.
    #[inline]
    pub(crate) fn insert(&mut self, instance_id: InstanceId, entity: Entity) {
        self.index.insert(instance_id, entity);
    }

    /// Remove a mapping by `InstanceId`.
    ///
    /// This is called internally by the scene tree plugin.
    #[inline]
    pub(crate) fn remove(&mut self, instance_id: InstanceId) -> Option<Entity> {
        self.index.remove(&instance_id)
    }
}

/// Unified scene tree plugin that provides:
/// - SceneTreeRef for accessing the Godot scene tree
/// - Scene tree messages (NodeAdded, NodeRemoved, NodeRenamed)
/// - Automatic entity creation and mirroring for scene tree nodes
/// - Custom GodotChildOf/GodotChildren relationship for scene tree hierarchy
///
/// This plugin is always included in the core plugins and provides
/// complete scene tree integration out of the box.
pub struct GodotSceneTreePlugin {
    /// When true, despawning a parent entity will automatically despawn all children
    /// via the GodotChildren on_despawn hook.
    ///
    /// Set to false if you want to manually manage entity lifetimes independently
    /// of the Godot scene tree (e.g., for object pooling or entities that outlive their nodes).
    ///
    /// `ProtectedNodeEntity` children are never despawned automatically.
    pub auto_despawn_children: bool,
}

impl Default for GodotSceneTreePlugin {
    fn default() -> Self {
        Self {
            auto_despawn_children: true,
        }
    }
}

/// Configuration resource for scene tree behavior
#[derive(Resource, Reflect)]
#[reflect(Resource)]
pub struct SceneTreeConfig {
    /// When true, despawning a parent entity will automatically despawn all children
    /// via the GodotChildren on_despawn hook.
    ///
    /// Set to false if you want to manually manage entity lifetimes independently
    /// of the Godot scene tree (e.g., for object pooling or entities that outlive their nodes).
    ///
    /// `ProtectedNodeEntity` children are never despawned automatically.
    pub auto_despawn_children: bool,
}

impl Plugin for GodotSceneTreePlugin {
    fn build(&self, app: &mut App) {
        // Auto-register all discovered AutoSyncBundle plugins
        super::autosync::register_all_autosync_bundles(app);

        app.init_non_send_resource::<SceneTreeRefImpl>()
            .init_resource::<NodeEntityIndex>()
            .insert_resource(SceneTreeConfig {
                auto_despawn_children: self.auto_despawn_children,
            })
            .add_message::<SceneTreeMessage>()
            .add_systems(
                PreStartup,
                (connect_scene_tree, initialize_scene_tree).chain(),
            )
            .add_systems(
                First,
                (
                    write_scene_tree_messages.before(message_update_system),
                    read_scene_tree_messages.before(message_update_system),
                ),
            );
    }
}

#[derive(SystemParam)]
pub struct SceneTreeRef<'w, 's> {
    gd: NonSendMut<'w, SceneTreeRefImpl>,
    phantom: PhantomData<&'s ()>,
}

impl<'w, 's> SceneTreeRef<'w, 's> {
    pub fn get(&mut self) -> Gd<SceneTree> {
        self.gd.0.clone()
    }
}

#[doc(hidden)]
#[derive(Debug)]
pub(crate) struct SceneTreeRefImpl(Gd<SceneTree>);

impl SceneTreeRefImpl {
    fn get_ref() -> Gd<SceneTree> {
        Engine::singleton()
            .get_main_loop()
            .unwrap()
            .cast::<SceneTree>()
    }
}

impl Default for SceneTreeRefImpl {
    fn default() -> Self {
        Self(Self::get_ref())
    }
}

#[main_thread_system]
fn initialize_scene_tree(
    mut commands: Commands,
    mut scene_tree: SceneTreeRef,
    mut entities: Query<(&mut GodotNodeHandle, Entity, Option<&ProtectedNodeEntity>)>,
    component_registry: Res<SceneTreeComponentRegistry>,
    mut node_index: ResMut<NodeEntityIndex>,
) {
    let root = scene_tree.get().get_root().unwrap();

    // Check if we have the optimized GDScript watcher for type pre-analysis
    let optimized_watcher = root
        .try_get_node_as::<Node>("/root/BevyAppSingleton/OptimizedSceneTreeWatcher")
        .or_else(|| root.try_get_node_as::<Node>("BevyAppSingleton/OptimizedSceneTreeWatcher"));

    let messages = if let Some(mut watcher) = optimized_watcher {
        // Use optimized GDScript watcher to analyze the initial tree with type information
        tracing::info!("Using optimized initial tree analysis with type pre-analysis");

        let analysis_result = watcher.call("analyze_initial_tree", &[]);
        let result_dict = analysis_result.to::<godot::builtin::VarDictionary>();
        let instance_ids = result_dict
            .get("instance_ids")
            .unwrap()
            .to::<godot::builtin::PackedInt64Array>();
        let node_types = result_dict
            .get("node_types")
            .unwrap()
            .to::<godot::builtin::PackedStringArray>();

        let mut messages = Vec::new();
        let len = instance_ids.len().min(node_types.len());
        for i in 0..len {
            if let (Some(id), Some(type_gstring)) = (instance_ids.get(i), node_types.get(i)) {
                let type_str = type_gstring.to_string();

                messages.push(SceneTreeMessage {
                    node: GodotNodeHandle::from_instance_id(godot::prelude::InstanceId::from_i64(
                        id,
                    )),
                    message_type: SceneTreeMessageType::NodeAdded,
                    node_type: Some(type_str),
                });
            }
        }

        messages
    } else {
        // Use fallback traversal without type optimization
        tracing::info!("Using fallback initial tree analysis (no type optimization)");
        traverse_fallback(root.upcast())
    };

    create_scene_tree_entity(
        &mut commands,
        messages,
        &mut scene_tree,
        &mut entities,
        &component_registry,
        &mut node_index,
    );
}

fn traverse_fallback(node: Gd<Node>) -> Vec<SceneTreeMessage> {
    fn traverse_recursive(node: Gd<Node>, messages: &mut Vec<SceneTreeMessage>) {
        messages.push(SceneTreeMessage {
            node: GodotNodeHandle::from_instance_id(node.instance_id()),
            message_type: SceneTreeMessageType::NodeAdded,
            node_type: None, // No type optimization available
        });

        for child in node.get_children().iter_shared() {
            traverse_recursive(child, messages);
        }
    }

    let mut messages = Vec::new();
    traverse_recursive(node, &mut messages);
    messages
}

#[derive(Debug, Clone, Message)]
pub struct SceneTreeMessage {
    pub node: GodotNodeHandle,
    pub message_type: SceneTreeMessageType,
    pub node_type: Option<String>, // Pre-analyzed node type from GDScript watcher
}

#[derive(Copy, Clone, Debug, GodotConvert)]
#[godot(via = GString)]
pub enum SceneTreeMessageType {
    NodeAdded,
    NodeRemoved,
    NodeRenamed,
}

/// Helper function to recursively search for a node by name
fn find_node_by_name(parent: &Gd<Node>, name: &str) -> Option<Gd<Node>> {
    // Check if this node matches
    if parent.get_name().to_string() == name {
        return Some(parent.clone());
    }

    // Search children recursively
    for i in 0..parent.get_child_count() {
        if let Some(child) = parent.get_child(i) {
            let child_node = child.cast::<Node>();
            if let Some(found) = find_node_by_name(&child_node, name) {
                return Some(found);
            }
        }
    }

    None
}

#[main_thread_system]
fn connect_scene_tree(mut scene_tree: SceneTreeRef) {
    let mut scene_tree_gd = scene_tree.get();
    let root = scene_tree_gd.get_root().unwrap();

    // Try multiple paths to find the SceneTreeWatcher - support both production and test environments
    let watcher = root
        .try_get_node_as::<Node>("/root/BevyAppSingleton/SceneTreeWatcher")
        .or_else(|| {
            // Try without the full path for test environments
            root.try_get_node_as::<Node>("BevyAppSingleton/SceneTreeWatcher")
        })
        .or_else(|| {
            // Fallback: search entire tree for any SceneTreeWatcher (for test environments)
            tracing::debug!("Searching entire scene tree for SceneTreeWatcher");
            find_node_by_name(&root.clone().upcast(), "SceneTreeWatcher")
        })
        .unwrap_or_else(|| {
            panic!("SceneTreeWatcher not found. Searched /root/BevyAppSingleton/SceneTreeWatcher, BevyAppSingleton/SceneTreeWatcher, and entire tree.");
        });

    // Check if we have the optimized GDScript watcher
    let optimized_watcher = root
        .try_get_node_as::<Node>("/root/BevyAppSingleton/OptimizedSceneTreeWatcher")
        .or_else(|| root.try_get_node_as::<Node>("BevyAppSingleton/OptimizedSceneTreeWatcher"))
        .or_else(|| {
            // Fallback: search entire tree
            find_node_by_name(&root.clone().upcast(), "OptimizedSceneTreeWatcher")
        });

    if optimized_watcher.is_some() {
        // The optimized GDScript watcher handles scene tree connections and forwards
        // pre-analyzed messages to the Rust watcher (which has the MPSC sender)
        // No need to connect here - it connects automatically in its _ready()
        tracing::info!("Using optimized GDScript scene tree watcher with type pre-analysis");
    } else {
        // Fallback to direct connection without type optimization
        tracing::info!("Using fallback scene tree connection (no type optimization)");

        scene_tree_gd.connect(
            "node_added",
            &watcher
                .callable("scene_tree_event")
                .bind(&[SceneTreeMessageType::NodeAdded.to_variant()]),
        );

        scene_tree_gd.connect(
            "node_removed",
            &watcher
                .callable("scene_tree_event")
                .bind(&[SceneTreeMessageType::NodeRemoved.to_variant()]),
        );

        scene_tree_gd.connect(
            "node_renamed",
            &watcher
                .callable("scene_tree_event")
                .bind(&[SceneTreeMessageType::NodeRenamed.to_variant()]),
        );
    }
}

#[derive(Component, Debug, Reflect)]
#[reflect(Component)]
pub struct Groups {
    groups: Vec<String>,
}

impl Groups {
    pub fn is(&self, group_name: &str) -> bool {
        self.groups.iter().any(|name| name == group_name)
    }
}

impl<T: Inherits<Node>> From<&Gd<T>> for Groups {
    fn from(node: &Gd<T>) -> Self {
        Groups {
            groups: node
                .clone()
                .upcast::<Node>()
                .get_groups()
                .iter_shared()
                .map(|variant| variant.to_string())
                .collect(),
        }
    }
}

#[doc(hidden)]
pub struct SceneTreeMessageReader(pub std::sync::mpsc::Receiver<SceneTreeMessage>);

fn write_scene_tree_messages(
    message_reader: NonSendMut<SceneTreeMessageReader>,
    mut message_writer: MessageWriter<SceneTreeMessage>,
) {
    message_writer.write_batch(message_reader.0.try_iter());
}

/// Marks an entity so it is not despawned when its corresponding Godot Node is freed, breaking
/// the usual 1-to-1 lifetime between them. This allows game logic to keep running on entities
/// that have no Node, such as simulating off-screen factory machines or NPCs in inactive scenes.
/// A Godot Node can be re-associated later by adding a `GodotScene` component to the **entity.**
#[derive(Component)]
pub struct ProtectedNodeEntity;

fn create_scene_tree_entity(
    commands: &mut Commands,
    messages: impl IntoIterator<Item = SceneTreeMessage>,
    scene_tree: &mut SceneTreeRef,
    entities: &mut Query<(&mut GodotNodeHandle, Entity, Option<&ProtectedNodeEntity>)>,
    component_registry: &SceneTreeComponentRegistry,
    node_index: &mut NodeEntityIndex,
) {
    // Build local mapping with protection info (only needed during this function)
    let mut ent_mapping = entities
        .iter()
        .map(|(reference, ent, protected)| (reference.instance_id(), (ent, protected)))
        .collect::<HashMap<_, _>>();
    let scene_root = scene_tree.get().get_root().unwrap();

    // CollisionWatcher is optional - only required if GodotCollisionsPlugin is added
    let collision_watcher = scene_root
        .try_get_node_as::<Node>("/root/BevyAppSingleton/CollisionWatcher")
        .or_else(|| {
            // Try without the full path for test environments
            scene_root.try_get_node_as::<Node>("BevyAppSingleton/CollisionWatcher")
        })
        .or_else(|| {
            // Fallback: search entire tree for any CollisionWatcher (for test environments)
            tracing::debug!("Searching entire scene tree for CollisionWatcher");
            find_node_by_name(&scene_root.clone().upcast(), "CollisionWatcher")
        });

    for message in messages.into_iter() {
        trace!(target: "godot_scene_tree_messages", message = ?message);

        let mut node = message.node.clone();
        let ent = ent_mapping.get(&node.instance_id()).cloned();

        match message.message_type {
            SceneTreeMessageType::NodeAdded => {
                // Skip nodes that have been freed before we process them (can happen in tests)
                if !node.instance_id().lookup_validity() {
                    continue;
                }

                let mut ent = if let Some((ent, _)) = ent {
                    commands.entity(ent)
                } else {
                    commands.spawn_empty()
                };

                ent.insert(GodotNodeHandle::clone(&node))
                    .insert(Name::from(node.get::<Node>().get_name().to_string()));

                // Add node type marker components - use optimized version if available
                if let Some(ref node_type_str) = message.node_type {
                    // Use pre-analyzed type from GDScript watcher (much faster)
                    add_node_type_markers_from_string(&mut ent, node_type_str);
                } else {
                    // Fallback to comprehensive analysis with FFI calls
                    add_comprehensive_node_type_markers(&mut ent, &mut node);
                }

                let mut node = node.get::<Node>();

                // Check if the node is a collision body (Area2D, Area3D, RigidBody2D, RigidBody3D, etc.)
                // These nodes typically have collision detection capabilities
                // Only connect if CollisionWatcher exists (i.e., GodotCollisionsPlugin was added)
                if let Some(ref collision_watcher) = collision_watcher {
                    let is_collision_body = COLLISION_START_SIGNALS
                        .iter()
                        .any(|&signal| node.has_signal(signal));

                    if is_collision_body {
                        debug!(target: "godot_scene_tree_collisions",
                               node_id = node.instance_id().to_string(),
                               "is collision body");

                        let node_clone = node.clone();

                        if node.has_signal(BODY_ENTERED) {
                            node.connect(
                                BODY_ENTERED,
                                &collision_watcher.callable("collision_event").bind(&[
                                    node_clone.to_variant(),
                                    CollisionMessageType::Started.to_variant(),
                                ]),
                            );
                        }

                        if node.has_signal(BODY_EXITED) {
                            node.connect(
                                BODY_EXITED,
                                &collision_watcher.callable("collision_event").bind(&[
                                    node_clone.to_variant(),
                                    CollisionMessageType::Ended.to_variant(),
                                ]),
                            );
                        }

                        if node.has_signal(AREA_ENTERED) {
                            node.connect(
                                AREA_ENTERED,
                                &collision_watcher.callable("collision_event").bind(&[
                                    node_clone.to_variant(),
                                    CollisionMessageType::Started.to_variant(),
                                ]),
                            );
                        }

                        if node.has_signal(AREA_EXITED) {
                            node.connect(
                                AREA_EXITED,
                                &collision_watcher.callable("collision_event").bind(&[
                                    node_clone.to_variant(),
                                    CollisionMessageType::Ended.to_variant(),
                                ]),
                            );
                        }

                        // Add Collisions component to track collision state
                        ent.insert(Collisions::default());
                    }
                }

                ent.insert(Groups::from(&node));

                // Add all components registered by plugins
                component_registry.add_to_entity(&mut ent, &message.node);

                let ent = ent.id();
                let instance_id = node.instance_id();
                ent_mapping.insert(instance_id, (ent, None));
                node_index.insert(instance_id, ent);

                // Try to add any registered bundles for this node type
                super::autosync::try_add_bundles_for_node(commands, ent, &message.node);

                // Add GodotChildOf relationship to mirror Godot's scene tree hierarchy
                if node.instance_id() != scene_root.instance_id()
                    && let Some(parent) = node.get_parent()
                {
                    let parent_id = parent.instance_id();
                    if let Some((parent_entity, _)) = ent_mapping.get(&parent_id) {
                        commands
                            .entity(ent)
                            .insert(super::relationship::GodotChildOf(*parent_entity));
                    } else {
                        warn!(target: "godot_scene_tree_messages",
                            "Parent entity with ID {} not found in ent_mapping. This might indicate a missing or incorrect mapping.",
                            parent_id);
                    }
                }
            }
            SceneTreeMessageType::NodeRemoved => {
                if let Some((ent, prot_opt)) = ent {
                    // Check if node is being reparented vs truly removed
                    // During reparenting, the node is temporarily removed from old parent
                    // but still exists in the scene tree (has a parent)
                    // We need to try_get because the node handle might be invalid if freed
                    let is_reparenting = node
                        .try_get::<Node>()
                        .map(|godot_node| godot_node.get_parent().is_some())
                        .unwrap_or(false);

                    if is_reparenting {
                        // Node is being reparented - don't despawn entity, it will be re-added
                        trace!(target: "godot_scene_tree_events",
                            "Node is being reparented, preserving entity");
                        // Don't remove from ent_mapping - entity still valid
                    } else {
                        // Node is truly being removed (freed or despawned)
                        let protected = prot_opt.is_some();
                        if !protected {
                            commands.entity(ent).despawn();
                        } else {
                            _strip_godot_components(commands, ent, &node);
                        }
                        let instance_id = node.instance_id();
                        ent_mapping.remove(&instance_id);
                        node_index.remove(instance_id);
                    }
                } else {
                    // Entity was already despawned (common when using queue_free)
                    trace!(target: "godot_scene_tree_messages", "Entity for removed node was already despawned");
                }
            }
            SceneTreeMessageType::NodeRenamed => {
                if let Some((ent, _)) = ent {
                    commands
                        .entity(ent)
                        .insert(Name::from(node.get::<Node>().get_name().to_string()));
                } else {
                    trace!(target: "godot_scene_tree_messages", "Entity for renamed node was already despawned");
                }
            }
        }
    }
}

fn _strip_godot_components(commands: &mut Commands, ent: Entity, node: &GodotNodeHandle) {
    let mut entity_commands = commands.entity(ent);

    entity_commands.remove::<GodotNodeHandle>();
    entity_commands.remove::<GodotScene>();
    entity_commands.remove::<Name>();
    entity_commands.remove::<Groups>();

    remove_comprehensive_node_type_markers(&mut entity_commands, &mut node.clone());
}

#[main_thread_system]
#[allow(clippy::too_many_arguments)]
fn read_scene_tree_messages(
    mut commands: Commands,
    mut scene_tree: SceneTreeRef,
    mut message_reader: MessageReader<SceneTreeMessage>,
    mut entities: Query<(&mut GodotNodeHandle, Entity, Option<&ProtectedNodeEntity>)>,
    component_registry: Res<SceneTreeComponentRegistry>,
    mut node_index: ResMut<NodeEntityIndex>,
) {
    create_scene_tree_entity(
        &mut commands,
        message_reader.read().cloned(),
        &mut scene_tree,
        &mut entities,
        &component_registry,
        &mut node_index,
    );
}