Pause Level

v4.0

Pause Level

Gametime has a value for pauseLevel, an int value which specifies different tiers of pausing in the game. Generally a value of 0 means the game is not paused, and a value higher would imply there is some amount of pause.

While some games may just have two states, other projects will have multiple states, such as a level where enemies and projectiles stop moving, but the player can continue to work with the UI, while a higher pause level may keep the player from working with the UI, and only allow operation of the game settings menu.

Variables

// The active pauseLevel
public int pauseLevel = 0;

// This value is set by the script, and records the most recent pauseLevel
[SerializeField] private int lastPauseLevel = 0;

// If not -1, when this script is awakened, game will pause to this level
public int pauseLevelOnAwake = -1;

// If not -1, game will pause to this level when we change scenes.
public int pauseLevelWhenSceneChanges = -1; 

// If not -1, game will pause to this level when a new scene is loaded.
public int pauseLevelWhenNewSceneLoaded = -1; 

// Gametime will not advance if pauseLevel >= this value.
public int stopTimeOnPauseLevel = 1;

Methods

// Set the pauseLevel to the maximum of pauseLevel or newLevel
// If saveLastPauseLevel is true, lastPauseLevel will be saved only if pauseLevel
// does not equal newLevel.
public void SetPauseLevelMax(int newLevel, bool saveLastPauseLevel = true)

// Set the pauseLevel to the minimum of pauseLevel or newLevel
public void SetPauseLevelMin(int newLevel)

// Reset pauseLevel to the value of lastPauseLevel
public void ResetToLastPauseLevel()

Examples

This example comes from DialoguePanel.cs in the Party Base RPG demo game. When the panel is enabled, the game should set to a pauseLevel that is set on the script itself.

using static InfinityPBR.Modules.Timeboard;

//........

// These values are set in the Inspector
public int pauseLevelOnEnable = 2;
public bool revertPauseLevelOnDisable = true;

// On enable, set the pause level to pauseLevelOnEnable
private void OnEnable() => StartCoroutine(SetPauseLevel());

// We require timeboard, which is a singleton, so ensure it is
// available before we try to utilize it.
private IEnumerator SetPauseLevel()
{
    while (timeboard == null)
        yield return null;

    // Access gametime via timeboard.gametime
    timeboard.gametime.SetPauseLevelMax(pauseLevelOnEnable);
}

// When our panel closes, we reset to the last pause level, whatever that was.
private void OnDisable() => timeboard.gametime.ResetToLastPauseLevel();

Last updated