A class that manages game data, including saving, loading, and exporting data.

The main methods are: DataManager.saveData, DataManager.loadData, and DataManager.exportData. The other methods are used internally, but can be used for more advanced functionality / customization.

Constructors

Methods

  • Adds an event to call when the game data is loaded.

    Parameters

    • event: (() => void)

      The event to call when the game data is loaded.

        • (): void
        • Returns void

    Returns void

    dataManager.addEventOnLoad(() => console.log("Data loaded!"));
    
  • Compresses the given game data to a base64-encoded using lz-string.

    Parameters

    • data: UnknownObject = ...

      The game data to be compressed. Defaults to the current game data.

    Returns string

    The compressed game data and a hash as a base64-encoded string to use for saving.

  • Compiles the given game data to a tuple containing the compressed game data and a hash.

    Parameters

    • data: UnknownObject = ...

      The game data to be compressed. Defaults to the current game data.

    Returns [SaveMetadata, object]

    [hash, data] - The compressed game data and a hash as a base64-encoded string to use for saving.

  • Decompiles the data stored in localStorage and returns the corresponding object.

    Parameters

    • Optionaldata: null | string

      The data to decompile. If not provided, it will be fetched from localStorage using the key ${game.config.name.id}-data.

    Returns null | [SaveMetadata, UnknownObject]

    The decompiled object, or null if the data is empty or invalid.

  • Compiles the game data and prompts the user to download it as a text file using window.prompt. If you want to implement a custom data export, use compileData instead.

    Returns void

  • Gets the data for the given key.

    Parameters

    • key: string

      The key to get the data for.

    Returns unknown

    The data for the given key.

    Set the return value of setData to a variable instead, as that is a getter and provides type checking.

  • Gets the static data for the given key.

    Parameters

    • key: string

      The key to get the static data for.

    Returns unknown

    The static data for the given key.

    Set the return value of setStatic to a variable instead, as that is a getter and provides type checking. Also, static data is basically useless and should not be used. Use variables in local scope instead.

  • Initializes / sets data that is unmodified by the player. This is used to merge the loaded data with the default data. It should be called before you load data. Note: This should only be called once, and after it is called, you should not add new properties to data.

    Returns void

    dataManager.init(); // Call this after setting the initial data.
    
  • Loads game data and processes it.

    Parameters

    Returns null | boolean

    Returns null if the data is empty or invalid, or false if the data is tampered with. Otherwise, returns true.

  • Loads game data and processes it.

    Parameters

    • dataToParse: null | [SaveMetadata, UnknownObject] = ...

      The data to load. If not provided, it will be fetched from localStorage using decompileData.

    • mergeData: boolean = true

      Whether to merge the loaded data with the normal data. Defaults to true. Warning: If set to false, the loaded data may have missing properties and may cause errors.

    Returns null | UnknownObject

    The loaded data.

  • Resets the game data to its initial state and saves it.

    Parameters

    • reload: boolean = false

      Whether to reload the page after resetting the data. Defaults to false. (Reloading may help with some issues with saving data)

    Returns void

  • Saves the game data to local storage under the key ${game.config.name.id}-data. If you don't want to save to local storage, use compileData instead.

    Parameters

    • dataToSave: string = ...

      The data to save. If not provided, it will be fetched from localStorage using compileData.

    Returns void

  • Sets the data for the given key. The getter is a work in progress.

    Type Parameters

    • S extends string

      The key to set the data for.

    • T

      The value to set the data to.

    Parameters

    • key: S

      The key to set the data for.

    • value: T

      The value to set the data to.

    Returns {
        setValue: ((valueToSet: T) => void);
        value: T;
    }

    An object with a single entry of the name of the key and the value of the data. This is a getter and setter.

    • setValue: ((valueToSet: T) => void)

      Use the setter instead.

        • (valueToSet): void
        • Parameters

          • valueToSet: T

          Returns void

    • value: T
    // ! WARNING: Do not destruct the `value` property, as it will remove the getter and setter.
    const testData = dataManager.setData("test", 5);
    console.log(testData.value); // 5
    testData.value = 10; // Also sets the data
    console.log(testData.value); // 10
  • Sets the static data for the given key. This data is not affected by data loading and saving, and is mainly used internally.

    Type Parameters

    • T

    Parameters

    • key: string

      The key to set the static data for.

    • value: T

      The value to set the static data to.

    Returns T

    A getter for the static data.

    Static data is basically useless and should not be used. Use variables in local scope instead.

  • Validates the given data using a hashing algorithm (md5)

    Parameters

    • data: [SaveMetadata, object]

      [hash, data] The data to validate.

    Returns boolean

    Whether the data is valid / unchanged. False means that the data has been tampered with / save edited.