Getting started with the Game State Machine sample
Prerequisites
This sample requires the following:
- Unity 6000.7 or later
- TextMeshPro Essential Resources, imported through Window > TextMeshPro > Import TMP Essential Resources.
The sample reads input through either of Unity's two input systems, so the Input System package is optional.
Import the sample
- Open Package Manager
- Select the Graph Toolkit package
- Navigate to the Samples tab
- Import the Game State Machine sample
The Package Manager copies sample files in your Assets folder, that's now ready to use.
Test the sample
- Open
GameFlowStateScene.unity(Assets/Samples/Graph Toolkit/<installed package version>/GameStateMachine Sample/Assets/GameFlowStateScene.unity). The scene contains the endless runner game and aGameFlowRunnerwith the state machine already assigned. - Open
Game Flow State Machine.gameflow, in the same folder. Double-click it to open the state machine editor, and arrange the two windows so you can see both. - Select any state and look at the graph inspector. The fields shown there are the state options of that state's type, such as the
Durationof the Countdown state. - Enter Play Mode.
The game starts on the splash screen and moves to the title screen on its own. From there:
- Press Space or the up arrow, click, or tap the screen to start a run, to jump, and to play again after a game over.
- Press Escape to pause and unpause.
While the game plays, the state machine editor shows its runtime status: the active state is animated, its exit transitions are colored by whether their conditions were met, and each condition gets a true, false or pending icon on its row in the transition inspector. An active transition is held green for a moment so that it is visible. Use the Debug View toggle in the state machine editor's toolbar to turn all of that off or on.
The state machine usually changes state when gameplay code raises a signal. The Main Game state raises one when the player dies, the Ready and Game Over states raise one on a button press, and each is matched by a GameSignal condition on a transition. The name of each signal is a state option, so it can be changed in the graph inspector.
Create your own game flow
- Click Assets > Create > Graph Toolkit Samples > Game Flow State Machine.
- Name the new state machine asset (for example,
My Own Game Flow). - Double-click it to open the state machine editor.
- Right-click in the canvas and create a state of each type the game needs, for example
Splash,ReadyorPause. - Select each state and set its options in the graph inspector.
- Right-click the state the game should start in and create an
Entrytransition on it. Without one, the runner starts in the first state that was authored and logs a warning on import. - Connect the states, and add
GameSignal,TimeInState, or blackboard variable conditions to the transition rules.
Saving the state machine automatically builds the runtime asset and links it to the same disk asset. Drag the asset onto a GameFlowRunner component in a scene to run it.
To add a state behaviour the sample does not cover, write two classes:
- A runtime class that derives from
RuntimeGameState, is marked[Serializable], and overrides the methods you need. - An editor class that derives from
GameState, is marked[Serializable]and[State], declares its options inOnDefineOptions, and returns a new instance of your runtime class fromCreateRuntimeState.