#Spawn on State change take a second

3 messages · Page 1 of 1 (latest)

thorn relic
#


use bevy::prelude::*;


fn main(){

    App::new()

        .add_plugins(DefaultPlugins)

        .add_state::<AppState>()

        .add_systems(Startup, camera_spawn)

        .add_systems(OnEnter(AppState::Spawning), setup)

        .add_systems(Update, toggle_states)

        .add_systems(OnExit(AppState::Spawning), despawn )

    .run();

}


#[derive(Component)]

struct CameraSpawn;


#[derive(Component)]

struct Sprites;


fn camera_spawn(

    mut commands: Commands

){

    commands.spawn(Camera2dBundle::default());

}


fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {

    commands.spawn((SpriteBundle {

        texture: asset_server.load("sface.png"),

        ..default()

    }, Sprites));

}


fn despawn(

    mut commands: Commands, 

    s_querry: Query<Entity, With<Sprites>>

    ) {

        for sprite in s_querry.iter() {

            commands.entity(sprite).despawn();

        }

}


#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]

pub enum AppState{

    #[default]

    Running,

    Spawning

}


fn toggle_states(

    mut commands: Commands,

    keyboard_input: Res<Input<KeyCode>>,

    app_state: Res<State<AppState>>

){

    if keyboard_input.just_pressed(KeyCode::Q){

        if app_state.get() == &AppState::Running {

            commands.insert_resource(NextState(Some(AppState::Spawning)))

        }

        if app_state.get() == &AppState::Spawning {

            commands.insert_resource(NextState(Some(AppState::Running)))

        }

    }

}

Despawn is instant but spawn takes a second. It is not a big file, my pc has 8 years and it is just build version not release but should it lag like that?
(I can't cargo build --release because my pc turns off around 95% of completion, same with blender rendering)

cursive dragon
wary trail
#

could maybe limit your cpu's frequency or power output