examples/systems/system_startup_system.rs
1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4// Startup systems are systems registered with the EcsOnStart phase, and are
5// only ran during the first frame. Just like with regular phases, custom phases
6// can depend on the EcsOnStart phase (see custom_phases example). Phases that
7// depend on EcsOnStart are also only ran during the first frame.
8//
9// Other than that, startup systems behave just like regular systems (they can
10// match components, can introduce merge points), with as only exception that
11// they are guaranteed to always run on the main thread.
12
13fn main() {
14 let world = World::new();
15
16 // Startup system
17 world
18 .system_named::<()>("Startup")
19 .kind(id::<flecs::pipeline::OnStart>())
20 .run(|mut it| {
21 while it.next() {
22 println!("{}", it.system().name());
23 }
24 });
25
26 // Regular system
27 world.system_named::<()>("Update").run(|mut it| {
28 while it.next() {
29 println!("{}", it.system().name());
30 }
31 });
32
33 // First frame. This runs both the Startup and Update systems
34 world.progress();
35
36 // Second frame. This runs only the Update system
37 world.progress();
38
39 // Output:
40 // Startup
41 // Update
42 // Update
43}
44
45#[cfg(feature = "flecs_nightly_tests")]
46#[test]
47fn test() {
48 let output_capture = OutputCapture::capture().unwrap();
49 main();
50 output_capture.test("system_startup_system".to_string());
51}