forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In response to bevyengine#2023, here is a draft for a PR. Fixes bevyengine#2023 I've added an example to show how to use `WithBundle`, and also to test it out. Right now there is a bug: If a bundle and a query are "the same", then it doesn't filter out what it needs to filter out. Example: ``` Print component initated from bundle. [examples/ecs/query_bundle.rs:57] x = Dummy( <========= This should not get printed 111, ) [examples/ecs/query_bundle.rs:57] x = Dummy( 222, ) Show all components [examples/ecs/query_bundle.rs:50] x = Dummy( 111, ) [examples/ecs/query_bundle.rs:50] x = Dummy( 222, ) ``` However, it behaves the right way, if I add one more component to the bundle, so the query and the bundle doesn't look the same: ``` Print component initated from bundle. [examples/ecs/query_bundle.rs:57] x = Dummy( 222, ) Show all components [examples/ecs/query_bundle.rs:50] x = Dummy( 111, ) [examples/ecs/query_bundle.rs:50] x = Dummy( 222, ) ``` I hope this helps. I'm definitely up for tinkering with this, and adding anything that I'm asked to add or change. Co-authored-by: Carter Anderson <[email protected]>
- Loading branch information
Showing
5 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use bevy::{log::LogPlugin, prelude::*}; | ||
|
||
fn main() { | ||
App::build() | ||
.add_plugin(LogPlugin) | ||
.add_startup_system(setup.system()) | ||
.add_system(log_names.system().label(LogNamesSystem)) | ||
.add_system(log_person_bundles.system().after(LogNamesSystem)) | ||
.run(); | ||
} | ||
|
||
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)] | ||
struct LogNamesSystem; | ||
|
||
#[derive(Debug)] | ||
struct Name(String); | ||
|
||
#[derive(Debug)] | ||
struct Age(usize); | ||
|
||
#[derive(Debug, Bundle)] | ||
struct PersonBundle { | ||
name: Name, | ||
age: Age, | ||
} | ||
|
||
/// Sets up two entities, one with a [Name] component as part of a bundle, | ||
/// and one entity with [Name] only. | ||
fn setup(mut commands: Commands) { | ||
commands.spawn().insert(Name("Steve".to_string())); | ||
commands.spawn().insert_bundle(PersonBundle { | ||
name: Name("Bob".to_string()), | ||
age: Age(40), | ||
}); | ||
} | ||
|
||
fn log_names(query: Query<&Name>) { | ||
info!("Log all entities with `Name` component"); | ||
// this will necessarily have to print both components. | ||
for name in query.iter() { | ||
info!("{:?}", name); | ||
} | ||
} | ||
fn log_person_bundles(query: Query<&Name, WithBundle<PersonBundle>>) { | ||
info!("Log `Name` components from entities that have all components in `PersonBundle`."); | ||
// this should only print `Name("Bob")`. | ||
for name in query.iter() { | ||
info!("{:?}", name); | ||
} | ||
} |