添加 system 函数时遇到的问题
当你在向你的 Bevy 应用程序添加 system 时,有时你会遇到令人困惑的编译器错误。
这些错误看起来像这样:
the trait bound `for<'r, 's, 't0> fn(bevy::prelude::Query<'r, 's, (&'t0 Param)) {my_system}: IntoSystem<(), (), _>` is not satisfied
这是由于你的 system 函数包含不兼容的参数造成的。Bevy 只能接受特殊类型作为 system 参数。
有可能你的错误也会类似下面这样的错误:
the trait bound `Component: WorldQuery` is not satisfied
the trait `WorldQuery` is not implemented for `Component`
this struct takes at most 2 type arguments but 3 type arguments were supplied
这些错误是由错误的 query 查询引起的。
初学者常见的错误
- 使用
&mut Commands
(bevy 0.4 语法)而不是Commands
。 - 使用
Query<MyStuff>
而不是Query<&MyStuff>
或Query<&mut MyStuff>
。 - 使用
Query<&ComponentA, &ComponentB>
而不是Query<(&ComponentA, &ComponentB)>
(忘记这是元组)。 - 直接使用你的资源类型而不使用
Res
或ResMut
。 - 直接使用你的组件类型而没把它们放在
Query
中。 - 在你的函数中使用其他任意的类型。
请注意,Query<Entity>
是正确的用法,因为 Entity ID 很特殊,它不是一个组件。
支持的类型
系统参数只支持以下类型:
Commands
: 使用 commands 来操作 ECSRes<T>
: 对某个 resource 的共享访问ResMut<T>
: 对某个 resource 的独占访问(可写)Option<Res<T>>
: 对一个可能不存在资源的共享访问Option<ResMut<T>>
: 对一个可能不存在资源的独占访问(可写)Query<T, F = ()>
(可以包含最多 15 种类型的元组): 访问 entities and componentsQuerySet
(最多包含4个查询): 解决 冲突的查询Local<T>
: 本地 system 数据EventReader<T>
: 接收 事件EventWriter<T>
: 发送 事件RemovedComponents<T>
: Component 移除检测NonSend<T>
: 对 Non-Send
(限主线程) 数据的共享访问NonSendMut<T>
: 对 Non-Send
(限主线程) 数据的可写访问Entities
: 底层 ECS 元数据:所有 EntitiesComponents
: 底层 ECS 元数据:所有 ComponentsBundles
: 底层 ECS 元数据: 所有 BundlesArchetypes
: 底层 ECS 元数据: 所有 ArchetypesSystemChangeTick
: 底层 ECS 元数据: 用于监测变化的时钟- 包含以上类型中任何一种的元组,元组成员最多不超过 16 个
你的 system 函数总共最多可以有 16 个参数,如果你需要更多参数,用元组把它们包起来解决这个限制。单个元组最多可以包含 16 个成员,但可以无限地嵌套。