bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Optional state

Open lee-orr opened this issue 2 years ago • 3 comments

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_mut to world.get_resource/world.get_resource_mut in the run_enter_schedule and apply_state_transition systems and handled the None option.
  • in_state now returns a FnMut(Option<Res<State<S>>>) -> bool + Clone, returning false if the resource doesn't exist.
  • state_exists_and_equals was marked as deprecated, and now just runs and returns in_state, since their bevhaviour is now identical
  • state_changed now takes an Option<Res<State<S>>> and returns false if it does not exist.

I would like to remove state_exists_and_equals fully, but wanted to ensure that is acceptable before doing so.

lee-orr avatar Jan 18 '24 21:01 lee-orr

Will this make it harder to diagnose when you forgot to call add_state?

hymm avatar Jan 18 '24 21:01 hymm

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?

lee-orr avatar Jan 18 '24 22:01 lee-orr

Added a warning to in_state, that runs only the first time

lee-orr avatar Jan 18 '24 22:01 lee-orr

If you have time to use the once macro before this PR gets a second approval, please do so. Otherwise, happy to merge as is.

Done! Thanks :)

lee-orr avatar Jan 19 '24 00:01 lee-orr

Couple nits on comments, looks good otherwise.

@hymm - Thanks for catching those! I applied your suggestions

lee-orr avatar Jan 19 '24 17:01 lee-orr

The PR description does not match. It says Res<Option<State<S>>> instead of Option<Res<State<S>>>, which left me very confused when I read the actual changes.

Sorry about that - fixed

lee-orr avatar Jan 19 '24 19:01 lee-orr