Skip to content

Add 'observe_target' method to 'RelatedSpawner' #18975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Glory2Antares
Copy link

@Glory2Antares Glory2Antares commented Apr 29, 2025

Objective

Add a way to spawn an observer targeting the 'target_entity()' of a 'RelatedSpawner'. This can be useful to react to Trigger's targeting a parent entity, and then making changes to children.

This enables a pattern very similar to signals' in Godot.

Solution

Use the 'world' field of 'RelatedSpawner' to spawn the 'Observer'.

Showcase

Use case:

Click to view showcase
fn before(world: &mut World) {
    world.spawn(Name::new("Parent")).with_children(|builder| {
        let parent_id = builder.target_entity();

        let mut child = builder.spawn(Name::new("Child"));
        let child_id = child.id();

        child.world_scope(move |world| {
            world.entity_mut(parent_id).observe(change_name(child_id));
        });
    });
}

fn after(mut commands: Commands) { // note now we can use commands instead of world
    commands.spawn(Name::new("Parent")).with_children(|builder| {
        let child_id = builder.spawn(Name::new("Child")).id();

        builder.observe_target(change_name(child_id));
    });
}

fn change_name(entity: Entity) -> impl Fn(Trigger<MyEvent>, Query<&mut Name>) {
    move |_: Trigger<MyEvent>, mut query: Query<&mut Name>| {
        if let Ok(mut name) = query.get_mut(entity) {
            name.set("New Name!");
        }
    }
}

Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@Glory2Antares Glory2Antares marked this pull request as draft April 29, 2025 05:12
@ickshonpe ickshonpe added the A-ECS Entities, components, systems, and events label Apr 29, 2025
@alice-i-cecile alice-i-cecile added C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Apr 29, 2025
@Glory2Antares Glory2Antares marked this pull request as ready for review April 29, 2025 18:50
@Glory2Antares
Copy link
Author

Glory2Antares commented May 4, 2025

For tracking it may be necessary to spawn the observer as so:

    pub fn observe_target<E: Event, B: Bundle, M>(
        &mut self,
        observer: impl IntoObserverSystem<E, B, M>,
    ) -> EntityWorldMut<'_> {
        self.world
        self.assert_not_despawned();
        self.world
            .spawn_with_caller(Observer::new(observer).with_entity(self.target), MaybeLocation::caller());
        self.world.flush();
        self.update_location();
        self
}

But I am not sure.

@alice-i-cecile alice-i-cecile added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels May 4, 2025
@alice-i-cecile alice-i-cecile moved this to Observer overhaul in Alice's Work Planning May 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
Status: Observer overhaul
Development

Successfully merging this pull request may close these issues.

3 participants