simple-visual-novel
    Preparing search index...

    Class StateManager

    Manages game state including variables and scene history.

    The StateManager handles all persistent game state, including:

    • Custom game variables (flags, counters, player choices, etc.)
    • Current scene tracking
    • Scene navigation history for back functionality

    State is stored in memory and can be accessed for save/load functionality.

    StateManager

    const stateManager = new StateManager("intro");

    // Set and get variables
    stateManager.setVariable("hasKey", true);
    stateManager.setVariable("gold", 100);

    const hasKey = stateManager.getVariable("hasKey"); // true
    const allVars = stateManager.allVariables; // { hasKey: true, gold: 100 }

    // Scene navigation
    stateManager.currentSceneId = "chapter1";
    const history = stateManager.sceneHistory; // ["intro"]
    Index

    Constructors

    • Creates a new StateManager instance.

      Parameters

      • OptionalinitialSceneId: string | null = null

        Initial scene ID

      Returns StateManager

    Accessors

    • get allVariables(): Record<string, any>

      Gets all game variables.

      Returns Record<string, any>

      Copy of all variables

    • get currentSceneId(): string | null

      Gets the current scene ID.

      Returns string | null

      The ID of the currently active scene, or null if none

    • set currentSceneId(sceneId: string | null): void

      Sets the current scene ID and adds previous scene to history.

      Parameters

      • sceneId: string | null

        The scene ID to set

      Returns void

    • get sceneHistory(): string[]

      Gets a copy of the scene navigation history.

      The history contains scene IDs in the order they were visited, with the most recently visited scene at the end.

      Returns string[]

      Array of previously visited scene IDs

    • get state(): GameState

      Gets a shallow copy of the current game state.

      This returns a copy to prevent external mutation of the internal state. Useful for save functionality or debugging.

      Returns GameState

      A copy of the current game state

    Methods

    • Clears the scene navigation history.

      This can be useful when starting a new game or when you don't want players to be able to navigate back to previous scenes.

      Returns void

    • Gets a game variable.

      Parameters

      • key: string

        Variable name

      Returns any

      The variable value, or undefined if not set

    • Sets a game variable.

      Parameters

      • key: string

        Variable name

      • value: any

        Variable value

      Returns void