Created
August 4, 2024 17:42
-
-
Save lucidBrot/55322cf9111957e74c72d4275080dd2d to your computer and use it in GitHub Desktop.
mutable bevy query with Optional component
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
[package] | |
name = "wobbler-repro" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
bevy = { version = "0.14.0" } | |
# reduce bevy logging that is "not relevant for an end user" | |
log = { version = "*", features = ["max_level_debug", "release_max_level_warn"] } | |
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
/* | |
[dependencies] | |
bevy = { version = "0.14.0" } | |
# reduce bevy logging that is "not relevant for an end user" | |
log = { version = "*", features = ["max_level_debug", "release_max_level_warn"] } | |
*/ | |
use bevy::prelude::*; | |
#[derive(Component)] | |
struct Person; | |
#[derive(Component, Debug)] | |
struct Name(String); | |
fn my_setup_system(mut commands: Commands) { | |
// One Person has a Name | |
commands.spawn((Person, Name("Alessan".to_string()))); | |
// One Person does not have a Name | |
commands.spawn((Person,)); | |
} | |
// Fetching Name directly: works | |
// Mutation requires a `mut` before the query, before the name in the loop, and before the query in | |
// the loop. | |
// The mut before name is without ampersand because it is about the binding, not the | |
// reference. | |
fn my_update_system(mut query: Query<&mut Name, With<Person>>) { | |
for mut name in &mut query { | |
println!("Hello, {}. I'll call you Bernhard.", name.0); | |
name.0 = "Bernhard".to_string(); | |
} | |
} | |
fn my_last_system(query: Query<&Name, With<Person>>) { | |
for name in &query { | |
println!("Bye, {}", name.0); | |
} | |
} | |
// Fetching Person with optional Name: works | |
fn my_second_system(query: Query<(&Person, Option<&Name>)>) { | |
for (_person,name) in &query { | |
println!("Hello Again, {:?}", name); | |
} | |
} | |
// This does not work! | |
fn my_mut_system(query: Query<(&Person, Option<&mut Name>)>) { | |
for (_person, mut name_option) in &query { | |
// Note: maybe I could even drop both of these ref mut and &mut ? | |
// The confusion_two.rs file seems to indicate so. | |
if let Some(ref mut name) = &mut name_option { | |
println!("Hello a third time, {:?}", name); | |
*name = &Name("Renamedinho".to_string()); | |
} else { | |
println!("Hello, Anon."); | |
} | |
} | |
} | |
// This seems to work | |
fn my_mut_system_two(mut query: Query<(&Person, Option<&mut Name>)>) { | |
for (_person, mut name_option) in &mut query { | |
if let Some(name) = &mut name_option { | |
println!("Hello a third time, {:?}", name); | |
name.0 = "Succeederinho".to_string(); | |
} else { | |
println!("Hello another time, Anon."); | |
} | |
} | |
} | |
fn main() { | |
App::new() | |
.add_systems(Startup, my_setup_system) | |
.add_systems(Update, (my_update_system, my_second_system, my_mut_system, my_mut_system_two, my_last_system).chain()) | |
.run(); | |
} |
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
$ cargo run | |
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.33s | |
Running `target/debug/wobbler-repro` | |
Hello, Alessan. I'll call you Bernhard. | |
Hello Again, Some(Name("Bernhard")) | |
Hello Again, None | |
Hello a third time, Name("Bernhard") | |
Hello, Anon. | |
Hello a third time, Mut(Name("Bernhard")) | |
Hello another time, Anon. | |
Bye, Succeederinho | |
$ # Note that Renamedinho never got printed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment