Class ModSave

maginai.modSave submodule class

Do not instantiate directly; use from maginai.modSave.

You can store and retrieve a json-serializable object for each name to/from the save.
In this document, we call the object 'save object'.
name is the unique key for the corresponding save object; usually, mod's name is used to avoid conflict with other mods' save objects.

Use addSaveObjectHandlers for standard store/retrieve handling.
See the detail and examples of the method.

In few cases, you might need to use getSaveObject, setSaveObject or removeSaveObject directly.
You can call these methods at any time as long as a save is loaded.

Use getSaveObject(name) to obtain the save object for name from the current save.

Use setSaveObject(name, obj) to set obj as a save object for name.

Use removeSaveObject(name) to delete the save object for name.

The changes made by setSaveObject and removeSaveObject will be written to the current save the next time save operation is performed.

Each of these methods throws an exception if no save data is loaded, such as on the title screen.

Notes:

  • Calling addSaveObjectHandlers is the same as:
maginai.events.saveLoading.addHandler((e) => {
const modSaveObject = maginai.modSave.getSaveObject(name);
if (modSaveObject === undefined) {
notFoundHandler(e.isNewGame);
} else {
loadHandler(modSaveObject);
}
});

maginai.events.saveObjectRequired.addHandler(() => {
const modSaveObject = saveHandler();
maginai.modSave.setSaveObject(name, modSaveObject);
});
  • Using the same name results in operation on the same save object. You can use this to extend other mods
  • The save capacity of the browser version of CoAW is about 5MB for two save slots. Therefore, if the save data becomes too large, saving may fail
    • Tip: If tWgm.isL is set to true, the save size will be logged to the dev console when a save operation is performed

Constructors

Properties

isSaveAvailable: boolean

Whether save data is available

saveLoading: ModEvent

Event triggered after a save slot is selected and before loading data from the save to all game objects (such as tWgm.tGameCharactor)

Each mod's save object is available in the handler.

saveObjectRequired: ModEvent

Event triggered when collecting save objects from each mod

Methods

  • Adds handlers for the save object of your mod

    By adding these handlers, you can store and retrieve your mod's save object to/from the save.

    Example:

    // `init.js` of `sample4` mod, which counts how many times you performed saving
    (function () {
    const logger = maginai.logging.getLogger('sample4');

    // Variable for counting saving
    let saveCount;

    maginai.modSave.addSaveObjectHandlers(
    // `name` - Mod's name
    'sample4',

    // `notFoundHandler` - Called when no existing save object for `name`
    (isNewGame) => {
    // Initialize saveCount
    saveCount = 0;
    // Show a message and whether it's a new game
    logger.info(
    `'sample4' is applied to this save for the first time. isNewGame: ${isNewGame}`
    );
    },

    // `loadHandler` - Called when existing save object found for `name`
    (saveObj) => {
    saveCount = saveObj.saveCount;
    // Show the `saveCount` loaded from the save
    logger.info(
    `Save object has been loaded. saveCount:` + saveCount.toString()
    );
    },

    // `saveHandler` - Should return a save object to be written for `name`
    () => {
    // Increment `saveCount` by 1
    saveCount += 1;
    // Show the current `saveCount`
    logger.info(
    `Save object has been saved. saveCount:` + saveCount.toString()
    );
    // Return a new save object
    return { saveCount };
    }
    );
    })();

    Parameters

    • name: string

      Mod's name

    • notFoundHandler: ((isNewGame) => void)

      Called when the selected save is loading, but there's no existing save object for name in it

        • (isNewGame): void
        • Parameters

          • isNewGame: boolean

          Returns void

    • loadHandler: ((saveObject) => void)

      Called when the selected save is loading, and saveObject is the existing save object for name

        • (saveObject): void
        • Parameters

          • saveObject: any

          Returns void

    • saveHandler: (() => any)

      Called just before saving, and it should return a save object for name to be written

        • (): any
        • Returns any

    Returns void

  • Returns the save object corresponding to the key name from the current save data
    If no save object for name exists, returns undefined.

    Parameters

    • name: string

    Returns any

    saveObject

  • Removes the save object corresponding to the key name.
    The removal will be reflected in the current save data the next time a save operation is performed (e.g. when the player selects 'Save' in the menu)

    Parameters

    • name: string

    Returns void

  • Sets obj as a save object corresponding to the key name
    The object will be written to the current save data the next time a save operation is performed (e.g. when the player selects 'Save' in the menu)

    Parameters

    • name: string
    • saveObj: any

    Returns void