#Node is not deleted after `despawn_recursive`

20 messages · Page 1 of 1 (latest)

ashen shore
#
#[derive(Component)]
pub struct MainMenu {}

pub struct UI;

impl Plugin for UI {
    fn build(&self, app: &mut App) {
        app
        .add_systems(OnEnter(AppState::MainMenu), spawn_main_menu)
        .add_systems(Update, (
            transition_to_game_state, 
            translation_to_main_menu
        ))
        // Just Buttons
        .add_systems(Update, (
            interact_with_play_button.run_if(in_state(AppState::MainMenu)), 
            interact_with_quit_button.run_if(in_state(AppState::MainMenu))
        ))
        .add_systems(Update, exit_game)
        .add_systems(OnExit(AppState::MainMenu), despawn_main_menu)
        .add_systems(OnEnter(AppState::Game), spawn_game_ui)
        .add_systems(OnExit(AppState::Game), despawn_game_ui);
    }
}

fn spawn_main_menu(mut commands: Commands, asset_server: Res<AssetServer>) {
    build_main_menu(&mut commands, &asset_server);
}

...
#
fn build_main_menu(commands: &mut Commands, _asset_server: &Res<AssetServer>) -> Entity {
    let main_menu_entity = commands
        .spawn(NodeBundle {
                style: Style {
                    height: Val::Percent(100.0),
                    width: Val::Percent(100.0),
                    flex_direction: FlexDirection::Column,
                    justify_content: JustifyContent::Center,
                    align_items: AlignItems::Center,
                    ..default()
                },
                background_color: Color::rgb(50.0, 50.0, 50.0).into(),
                ..default()
            })
            //childs
            })
            .id();
    main_menu_entity
}

fn despawn_main_menu(mut commands: Commands, main_menu_query: Query<Entity, With<MainMenu>>) {
    if let Ok(main_menu_entity) = main_menu_query.get_single() {
        commands.entity(main_menu_entity).despawn_recursive()
    }
}
#
fn spawn_game_ui(mut commands: Commands, asset_server: Res<AssetServer>) {
    build_game_ui(&mut commands, &asset_server);
}

fn build_game_ui(commands: &mut Commands, _asset_server: &Res<AssetServer>) -> Entity {
    let game_ui_entity = commands
        .spawn((
            NodeBundle {
                style: Style {
                    width: Val::Percent(100.0),
                    height: Val::Percent(10.0),
                    align_items: AlignItems::Center,
                    padding: UiRect::all(Val::Px(10.0)),
                    ..default()
                },
                background_color: Color::BLUE.into(),
                ..default()
            },
            GameUI {},
        )).id();
    game_ui_entity
}

fn despawn_game_ui(mut commands: Commands, game_ui_query: Query<Entity, With<GameUI>>) {
    if let Ok(game_ui_entity) = game_ui_query.get_single() {
        commands.entity(game_ui_entity).despawn_recursive()
    }
}
#
pub fn transition_to_game_state(
    keyboard_input: Res<ButtonInput<KeyCode>>,
    app_state: Res<State<AppState>>,
    mut app_state_next_state: ResMut<NextState<AppState>>,
) {
    if keyboard_input.just_pressed(KeyCode::Enter) {
        if app_state.get() != &AppState::Game {
            app_state_next_state.set(AppState::Game);
            println!("Entered AppState::Game");
        }
    }
}

pub fn translation_to_main_menu(
    keyboard_input: Res<ButtonInput<KeyCode>>,
    app_state: Res<State<AppState>>,
    mut app_state_reverse: ResMut<NextState<AppState>>
) {
    if keyboard_input.just_pressed(KeyCode::Escape) {
        if app_state.get() != &AppState::MainMenu {
            app_state_reverse.set(AppState::MainMenu);
            println!("Entered AppState::MainMenu")
        }
    }
}

pub fn exit_game(
    keyboard_input: Res<ButtonInput<KeyCode>>,
    mut app_exit_event_writer: EventWriter<AppExit>,
) {
    if keyboard_input.just_pressed(KeyCode::F4) {
        app_exit_event_writer.send(AppExit);
    }
}
ashen shore
#

I changed the code a bit, but I'm still worried about the increase in the number of 2v(x)

elfin current
ashen shore
elfin current
elfin current
ashen shore
elfin current
ashen shore
#

In this case, I just don't understand how the inspector works, every time I switch between two UIs, the node's numbers grow, I do not know if this is the norm

elfin current
#

the thing that goes up is the generational index

#

which just counts how often the slot (the first number) has been spawned/despawned

#

it exists to invallidate despawned entities while still being able to use their slot

ashen shore
#

OK, it's a false alarm. Knowing my experience with C#, I immediately pay attention to it and perceive it as a bug/error

#

Thanks

elfin current
#

no problem