Optional state
Objective
Adjust bevy internals to utilize Res<Option<State<S>>> instead of Res<State<S>>, to allow for adding/removing states at runtime and avoid unexpected panics.
As requested here: https://github.com/bevyengine/bevy/pull/10088#issuecomment-1869185413
Changelog
- Changed the use of
world.resource/world.resource_muttoworld.get_resource/world.get_resource_mutin therun_enter_scheduleandapply_state_transitionsystems and handled theNoneoption. -
in_statenow returns aFnMut(Option<Res<State<S>>>) -> bool + Clone, returningfalseif the resource doesn't exist. -
state_exists_and_equalswas marked as deprecated, and now just runs and returnsin_state, since their bevhaviour is now identical -
state_changednow takes anOption<Res<State<S>>>and returnsfalseif it does not exist.
I would like to remove state_exists_and_equals fully, but wanted to ensure that is acceptable before doing so.
Will this make it harder to diagnose when you forgot to call add_state?
It will make it a bit harder to diagnose, yeah. I'd be in favor of a debounced warning personally.
is there a pre-existing way to debounce a warning?
Added a warning to in_state, that runs only the first time
If you have time to use the
oncemacro before this PR gets a second approval, please do so. Otherwise, happy to merge as is.
Done! Thanks :)
Couple nits on comments, looks good otherwise.
@hymm - Thanks for catching those! I applied your suggestions
The PR description does not match. It says
Res<Option<State<S>>>instead ofOption<Res<State<S>>>, which left me very confused when I read the actual changes.
Sorry about that - fixed