Skip to content

Commit

Permalink
Implement Reflect for State<S> and NextState<S> (bevyengine#9742)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
johanhelsing authored and Ray Redondo committed Jan 9, 2024
1 parent 1a2d99f commit 90ddea7
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions crates/bevy_ecs/src/schedule/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down

0 comments on commit 90ddea7

Please sign in to comment.