Latest commit 1d3f6e4
固定时间步长
相关的官方文档:[fixed_timestep]
如果要在固定的时间间隔内执行某些代码(例如物理更新),可以用 Bevy 的 FixedTimestep 运行标准。
use bevy::core::FixedTimestep; // 时间步长表示每秒运行 SystemSet 的次数 // 本例中,TIMESTEP_1 每秒一次,TIMESTEP_2 每秒两次 const TIMESTEP_1_PER_SECOND: f64 = 60.0 / 60.0; const TIMESTEP_2_PER_SECOND: f64 = 30.0 / 60.0; fn main() { App::new() .add_system_set( SystemSet::new() // 每秒打印一次 “hello world” .with_run_criteria(FixedTimestep::step(TIMESTEP_1_PER_SECOND)) .with_system(slow_timestep) ) .add_system_set( SystemSet::new() // 每秒打印两次 “goodbye world” .with_run_criteria(FixedTimestep::step(TIMESTEP_2_PER_SECOND)) .with_system(fast_timestep) ) .run(); } fn slow_timestep() { println!("hello world"); } fn fast_timestep() { println!("goodbye world"); }
状态
可以访问 FixedTimesteps 资源来检查固定时间步长跟踪器的当前状态,从而知道距离下次触发还剩多少时间,或者超过了多少时间。需要标记固定时间步长。
请查看官方示例。
注意事项
请注意,由于固定时间步长是用运行标准实现的,因此它不能与其他运行标准(例如状态)结合使用。这种冲突使得固定时间步长无法适用于大多数项目,因为这些项目需要依靠状态来实现主菜单、加载屏幕等功能。
todo:不太懂,大意是固定时间步长不准确 Also, note that your systems are still called as part of the regular frame-update cycle, along with all of the normal systems. So, the timing is not exact.
FixedTimestep 运行标准只是检查自上次运行系统以来经过的时间,并根据需要决定是否在当前帧内运行,或者多次运行。
危险!丢失事件!
默认情况下,Bevy 的事件是不可靠的:事件只持续 2 帧,之后就会丢失。如果用了固定时间步长的系统需要接收事件,请注意,帧率高于固定时间步长的 2 倍时,可能会错过某些事件。一个解决办法是手动清除时间,这样可以控制事件持续多长时间,但忘记清除事件会浪费内存甚至导致内存泄漏。