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

Remove rendering systems from app when they aren't relevant to the currently shown view state. #40

Open
MeoMix opened this issue Jan 22, 2024 · 0 comments

Comments

@MeoMix
Copy link
Owner

MeoMix commented Jan 22, 2024

Currently, Bevy does not support removing Systems from running applications, bevyengine/bevy#279

This results in rendering systems which must check state and early exit. As an example,

pub fn on_update_ant_position(
    ant_model_query: Query<(Entity, &Position), (With<Ant>, Changed<Position>)>,
    mut ant_view_query: Query<(&mut Transform, &TranslationOffset)>,
    nest_query: Query<&Grid, With<Nest>>,
    model_view_entity_map: Res<ModelViewEntityMap>,
    visible_grid: Res<VisibleGrid>,
) {
    let visible_grid_entity = match visible_grid.0 {
        Some(visible_grid_entity) => visible_grid_entity,
        None => return,
    };

    let grid = match nest_query.get(visible_grid_entity) {
        Ok(grid) => grid,
        Err(_) => return,
    };

    for (ant_model_entity, position) in ant_model_query.iter() {
        if let Some(&ant_view_entity) = model_view_entity_map.0.get(&ant_model_entity) {
            if let Ok((mut transform, translation_offset)) = ant_view_query.get_mut(ant_view_entity)
            {
                transform.translation = grid
                    .grid_to_world_position(*position)
                    .add(translation_offset.0);
            }
        }
    }
}

This system checks if Nest is visible and, if not, exits without taking effect.

If we were to conditionally not run this system by using a run_if condition then change detection would be negatively affected. Changed<Position> would contains all changes since the system last ran. This isn't desirable. An alternative would be to remove the system entirely, but this isn't currently supported. As such, the workaround is to run the system, but do nothing, to ensure change detection functions as desired.

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

No branches or pull requests

1 participant