mcpField
B
bevy-core-concepts

bevy-core-concepts

github.com/chrisgliddon开发2691 次安装9 周前更新
代码仓库

关于 bevy-core-concepts

面向 Bevy 0.19 应用和插件开发者的核心概念技能,帮助判断系统应在哪个调度阶段运行。内容涵盖 App、Plugin、World、系统排序及延迟命令的注意事项。

主要功能

  1. 01App 与插件设置
  2. 02调度阶段选择指南
  3. 03独占系统注意事项
  4. 04系统排序与循环
  5. 05延迟命令行为
  6. 06Bevy 0.19 迁移提示

适用场景

  1. 01启动 Bevy 应用或插件
  2. 02为系统选择调度阶段
  3. 03诊断调度循环
  4. 04确保后续系统能读取命令

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/chrisgliddon/bevy-skills --skill bevy-core-concepts

安装后在 Claude Code 中输入/bevy-core-concepts确认已加载。

详细介绍

Bevy 0.19 — 核心概念(App、Plugin、Schedule、World)

何时使用此技能

  • 设置新的 Bevy 应用或库插件。
  • 决定系统应属于哪个 Schedule(Startup、Update、FixedUpdate、PostUpdate 等)。
  • 编写需要 &mut World 的独占系统。
  • 遇到 Schedule 循环、歧义诊断级别升高或延迟命令顺序错误。
  • 想知道“X 应该在哪里运行?”

规范模式

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(GamePlugin)
        .run();
}

pub struct GamePlugin;

impl Plugin for GamePlugin {
    fn build(&self, app: &mut App) {
        app.insert_resource(Score(0))
            .add_systems(Startup, spawn_world)
            .add_systems(Update, (read_input, apply_gravity).chain())
            .add_systems(FixedUpdate, simulate_physics)
            .add_systems(PostUpdate, sync_transforms);
    }
}

#[derive(Resource)]
struct Score(u32);

fn spawn_world(mut commands: Commands) {
    commands.spawn((Camera3d::default(), Transform::from_xyz(0.0, 4.0, 8.0)));
}

fn read_input(_keys: Res<ButtonInput<KeyCode>>) {}
fn apply_gravity(_t: Res<Time>) {}
fn simulate_physics(_t: Res<Time<Fixed>>) {}
fn sync_transforms(_q: Query<&mut Transform>) {}

// Exclusive system — full mutable World access, runs alone.
fn rebuild_index(world: &mut World) {
    let count = world.entities().len();
    world.insert_resource(EntityCount(count));
}

#[derive(Resource)]
struct EntityCount(u32);

Schedule 速查表

Schedule用途
Startup一次性设置。在 Update 之前运行一次。
PreUpdate读取输入、网络数据接入、帧开始时的记账工作。
Update每帧游戏逻辑。按渲染帧率运行。游戏系统的默认选择。
PostUpdateUpdate 之后的响应式记账工作(变换传播在此处进行)。
Last必须在 PostUpdate 之后运行的内容(少见)。
FixedFirst / FixedPreUpdate / FixedUpdate / FixedPostUpdate / FixedLast确定性、按 tick 频率执行的工作:物理、网络模拟、游戏状态。每个渲染帧可能运行零次或多次,以追上 Time<Fixed>。

**经验法则:**如果系统必须在相同输入下无论帧率如何都产生相同结果,就将其放入 FixedUpdate。否则放入 Update。

Bevy 0.19 注意事项

  • **SimpleExecutor 已移除。**它过去会在每个系统之后应用延迟命令。剩余的执行器会根据依赖信息安排 ApplyDeferred;如果下游系统必须在同一个 Schedule 中看到命令,请添加 .before、.after 或 .chain()。共享的可变访问会被串行化,但这本身并不能决定哪个系统先运行。
  • **ScheduleBuildError 变体已重命名。**如果你使用 match 匹配它们,请更新:HierarchyLoop → HierarchySort(DiGraphToposortError::Loop(...)),DependencyCycle → DependencySort(DiGraphToposortError::Cycle(...))。
  • **0.19 中配置执行器时需要传入执行器值。**将 set_executor_kind(ExecutorKind::SingleThreaded) 替换为 set_executor(SingleThreadedExecutor::new())(或 MultiThreadedExecutor::new() / default_executor())。
  • **0.19 中 Resource 实现了 Component。**过去仅用 Resource 来排除组件的泛型约束不再有效;请检查范围较广的组件查询和泛型 API。
  • State::set() 现在总会触发状态转换——即使请求的状态与当前状态相同也是如此。若要保留过去“相同时不执行任何操作”的行为,请使用 next_state.set_if_neq(...)。
  • 不要在 Update 中执行渲染世界的处理流程。主世界组件可以在此处准备渲染数据,但 GPU 处理流程应放在渲染 Schedule 中。请参阅 bevy-rendering。
  • 独占系统会阻塞其 Schedule 中的所有内容——只有在确实需要 &mut World 时才使用(场景加载、批量反射、架构迁移)。

另请参阅

评论

支持 Markdown