🎨
UVC
  • Introduction
  • Installation
    • Installing in New Project
    • Installing in Existing Project - SCK
    • Installing in Existing Project - MCK
  • Space Combat Kit Tour
  • Mech Combat Kit Tour
  • Player Setup
  • Vehicle Framework
    • Game Agents
    • Vehicles
    • Modules
      • Module Mounts
      • Modules
      • Module Types
      • Module Attachments
      • Display Module On UI
      • Module Managers
  • Input
    • General Input
    • Vehicle Input
    • Rewired Integration
  • Player/AI
  • Spaceships
    • Speed UI
    • Space Fighters
    • Capital Ships
  • Mechs
    • Speed UI
  • Vehicle Control Animations
  • Character Enter/Exit
  • Camera
    • Camera Setup
    • Vehicle Setup
    • Secondary Cameras
    • Death Camera
  • Weapons
    • Framework
      • Triggerables System
      • Weapon System
    • Vehicle Setup
      • Vehicle Weapons
      • Cursor Aiming
    • Projectile Weapons
    • Beam Weapons
    • Missile Weapons
    • Resource Handling (Ammo/Heat)
    • Turrets
    • HUD Weapon Info
    • Health Modifier Volumes
  • Radar
    • Radar Setup
    • Target Setup
    • Target Selectors
    • Trackable Types
    • Improving Radar Performance
    • Target Notification
    • Radar Audio
  • Health
    • Health Framework
      • Health
      • Damageable
      • Damage Receiver
      • Health Modifier
      • Health Types
      • Health Modifier Types
      • Surface Types
    • Vehicle Setup
    • Damageable Object Setup
    • Detonators
    • Health Recharge
    • Health UI
    • Energy Shield
    • Damageable Module Linkers
  • HUD
    • HUD Basics
    • HUD Manager Setup
    • Custom HUD Components
    • HUD Module Display
    • Camera View Management
    • HUD Cursor / Reticle
    • Weapon UI
    • Resource Container UI
    • 2D Radar
    • 3D Radar
    • Target Boxes
    • Target Holograms
    • Minimaps
    • HUD Distance Lookup
  • Loadout
  • AI - Space Combat Kit
    • AI Setup
    • AI Spaceship Behaviours
  • AI - Mech Combat Kit
  • Game States
  • Teams
  • Floating Origin
  • Object Pooling
  • Menus
    • Creating Menus
    • Button Controllers
  • Rumbles
  • Rigidbody Characters [WIP]
  • Utilities [WIP]
    • Object Triggers
    • Shadow Caster Doubles
    • Gimbals
    • Game State Post Process Enabler
  • Objectives
  • Resources System
    • Resource Containers
    • Resource Handlers
    • Resource Container Triggers
    • Module/Vehicle Resource Usage
    • UI
  • Cutscenes
  • URP Upgrading
Powered by GitBook
On this page
  • Scene Setup
  • Creating Game States
  • Setting The Game State
  • Getting The Current Game State
  • Helpers

Was this helpful?

Game States

Sometimes, you game changes context in a way that influences how everything works across the entire scene. For example, when a player is playing your game and they go to the pause menu, the game input and audio needs to pause, many different scripts need to temporarily stop running code, etc. Managing all of this can be difficult without organizing your game into clearly separated states.

With the Game State system, you can easily organize your game into distinct states and switch between them. The game can only be in one state at any given time. This makes it easy to understand what should be happening, and to know which code is supposed to be running and when.

Scene Setup

To begin, add a Game State Manager component anywhere in your scene.

This is the component that enforces and stores the current game state. It is a singleton, which means there must be only one of them in the scene at any time.

Creating Game States

To create a new game state:

  1. Go to the Game State Manager, open the inspector, and add an entry to the Game States list

  2. Go down to the Project tab and right click > Create > VSX > Game State and rename the file to something that describes the game state

  3. Drag the game state file you created into the Game State field in the inspector of the new game state you created in Step 1.

  4. Customize the rest of the values in the inspector.

You have successfully created a new game state you can switch to.

Setting The Game State

There are multiple ways to set the game state.

Starting Game State

To set the starting game state, which is the game state that the game will go to when the scene starts, set the Starting Game State field in the inspector of the Game State Manager inspector.

Changing Game State

To change the game state during gameplay, simply use the following code:

// Enter a new game state
GameStateManager.Instance.EnterGameState(someGameState);

Getting The Current Game State

To get the current game state, use the following code:

// Get the current game state
GameState currentGameState = GameStateManager.Instance.CurrentGameState;

Helpers

To help you easily manage different aspects of your game according to the current game state without having to write code, there are a couple of helper scripts you can use.

Game State Enabler

The Game State Enabler component provides an event that you to add functions to in the inspector to be called when a specific game state (or one of multiple 'compatible' game states) is entered.

To set it up:

  1. Add a Game State Enabler component on any object in the scene

  2. Set the list of 'Compatible Game States'

  3. Add your functions to the On Compatible Game State Entered and On Incompatible Game State Entered events

Game State Enter

The Game State Enter component is designed as a way to easily enter a new game state from a Unity Event, without having to link to the Game State Manager directly. This means, for example, that you can set up some code to enter a new Game State on a prefab that isn't in the scene.

To set this up:

  1. Add a Game State Enter component to the object

  2. Set the Game State you want to go to in the inspector

  3. Add the Game State Enter component's Enter Game State function to a Unity Event or call it from any script to enter the game state

PreviousAI - Mech Combat KitNextTeams

Last updated 2 years ago

Was this helpful?