Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore overwrite capabilities of insert_state #13848

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/bevy_state/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy_ecs::{
schedule::{IntoSystemConfigs, ScheduleLabel},
world::FromWorld,
};
use bevy_utils::tracing::warn;

use crate::state::{
setup_state_transitions_in_world, ComputedStates, FreelyMutableState, NextState, State,
Expand Down Expand Up @@ -70,6 +71,9 @@ impl AppExtStates for SubApp {
exited: None,
entered: Some(state),
});
} else {
let name = std::any::type_name::<S>();
warn!("State {} is already initialized.", name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this is reasonable, although .init_resource doesn't warn for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should 😅

}

self
Expand All @@ -87,6 +91,16 @@ impl AppExtStates for SubApp {
exited: None,
entered: Some(state),
});
} else {
// Overwrite previous state and setup event
MiniaczQ marked this conversation as resolved.
Show resolved Hide resolved
self.insert_resource::<State<S>>(State::new(state.clone()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting that there's some duplication between the two branches of the if-else. Pulling them out of the if-else would probably hurt readability, so I won't suggest that :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of overlap between all the state methods, we can do a cleanup PR later trying to factorize out the common parts

self.world_mut()
.resource_mut::<Events<StateTransitionEvent<S>>>()
.clear();
self.world_mut().send_event(StateTransitionEvent {
exited: None,
entered: Some(state),
});
}

self
Expand All @@ -109,6 +123,9 @@ impl AppExtStates for SubApp {
exited: None,
entered: state,
});
} else {
let name = std::any::type_name::<S>();
warn!("Computed state {} is already initialized.", name);
}

self
Expand All @@ -132,6 +149,9 @@ impl AppExtStates for SubApp {
exited: None,
entered: state,
});
} else {
let name = std::any::type_name::<S>();
warn!("Sub state {} is already initialized.", name);
}

self
Expand Down