Skip to content

Rollback-aware state machines #302

@elementbound

Description

@elementbound

✨ Description

State machines are often used to implement complex behavior in a maintainable way. Often these manage not just the current state, but trigger events on state transitions, and even define valid and invalid transitions.

The latter is often handled by detecting a change in state and triggering signals based on the change.

What many of these systems are not prepared for is changing states multiple times in a single frame, and a state change not being a state transition. This breaks multiple projects where state machines are used in conjunction with RollbackSynchronizer.

An example could be provided with a state machine that implements the following:

  • Has a synchronizable state property
  • State callbacks can be tied to the network rollback loop
  • State transitions are triggered as expected, even during rollback

To make sure that the implementation works in real scenarios, an example project should be provided using it, or one of the existing examples should be adjusted.

Use case

State machines could be used for various game mechanics. A common example is player states for movement ( i.e. standing, jumping, landing, sprinting, etc. ).

Being an abstract concept, state machines can be useful regardless of genre.

Distribution

Either as an example or in netfox.extras

Notes

  • Example using a State Machine
    • multiplayer-netfox should be left as-is, as adding a state machine make it too different from multiplayer-simple
    • Forest Brawl's brawler movement might be split into states, although I'd prefer it to stay as-is, to have all the logic in one place
    • A separate example would be the cleanest
  • The use-case is probably common enough to warrant including it in netfox.extras
  • Implementation cues
    • NetworkedStateMachine class
      • Has a semi-private state property
      • State for transition should be set through a method call ( e.g. set_state() )
      • During _rollback_tick, it should grab the previous tick's state from a history buffer
    • NetworkedState class
      • Should include a custom process callback method to be called by the state machine ( e.g. process(), step(), or update() )
      • Should include callbacks for entering and exiting ( e.g. enter(), exit() )
      • Should include a callback to validate transitions ( e.g. can_enter() )

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions