#Running a system once

37 messages · Page 1 of 1 (latest)

grizzled shore
#

for some reason i need to run a system once at the beginning of the runtime i would like to keep things simple (with app and shedule)

    // in Setup shedule
    let id :SystemId = commands.register_system(one_shot_system);
    
    // this should be run in Update shedule
    if let Some(id) =   ???
    commands.run_system(id); 
    commands.unregister_system(id); 
    }

how to store the SystemId in Resource (initialization is an issue) or (other technic) in order to run & unregister that system at a chosen time ?
official example are sneaky using a Callback(SystemId) but i don't have it in my scope for compillation maybe a feature is missing...
https://github.com/bevyengine/bevy/blob/main/examples/ecs/one_shot_systems.rs

GitHub

A refreshingly simple data-driven game engine built in Rust - bevyengine/bevy

abstract river
half moat
#

could also use a run condition of 'once'

grizzled shore
# half moat could also use a run condition of 'once'

mind to give me an example ? what i need is to run a system once on update schedule with the minimal foot print in runtime so basically adding a system to the schedule Update stack and once executed only one time he can remove himself from the stack

fiery lake
sterile comet
#

with run_system_cached you don't need to register the system

fiery lake
sterile comet
#

yeah, lets see what is the intent here

#

and if it is not strictly on Startup you can also have a States and have the system on OnEnter(MyState::Start)

grizzled shore
#

that work with a warning ... fn setup_with_commands(mut commands: Commands) { let system_id = commands.register_system(make_visible); commands.spawn((Callback(system_id), A)); } fn make_visible( mut window: Single<&mut Window>, frames: Res<FrameCount>, mut winit_s: ResMut<WinitSettings>, mut commands: Commands, id_sys: Single<&Callback, With<A>>, ) { if frames.0 >= 3 { window.visible = true; }; commands.unregister_system(id_sys.0); }

#

but i get the console spaming:WARN bevy_ecs::error::handler: Encountered an error in command `Enable the debug feature to see the name`: System SystemId(13v0) was not registered...

half moat
#

app.add_systems(Update, your_system.run_if(|mut b: Local<bool>| {let n=*b; *b=true; !n}));

#

oh, can do !core::mem::replace(&mut b, true) instead

grizzled shore
#

this is working very well .add_systems( Update, make_visible.run_if(|mut b: Local<bool>| { let n = *b; *b = true; !n }), )

fiery lake
#

isn’t that just Startup though?

half moat
#

it's firstupdate

grizzled shore
fiery lake
half moat
#

Local is an &mut to a value that's stored in the system state

fiery lake
half moat
grizzled shore
fiery lake
half moat
fiery lake
#

shouldn’t the b be outside the run_if?

fiery lake
#

I see

fiery lake
grizzled shore
fiery lake
#

before Update runs for the first time

fiery lake
half moat
#

odd that they were using the startup schedule to register the system already

grizzled shore
half moat
#

if the default bool was true it could set it false and return the previous value unmodified

grizzled shore