Przejdź do treści
Biblioteka skilli
Programowanie · Budowanie i kod

bevy-ecs-components

/bevy-ecs-components

Obsługuje komponenty ECS w Bevy 0.19: derive Component, wymagane komponenty #[require], obserwatory On<E>, wybór magazynu Table lub SparseSet, hooki cyklu życia i migrację zdarzenia Replace.

Repozytorium
bevy-skills ↗
Licencja
MIT
Ostatnia zmiana repozytorium
25.08.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
1
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
0
Przestarzałość

Brak sygnałów. Repozytorium zmienione 25.08.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co robi i kiedy

Obsługuje komponenty ECS w Bevy 0.19: derive Component, wymagane komponenty #[require], obserwatory On<E>, wybór magazynu Table lub SparseSet, hooki cyklu życia i migrację zdarzenia Replace.

Oryginalny opis (EN)

Use when defining `#[derive(Component)]`, declaring required components with `#[require(...)]`, writing observers with `On<E>`, choosing Table versus SparseSet storage, registering `on_add`/`on_discard`/`on_remove` hooks, or migrating `Replace` lifecycle events in Bevy 0.19.

Jak działa

  1. When to use this skill
  2. Canonical pattern
  3. Bevy 0.19 gotchas
  4. See also
Sekcje z nagłówków SKILL.md, w oryginale · 90 linii, 502 słów
Podgląd SKILL.md
---
name: bevy-ecs-components
description: Use when defining `#[derive(Component)]`, declaring required components with `#[require(...)]`, writing observers with `On<E>`, choosing Table versus SparseSet storage, registering `on_add`/`on_discard`/`on_remove` hooks, or migrating `Replace` lifecycle events in Bevy 0.19.
license: MIT
compatibility: opencode,claude-code,cursor
metadata:
  tier: "1"
  area: ecs
  bevy_version: "0.19"
---

# Bevy 0.19 — ECS Components

## When to use this skill

- Defining a new `Component` for game state.
- Bundling components via `#[require(...)]` (the modern replacement for "bundles").
- Reacting to component lifecycle: spawning, despawning, inserting, removing.
- Observers — reacting to entity-targeted events with `On<E>`.
- Choosing storage: Table (default, fast iteration) vs SparseSet (fast add/remove).

## Canonical pattern

```rust
use bevy::prelude::*;

// 1. Plain components.
#[derive(Component, Default)]
struct Health(f32);

#[derive(Component)]
struct Velocity(Vec3);

// 2. Required components — spawning `Player` auto-spawns the rest.
//    `#[require]` accepts `Type` (Default), `Type(args)` (tuple constructor),
//    or `Type = expression`.
#[derive(Component)]
#[require(Health = Health(100.0), Velocity = Velocity(Vec3::ZERO), Transform)]
struct Player;

// 3. Sparse storage for components added/removed every frame (e.g. tags
//    flipped by gameplay). Default Table storage is faster to iterate.
#[derive(Component)]
#[component(storage = "SparseSet")]
struct Stunned;

// 4. An entity-targeted event reacted to by observers.
#[derive(EntityEvent)]
struct Damage {
    entity: Entity, // EntityEvent requires an `entity` field.
    amount: f32,
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, spawn_player)
        .add_systems(Update, deal_damage)
        .add_observer(on_damage)
        .run();
}

fn spawn_player(mut commands: Commands) {
    commands.spawn(Player);
}

fn deal_damage(mut commands: Commands, query: Query<Entity, With<Player>>) {
    for entity in &query {
        commands.trigger(Damage { entity, amount: 10.0 });
    }
}

// Observer parameter is `On<E>`, not `Trigger<E>` (renamed in 0.17, PR #19596).
fn on_damage(damage: On<Damage>, mut query: Query<&mut Health>) {
    let event = damage.event();
    if let Ok(mut hp) = query.get_mut(event.entity) {
        hp.0 -= event.amount;
    }
}
```

## Bevy 0.19 gotchas

- **`Trigger<E>` is gone.** Observer params are `On<E>` in 0.17+. Methods: `event()`, `event_mut()`, `observer()`, `original_event_target()`, `propagate(bool)`.
- **`EntityEvent::set_target`** requires `use bevy::ecs::entity::SetEntityEventTarget;` — not in the prelude.
- **Storage choice is irrevocable**: it is compiled into the component. SparseSet is designed for frequent insertion/removal; Table is the default and usually iterates faster. Benchmark the real workload before changing storage.
- **`#[require(T)]` runs `T::default()`**. If `T: !Default`, use `#[require(T = expression)]` or `#[require(T = T::new(...))]`.
- **Required components are recursive.** If `A` requires `B` and `B` requires `C`, inserting `A` also inserts `C`. A constructor specified directly by `A` wins over an inherited constructor; requirement cycles are invalid.
- **`Replace` became `Discard` in 0.19.** Use hooks `on_add`, `on_insert`, `on_discard`, and `on_remove`; the derive attribute is `#[component(on_discard = path)]`.
- **Hooks** run inside `World` mutations, cannot take arbitrary `SystemParam`s, and cannot despawn the entity they fire on. Use observers when you need flexibility.
- **`Resource` is now a `Component` subtrait.** Do not derive both. Each resource value lives as a component on a resource entity, and inserting another value of the same resource type can move which entity owns the singleton; keep ordinary entity components and global resources as distinct types unless that behavior is intentional.
- **`Bundle` derive still exists** but most use-cases are better served by `#[require(...)]` on a "marker" component, which keeps the spawn surface ergonomic.

## See also

- `bevy-ecs-queries` — reading components back out.
- `bevy-ecs-systems` — observers are themselves systems.
- `bevy-migration-0-17-to-0-18` — full `Trigger`→`On` and event→message rename map.
- `bevy-migration-0-18-to-0-19` — `Discard` lifecycle events and resources-as-components.

Wersje

RepozytoriumWynikLicencja
Ta strona · chrisgliddon/bevy-skills100MIT
ianpurton/disposable-heroes100MIT

Migawka bazy z 13.11.2026 · wersja paczki f7d49a73c6f3 (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100bevy-master
    Stanowi centralny przewodnik po Bevy ECS od wersji 0.20: pomaga zacząć grę, wyznaczyć granice ECS i wybrać odpowiedni skill Bevy.
  • 100bevy
    Wspiera projekty silnika gry Bevy: architekturę ECS, wzorce komponentów, kolejność systemów, interfejs UI i pułapki.
  • 100bevy-ecs
    Układa aplikację Bevy wokół Entity Component System: plugins, komponenty i zasoby, systemy z Query, Res i Commands, filtrowanie i kolejność systemów oraz zasób Time.
  • 100bevy-game-engine
    Pomaga budować gry w silniku Bevy w Rust: ECS, renderowanie, wejście i zarządzanie zasobami, w tym encje, komponenty i systemy oraz gry 2D i 3D.
  • 100bevy-ecs-patterns
    Pomaga w trudniejszych zagadnieniach Bevy ECS: złożone zapytania, harmonogram systemów, wykrywanie zmian i strojenie wydajności przy optymalizacji architektury gier.
  • 100bevy-ecs-composition
    Składa encje w Bevy 0.20 z małych komponentów i systemów: spawn krotek przez Commands, filtrowanie Query z With, porządkowanie zapisów przez chain i podpinanie ChildOf.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.