bevy-ecs-queries
Skill16 starsChanged 3 months agoMITView on GitHub
Use when writing `Query<D, F>` with `With`/`Without`/`Or`, detecting `Changed<T>`/`Added<T>`, parallelising with `par_iter_mut`, building a query lens, or fixing Bevy 0.19 generic-query errors involving `IterQueryData` or `SingleEntityQueryData`.
In this repository33 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- bevy-a11ySkill
- bevy-animationSkill
- bevy-assetsSkill
- bevy-audioSkill
- bevy-camerasSkill
- bevy-captureSkill
- bevy-cargo-featuresSkill
- bevy-core-conceptsSkill
- bevy-custom-assetsSkill
- bevy-diagnostics-profilingSkill
- bevy-ecs-componentsSkill
- bevy-ecs-systemsSkill
- bevy-fluentSkill
- bevy-input-actionsSkill
- bevy-migration-0-17-to-0-18Skill
- bevy-migration-0-18-to-0-19Skill
- bevy-pbr-materialsSkill
- bevy-physicsSkill
- bevy-portingSkill
- bevy-renderingSkill
- bevy-save-loadSkill
- bevySkill
- bevy-testingSkill
- bevy-uiSkill
- bevy-vfxSkill
- bevy-voxel-dataSkill
- bevy-voxel-pipelineSkill
- bevy-voxel-runtimeSkill
- bevy-wasm-webgpuSkill
- similarity-rsSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 99
Non-iterable query data uses lending iteration: Keep D: QueryData, call let mut iter = query.iter_mut(), then repeatedly call …
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Bevy 0.19 — ECS Queries
When to use this skill
- Reading or writing components from a system.
- Filtering by presence (
With/Without), alternation (Or), or change detection (Changed/Added). - Parallelising over a large entity set with
par_iter_mut. - Borrowing a subset of a query via a lens (
transmute_lens). - Compiler error mentioning
IterQueryDataorSingleEntityQueryDataafter upgrading to 0.19.
Canonical pattern
use bevy::prelude::*;
#[derive(Component, Default)]
struct Health(f32);
#[derive(Component, Default)]
struct Velocity(Vec3);
#[derive(Component)]
struct Player;
#[derive(Component)]
struct Enemy;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Update, (
move_things,
on_health_changed,
damage_visible_enemies,
integrate_in_parallel,
))
.run();
}
// Sequential iteration. `&` for read, `&mut` for write.
fn move_things(time: Res<Time>, mut q: Query<(&Velocity, &mut Transform)>) {
let dt = time.delta_secs();
for (vel, mut tf) in &mut q {
tf.translation += vel.0 * dt;
}
}
// Change detection. `Changed<T>` triggers on insert OR mutation.
// `Added<T>` triggers only on insert.
fn on_health_changed(q: Query<(Entity, &Health), Changed<Health>>) {
for (entity, hp) in &q {
info!("entity {:?} now has {} hp", entity, hp.0);
}
}
// Combined filters. `With`/`Without` constrain entities;
// `Or<(...)>` alternates over filters (not components).
fn damage_visible_enemies(
mut q: Query<&mut Health, (With<Enemy>, Without<Player>, Or<(Added<Enemy>, Changed<Transform>)>)>,
) {
for mut hp in &mut q {
hp.0 -= 1.0;
}
}
// Parallel iteration. Use when N is large (>10k) and per-entity work is non-trivial.
// Cannot use `Commands` or external mutable state — task pool runs items in parallel.
fn integrate_in_parallel(mut q: Query<(&Velocity, &mut Transform)>) {
q.par_iter_mut().for_each(|(vel, mut tf)| {
tf.translation += vel.0 * 0.016;
});
}
// Query lens: temporarily view a query as a narrower one. Useful for
// passing a stricter query into a helper without re-binding the system's
// SystemParam list.
#[allow(dead_code)]
fn use_lens(mut q: Query<(&mut Transform, &Velocity)>) {
// Read-only narrowed view of just the Transform column.
let mut lens = q.transmute_lens::<&Transform>();
let _read_only: Query<&Transform> = lens.query();
}Bevy 0.19 gotchas
- Nested query access added bounds in 0.19. Generic mutable iteration usually needs
D: IterQueryData; single-entity access, query transmutation/join, traversal, and sorting may needD: SingleEntityQueryData. Read-only iteration retains the broader bounds. - Non-iterable query data uses lending iteration. Keep
D: QueryData, calllet mut iter = query.iter_mut(), then repeatedly calliter.fetch_next()when a nested query item may access several entities. EntityMut::get_components_mut::<(&mut A, &mut B)>()is the safe way to grab two&muts out of one entity and now carries aSingleEntityQueryDatabound. It returnsResult; do not reach for unchecked aliasing.Query::get/get_mutreturnsResult, notOption. The error type carries the entity, so don't swallow it with.ok()if you actually need to know why a lookup missed.Or<(With<A>, With<B>)>—Oralternates over filters, not raw component types.Or<(A, B)>does not compile.Changed/Addedare tick-based. They compare against the system's last run, so ordinary run conditions do not lose an intervening change. They report mutation, not semantic inequality; use observers/messages when every occurrence or old/new value matters.- Don't pair
par_iter_mutwithCommands. Spawn/despawn from a sequential system that consumes aResourcequeue written by the parallel one.
See also
bevy-ecs-components— declaring the components queried here.bevy-ecs-systems— using queries insideSystemParamand run conditions.bevy-migration-0-17-to-0-18—EntityMut::get_components_mutand tick-type move.bevy-migration-0-18-to-0-19— nested-query trait bounds and resources in queries.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

