Phaser API Documentation

  Version: 
set(key, data)

Description:

Sets a value for the given key. If the key doesn't already exist in the Data Manager then it is created.

data.set('name', 'Red Gem Stone');

You can also pass in an object of key value pairs as the first argument:

data.set({ name: 'Red Gem Stone', level: 2, owner: 'Link', gold: 50 });

To get a value back again you can call get:

data.get('gold');

Or you can access the value directly via the values property, where it works like any other variable:

data.values.gold += 50;

When the value is first set, a setdata event is emitted.

If the key already exists, a changedata event is emitted instead, along an event named after the key. For example, if you updated an existing key called PlayerLives then it would emit the event changedata-PlayerLives. These events will be emitted regardless if you use this method to set the value, or the direct values setter.

Please note that the data keys are case-sensitive and must be valid JavaScript Object property strings. This means the keys gold and Gold are treated as two unique values within the Data Manager.

Parameters:

name type description
key string | object

The key to set the value for. Or an object or key value pairs. If an object the data argument is ignored.

data *

The value to set for the given key. If an object is provided as the key this argument is ignored.

Returns:
Description:

This DataManager object.

Inherited from: Phaser.Data.DataManager#set
Since: 3.0.0
Source: src/data/DataManager.js (Line 215)