Skip to content

Respond to asset changes using observers. #16041

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
inodentry opened this issue Oct 20, 2024 · 4 comments
Open

Respond to asset changes using observers. #16041

inodentry opened this issue Oct 20, 2024 · 4 comments
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Feature A new feature, making something new possible C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!

Comments

@inodentry
Copy link
Contributor

What problem does this solve or what need does it fill?

It would be quite ergonomic to be able to respond to when assets get loaded, changed, etc, using observers. We currently have AssetEvent, but the way currently things stand in Bevy, old-style events (using EventReader/EventWriter) and observer triggers are actually different workflows that are incompatible with one another, even though they both (confusingly) use the same trait (Event).

What solution would you like?

Bevy to "trigger" observer-style events when assets get loaded/changed/etc.

What alternative(s) have you considered?

Just using EventReader<AssetEvent>

Additional context

If Bevy ever moves to "Assets as Entities" (as was attempted during the AssetsV2 rework, but ultimately dropped in favor of using Assets<T> resources as before), this would become even more ergonomic, as the observer trigger could "target" the entity that represents the affected asset.

@inodentry inodentry added A-Assets Load files from disk to use for things like images, models, and sounds C-Feature A new feature, making something new possible C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Oct 20, 2024
@BenjaminBrienen BenjaminBrienen added S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes labels Oct 29, 2024
@tychedelia
Copy link
Member

This would be incredible helpful for #16159

@Peepo-Juice
Copy link
Contributor

I'm thinking about implementing this by just triggering the observers where we have EventWriter<AssetEvent>> and it seems theres only one place where we are doing this? I have no experience with bevy internals, so im not sure how naive that assumption is. Is there another way for events to be written to the queue?

@simbleau
Copy link
Member

Ah, #18048 is related.

@davids91
Copy link

davids91 commented May 16, 2025

The same would also be useful for Resources, which don't have an Event implementation, and can only detect changes from Res instances in queries.
This makes custom synchronization logic from MainWorld to Renderworld quite cumbersome. There is ExtractResourcePlugin for that, but that is one-way, which is inadequate for some use-cases.

pub(crate) fn sync_from_main_world(
    mut commands: Commands,
    world: Res<bevy::render::MainWorld>,
    render_world_resource: Option<ResMut<BojlerElado>>,
) {
    // If not available in renderworld, add. EZ
    if let Some(main_world_resource) = world.get_resource::<BojlerElado>() {
        if render_world_viewset.is_none() {
            commands.insert_resource(main_world_resource.clone());
            return;
        }

        // Change detection not available, because the main world resource is available through world
        let render_world_resource = render_world_viewset.as_ref().unwrap();
    }
}

As a workaround, It is also possible to run a query through
https://docs.rs/bevy/latest/bevy/ecs/system/struct.SystemState.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Feature A new feature, making something new possible C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!
Projects
None yet
Development

No branches or pull requests

6 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy