Skip to content

State machine implementing a state transition graph to use for a classic game scenes flow (in Unity).

Notifications You must be signed in to change notification settings

2-REC/unity-state-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

UNITY STATE ENGINE

State machine implementing a state transition graph to use for a classic game scenes structure.

USING THE ENGINE

ADD THE ENGINE IN PROJECT

There are 2 ways to add the engine to a Unity project:

  1. Copy the content of the "Engine" directory to a new "StateEngine" folder in the "Assets" folder of the Unity project.

  2. Run the script "set_links.bat" in "Examples" to create symbolic links to the engine (and thus not working with a copy)
    => This is useful if working with Git (...)
    ! - The script must be run with admin rights! (and is only for Windows - Linux script coming soon)

USE THE ENGINE

... How to use:

(TODO: OK HERE?)

  • Implement "GlobalManager" & create a prefab

  • Set script order for "GlobalManager" before "default"!
    (to be sure it is executed before "StateController")

  • Implement "GameManager" & create a prefab

  • Set script order for "GameManager" before "default" (just after "GlobalManager")!
    (to be sure it is executed before "StateController")
    ?!-TODO: check:

  • Idem with "LevelManager" (StateController last derived level)
    (for version 2 => move below?)

  • State graphs are defined in "Resources/Xml/game_states.xml" & "Resources/Xml/global_states.xml"
    => The files don't need to be modified, unless some specific states should be added or removed.

  • The name of the different scenes can be set in the 2 graph files.
    TODO: CHECK FOR LEVEL NAMES! ! - Except for the level scenes, which must be "level_", where corresponds to the numbers used in the map scene.

  • For each state, a scene must be created, which satisfies:

    • Its name as defined in the corresponding state graph
    • It must contain a graph management object:
      • Create an empty object, eg: "StateManager"
      • Add a "GlobalManager" (script) with an attached "GlobalStateManager" prefab object
        => This object will be shared between all states as a unique instance.
        TODO: WHY?!? (Except for the "NewGame" scene, that doesn't need one)
      • Add a "GlobalStateControler" (script) and specify its corresponding state, or a script derived from "GlobalStateControler" with the corresponding state name (or param?)
        TODO: OK?! => This object is specific to each state.
      • A "Timer" (script) can be added (global or not depending on graph) if want the state to be left after a period of time
    • It must be added to the project build settings
  • The starting scene must be the one defined as first in the global state graph
    => The "Logo" scene if the graphs were not modified.

  • The desired game data variables must be defined and used where needed:

    • "DifficultyData" & "Xml/values.xml": for fields depending on the difficulty level
    • SessionManager, GameData, etc.: getters and setters for fields to be used in game
    • Typical fields can be known (and potentially displayed) by specific states (by calling "GameData" getters):
      • "DebriefingFail": number of lives remaining
      • "GameOver": number of continues remaining (+points? ...)
      • "Debriefing": points?, ...
      • "Level": lives remaining, points, current life/energy, ...
    • Values need to be saved between levels in SessionManager (when win, lose, both, ...)
      • Values updated by calling GameData setters, and persisted by calling SessionManager setters
        => Decide which fields are saved persistently and in which case (win/lose/both).
    • ...TODO: continue...(?)

Optional: (version 2): - can add the "LevelManager" script (version 2), a "LevelController" script to the manager, and a "UI Canvas" prefab (containing a "HudController" script) to the scene, for generic game stuff (health, points, lives)
- for testing purpose, the prefab object "UI Tests Canvas" can be added to the "UI Canvas" object (as child), and the "LevelManager" object instance must be set in the script public parameters. It will add 4 buttons for the following actions: win level, lose level, get hit, add points.

  • in game states
    • possibility to "load global" (to init the game data, loaded when coming from global graph)
      => allowing to test a game state/scene without to start from global graph.
      (TODO: check if put details here or leave in file...)
      "InitGame" script in "Tests/Game" (see script for use details).

EXAMPLES

STATE ENGINE DEMO

Unity project showcasing how to use the state engine in its most basic way.
It is the minimal implementation required in order to use the engine.

It can be used as a template for the creation of a new game.

Before using the Unity project, the engine must be added to the project.
Look at the section "Adding The Engine" for details.

STATE ENGINE DEMO EXTENDED

Unity project showcasing how to use the state engine.
It implements a game controller with basic game mechanics, such as points, health and a simple UI allowing to simulate game events.

Before using the Unity project, the engine must be added to the project.
Look at the section "Adding The Engine" for details.

About

State machine implementing a state transition graph to use for a classic game scenes flow (in Unity).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages