LogoPixi’VN

Game storage

How to store, retrieve, and manage persistent and temporary variables in Pixi’VN, including Keyv integration.

What is the game storage? The game storage is a place where you can save variables that you want to keep between game sessions.

It is essential to understand that if variables are not saved in the game memory, the engine will not be able to handle them when you load a save or when you go back.

Additionally, in the game archive you can save any type of variable, except class and function (because they cannot be converted to JSON), such as: string, number, boolean, object, array, etc. If you want to save "flags" (boolean), it is recommended to use the flags functionality, a very high-performance flag management system.

Set

To set a variable in the game storage, use storage.setVariable. This function has the following parameters:

  • name: The name of the variable to set.
  • value: The value of the variable to set.
import { storage } from '@drincs/pixi-vn'

storage.setVariable("myVariable", 42);

Get

To get a variable from the game storage, use storage.getVariable. This function has the following parameters:

  • name: The name of the variable to get.
import { storage } from '@drincs/pixi-vn'

const myVariable = storage.getVariable<number>("myVariable");

Remove

To remove a variable from the game storage, use storage.removeVariable. This function has the following parameters:

  • name: The name of the variable to remove.
import { storage } from '@drincs/pixi-vn'

storage.removeVariable("myVariable");

Temporary storage

In many cases, it is useful to use variables only during a certain period of the narrative. Using normal storage, you would need to manually remove these variables when they are no longer needed, to keep saves light and efficient.

To solve this problem, Pixi’VN has a temporary storage system. Temporary variables initialized in a label will be deleted when the label is closed. If another label is called from it, the temporary variable will still be accessible from the child label. However, if another label is called with jump (so the current label will be closed and the new one started), the temporary variable will no longer be accessible.

Set a temporary variable:

To set a temporary variable, use storage.setTempVariable. This function has the following parameters:

  • name: The name of the variable to set.
  • value: The value of the variable to set.
import { storage } from '@drincs/pixi-vn'

storage.setTempVariable("myTempVariable", 42);

Get a temporary variable:

To get a temporary variable, use the normal storage.getVariable function.

Remove a temporary variable:

To remove a temporary variable, use storage.removeTempVariable. This function has the following parameters:

  • name: The name of the variable to remove.
import { storage } from '@drincs/pixi-vn'

storage.removeTempVariable("myTempVariable");

System variables

In game storage, there are some system variables that are used by the game engine. All system variables start with the prefix ___. So please avoid using this prefix in your variables.

You can get all system variable keys from the SYSTEM_RESERVED_STORAGE_KEYS constant.

keyv-h2

Keyv

The entire storage system was developed using Map, a native JavaScript object, so you can use Keyv to interact with game storage.

What is Keyv? Keyv is a simple key-value storage. It is a very easy-to-use system and very popular in the Node.js community. Keyv can be combined with other libraries, such as Cacheable (Caching for Node.js based on Keyv). You can learn more on the Keyv website.

How to use Keyv with Pixi’VN? You can use Keyv with Pixi’VN by creating a new instance of Keyv and passing the storage object as a parameter.

import { storage } from '@drincs/pixi-vn'
import Keyv from 'keyv';

const keyvStorage = new Keyv({ store: storage.storage });

keyvStorage.get<string>("myValue").then((value) => {
    console.log(value);
});