Implement Reflect for State<S> and NextState<S> (#9742)

# Objective

- Make it possible to snapshot/save states
- Useful for re-using parts of the state system for rollback safe states
- Or to save states with scenes/savegames

## Solution

- Conditionally add the derive if the `bevy_reflect` is enabled

---

## Changelog

- `NextState<S>` and `State<S>` now implement `Reflect` as long as `S`
does.
This commit is contained in:
Johan Klokkhammer Helsing 2023-09-11 21:18:47 +02:00 committed by GitHub
parent 0aa079fcf6
commit 4fe2b1220d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,9 +5,13 @@ use std::ops::Deref;
use crate as bevy_ecs;
use crate::change_detection::DetectChangesMut;
#[cfg(feature = "bevy_reflect")]
use crate::reflect::ReflectResource;
use crate::schedule::ScheduleLabel;
use crate::system::Resource;
use crate::world::World;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::std_traits::ReflectDefault;
pub use bevy_ecs_macros::States;
@ -75,6 +79,11 @@ pub struct OnTransition<S: States> {
///
/// The starting state is defined via the [`Default`] implementation for `S`.
#[derive(Resource, Default, Debug)]
#[cfg_attr(
feature = "bevy_reflect",
derive(bevy_reflect::Reflect),
reflect(Resource, Default)
)]
pub struct State<S: States>(S);
impl<S: States> State<S> {
@ -111,6 +120,11 @@ impl<S: States> Deref for State<S> {
/// Note that these transitions can be overridden by other systems:
/// only the actual value of this resource at the time of [`apply_state_transition`] matters.
#[derive(Resource, Default, Debug)]
#[cfg_attr(
feature = "bevy_reflect",
derive(bevy_reflect::Reflect),
reflect(Resource, Default)
)]
pub struct NextState<S: States>(pub Option<S>);
impl<S: States> NextState<S> {