Skip to content
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

Creating a View if Entity matches the Filter of a Kind _after_ it was already spawned. #2

Closed
speakk opened this issue Mar 16, 2025 · 3 comments

Comments

@speakk
Copy link

speakk commented Mar 16, 2025

Hi!

If I'm not mistaken, if an entity gets components added later, after spawn, to make it match a Kind Filter, whereas it didn't before, the View never spawns for this Entity.

Is this desired behaviour, and if so, should it perhaps be documented?

That said, if it's possible to add this feature, that would be very handy.

(I might be wrong about this, but I tried it out, and looked through the source and didn't at least see such functionality)

@Zeenobit
Copy link
Owner

Zeenobit commented Mar 18, 2025

In theory, this should already work. I need to add test coverage for it, so I'm not 100% sure.

If your component is added as a view for a different kind (i.e. add_view), then you'd need to rebuild the view so that the new view can be added. See moonshine_view::rebuild. Edit: This should work without needing a rebuild.

If your component is added as a viewable (i.e. add_viewable), then it should already work as is, since the viewable component is what triggers the view spawn.

Let me know if your observation is different please!

@Zeenobit
Copy link
Owner

I just did a quick test to sanity check this. It should already work as is:

    use bevy::prelude::*;

    #[derive(Component)]
    struct M;

    impl BuildView for M {
        fn build(_world: &World, _object: Object<Self>, mut view: ViewCommands<Self>) {
            view.insert(V);
        }
    }

    #[derive(Component)]
    struct MX;

    impl BuildView<M> for MX {
        fn build(_world: &World, _object: Object<M>, mut view: ViewCommands<M>) {
            view.insert(VX);
        }
    }

    #[derive(Component)]
    struct V;

    #[derive(Component)]
    struct VX;

    #[test]
    fn add_view() {
        let mut app = App::new();
        app.add_plugins(MinimalPlugins)
            .add_viewable::<M>()
            .add_view::<M, MX>();

        let m = app.world_mut().spawn(M).id();
        app.update();

        {
            let w = app.world();
            let v = w.entity(w.get::<Viewable<M>>(m).unwrap().view().entity());

            assert!(v.contains::<V>());
            assert!(!v.contains::<VX>());
        }

        app.world_mut().entity_mut(m).insert(MX);
        app.update();

        {
            let w = app.world();
            let v = w.entity(w.get::<Viewable<M>>(m).unwrap().view().entity());

            assert!(v.contains::<V>());
            assert!(v.contains::<VX>());
        }
    }

@speakk
Copy link
Author

speakk commented Mar 19, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants