|
1 |
| -<!-- Add DefaultQueryFilters --> |
2 |
| -<!-- https://github.com/bevyengine/bevy/pull/13120 --> |
| 1 | +How would go about marking an entity as disabled in an ECS: unseen-by-default and uninteractable? |
| 2 | +Simply deleting it is definitely reliable, but it's relatively expensive, throws away the data, and makes it hard to enable the entity again. |
| 3 | +You could move it into a shadow [`World`], but that's esoteric, expensive, and makes it challenging to fetch any data off of it and pass it back into the main [`World`]. |
3 | 4 |
|
4 |
| -<!-- TODO --> |
| 5 | +What about adding a simple marker component? That seems easy enough: make a unit struct, insert it, and then use `Without<Disabled>` in all of the relevant queries! |
| 6 | + |
| 7 | +While that approach works at first, it quickly becomes frustrating. Did you forget to add the boilerplate somewhere? |
| 8 | +Do you vendor all of your dependencies and change their queries too? What if you're making a library? Do all of *your* users need to remember to filter out disabled entities too? |
| 9 | + |
| 10 | +Conceptually though, this seems like the right idea: simply hide the entity from queries and systems that aren't looking for it. |
| 11 | +To make this pattern less frustrating Bevy 0.16 introduces the idea of **default query filters**. |
| 12 | + |
| 13 | +These do what they say on the tin: every query will act as if they have a `Without<Disabled>` filter, unless they explicitly mention [`Disabled`] (generally via a `With<Disabled>` or `Option<&Disabled>` argument). |
| 14 | +Because this machinery was already built, Bevy allows users (and libraries) to define their own disabling components, |
| 15 | +which can be registered via [`App::register_disabling_component`]. |
| 16 | +Having multiple distinct disabling components can be useful if you want each form of disabling to have its own semantics (or custom behavior!): you might use this feature for hiding networked entities, freezing entities in an off-screen chunk, creating a collection of prefab entities loaded and ready to spawn or something else entirely. |
| 17 | + |
| 18 | +To disable or enable an entity, simply remove or add the disabling component of your choice. It's that easy! |
| 19 | +Note that for maximum control and explicitness, only the entities that you directly add disabling components to are disabled: their children or other related entities are not automatically disabled! |
| 20 | +This can lead to strange bugs, so in most cases, you should either be careful to call [`Commands::insert_recursive`] and [`Commands::remove_recursive`] or add a hook or observer to get automatic hierarchy-aware disabling. |
| 21 | + |
| 22 | +[`World`]: https://dev-docs.bevyengine.org/bevy/ecs/prelude/struct.World.html |
| 23 | +[`Disabled`]: https://dev-docs.bevyengine.org/bevy/ecs/entity_disabling/struct.Disabled.html |
| 24 | +[`Commands::insert_recursive`]: https://dev-docs.bevyengine.org/bevy/prelude/struct.EntityCommands.html#method.insert_recursive |
| 25 | +[`Commands::remove_recursive`]: https://dev-docs.bevyengine.org/bevy/prelude/struct.EntityCommands.html#method.remove_recursive |
0 commit comments