Skip to content
Merged
Changes from all commits
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
5 changes: 5 additions & 0 deletions examples/state/computed_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ impl ComputedStates for InGame {
// Our computed state depends on `AppState`, so we need to specify it as the SourceStates type.
type SourceStates = AppState;

// This is necessary to prevent `setup_game` from running when the app is already in `AppState::InGame`
// and only `paused` and `turbo` are changed
const ALLOW_SAME_STATE_TRANSITIONS: bool = false;
// The compute function takes in the `SourceStates`
fn compute(sources: AppState) -> Option<Self> {
// You might notice that InGame has no values - instead, in this case, the `State<InGame>` resource only exists
Expand All @@ -80,6 +83,7 @@ struct TurboMode;

impl ComputedStates for TurboMode {
type SourceStates = AppState;
const ALLOW_SAME_STATE_TRANSITIONS: bool = false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you verify that you actually don’t need this line:

Suggested change
const ALLOW_SAME_STATE_TRANSITIONS: bool = false;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TurboMode can be re-entered (via activating turbo then pausing). This produces duplicate UI elements that overlap and thus don't cause visual artifacts.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah I see, I overlooked that, thank you for pointing that out


fn compute(sources: AppState) -> Option<Self> {
match sources {
Expand Down Expand Up @@ -107,6 +111,7 @@ enum IsPaused {

impl ComputedStates for IsPaused {
type SourceStates = AppState;
const ALLOW_SAME_STATE_TRANSITIONS: bool = false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you verify that you actually don’t need this line:

Suggested change
const ALLOW_SAME_STATE_TRANSITIONS: bool = false;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

While IsPaused cannot be re-entered it is tied to an OnEnter system that performs command.spawn calls like the other computed states. This line could be removed and retain proper behavior but I believe the example is stronger by keeping it.


fn compute(sources: AppState) -> Option<Self> {
// Here we convert from our [`AppState`] to all potential [`IsPaused`] versions.
Expand Down