bevyengine / bevyengine/bevy

Singleton components

Open
#8,944 4 comments 8 reactions 0 assignees View on GitHub
A-ECS C-Feature X-Needs-SME
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## What problem does this solve or what need does it fill?

I'd like to ask for support of singleton/unique components, i.e. components with at most one instance, that can be used for quickly changing markers of entities (like `PrimaryWindow`), optionally carrying data.

## What solution would you like?

A new `ComponentStorage` implementation that stores at most one instance, though API-wise I suggest using distinct parameters in the `#[component]` attribute macro, like `#[component(unique = true)]` instead of `#[component(storage = "UniqueSet")]` because a storage type by itself should not mandate cardinality.
Adding a unique component to an entity will detach it from another entity if set, allowing fast moving markers in one step.

## What alternative(s) have you considered?

* There's some overlap with `Resource`s/`Local`s storing an `Entity` reference, but singleton components integrate better with queries and query filters.
* One can use `Query::get_single` on a `SparseSet`, but this doesn't guarantee true uniqueness of a component.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.