Phaser API Documentation

  Version: 
Filter

The Data Component features a means to store pieces of data specific to a Game Object, System or Plugin. You can then search, query it, and retrieve the data. The parent must either extend EventEmitter, or have a property called events that is an instance of it.

Constructor:

new DataManagerPlugin(scene)

Parameters:

name type description
scene Phaser.Scene

A reference to the Scene that this DataManager belongs to.

Since: 3.0.0

Extends


Members

<private> _frozen: boolean
Focus
Focus

Description:

Whether setting data is frozen for this DataManager.

Type:
boolean
Default: false
Inherited from: Phaser.Data.DataManager#_frozen
Since: 3.0.0
Source: src/data/DataManager.js (Line 99)
Focus
Focus
count: number
Focus
Focus

Description:

Return the total number of entries in this Data Manager.

Type:
number
Inherited from: Phaser.Data.DataManager#count
Since: 3.0.0
Source: src/data/DataManager.js (Line 673)
Focus
Focus

Description:

The DataManager's event emitter.

Type:
Inherited from: Phaser.Data.DataManager#events
Since: 3.0.0
Source: src/data/DataManager.js (Line 48)
Focus
Focus
freeze: boolean
Focus
Focus

Description:

Gets or sets the frozen state of this Data Manager. A frozen Data Manager will block all attempts to create new values or update existing ones.

Type:
boolean
Inherited from: Phaser.Data.DataManager#freeze
Since: 3.0.0
Source: src/data/DataManager.js (Line 651)
Focus
Focus
list: Object.<string, *>
Focus
Focus

Description:

The data list.

Type:
Object.<string, *>
Default: {}
Inherited from: Phaser.Data.DataManager#list
Since: 3.0.0
Source: src/data/DataManager.js (Line 62)
Focus
Focus
parent: *
Focus
Focus

Description:

The object that this DataManager belongs to.

Type:
*
Inherited from: Phaser.Data.DataManager#parent
Since: 3.0.0
Source: src/data/DataManager.js (Line 39)
Focus
Focus
scene: Phaser.Scene
Focus
Focus

Description:

A reference to the Scene that this DataManager belongs to.

Type:
Since: 3.0.0
Focus
Focus

Description:

A reference to the Scene's Systems.

Type:
Since: 3.0.0
Focus
Focus
values: Object.<string, *>
Focus
Focus

Description:

The public values list. You can use this to access anything you have stored in this Data Manager. For example, if you set a value called gold you can access it via:

this.data.values.gold;

You can also modify it directly:

this.data.values.gold += 1000;

Doing so will emit a setdata event from the parent of this Data Manager.

Do not modify this object directly. Adding properties directly to this object will not emit any events. Always use DataManager.set to create new items the first time around.

Type:
Object.<string, *>
Default: {}
Inherited from: Phaser.Data.DataManager#values
Since: 3.10.0
Source: src/data/DataManager.js (Line 72)
Focus
Focus

Methods

<private> boot()
Focus
Focus

Description:

This method is called automatically, only once, when the Scene is first created. Do not invoke it directly.

Since: 3.5.1
Focus
Focus
destroy()
Focus
Focus

Description:

The Scene that owns this plugin is being destroyed. We need to shutdown and then kill off all external references.

Overrides: Phaser.Data.DataManager#destroy
Since: 3.5.0
Focus
Focus
each(callback, [context], [args])
Focus
Focus

Description:

Passes all data entries to the given callback.

Parameters:

name type arguments description
callback DataEachCallback

The function to call.

context * <optional>

Value to use as this when executing callback.

args * <optional>

Additional arguments that will be passed to the callback, after the game object, key, and data.

Returns:
Description:

This DataManager object.

Inherited from: Phaser.Data.DataManager#each
Since: 3.0.0
Source: src/data/DataManager.js (Line 415)
Focus
Focus
get(key)
Focus
Focus

Description:

Retrieves the value for the given key, or undefined if it doesn't exist.

You can also access values via the values object. For example, if you had a key called gold you can do either:

this.data.get('gold');

Or access the value directly:

this.data.values.gold;

You can also pass in an array of keys, in which case an array of values will be returned:

this.data.get([ 'gold', 'armor', 'health' ]);

This approach is useful for destructuring arrays in ES6.

Parameters:

name type description
key string | Array.<string>

The key of the value to retrieve, or an array of keys.

Returns:
Description:

The value belonging to the given key, or an array of values, the order of which will match the input array.

Type:
  • *
Inherited from: Phaser.Data.DataManager#get
Since: 3.0.0
Source: src/data/DataManager.js (Line 116)
Focus
Focus
getAll()
Focus
Focus

Description:

Retrieves all data values in a new object.

Returns:
Description:

All data values.

Type:
  • Object.<string, *>
Inherited from: Phaser.Data.DataManager#getAll
Since: 3.0.0
Source: src/data/DataManager.js (Line 167)
Focus
Focus
has(key)
Focus
Focus

Description:

Determines whether the given key is set in this Data Manager.

Please note that the 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

The key to check.

Returns:
Description:

Returns true if the key exists, otherwise false.

Type:
  • boolean
Inherited from: Phaser.Data.DataManager#has
Since: 3.0.0
Source: src/data/DataManager.js (Line 577)
Focus
Focus
inc(key, [data])
Focus
Focus

Description:

Increase a value for the given key. If the key doesn't already exist in the Data Manager then it is increased from 0.

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

Parameters:

name type arguments description
key string | object

The key to increase the value for.

data * <optional>

The value to increase for the given key.

Returns:
Description:

This DataManager object.

Inherited from: Phaser.Data.DataManager#inc
Since: 3.23.0
Source: src/data/DataManager.js (Line 282)
Focus
Focus
merge(data, [overwrite])
Focus
Focus

Description:

Merge the given object of key value pairs into this DataManager.

Any newly created values will emit a setdata event. Any updated values (see the overwrite argument) will emit a changedata event.

Parameters:

name type arguments Default description
data Object.<string, *>

The data to merge.

overwrite boolean <optional> true

Whether to overwrite existing data. Defaults to true.

Returns:
Description:

This DataManager object.

Inherited from: Phaser.Data.DataManager#merge
Since: 3.0.0
Source: src/data/DataManager.js (Line 447)
Focus
Focus
pop(key)
Focus
Focus

Description:

Retrieves the data associated with the given 'key', deletes it from this Data Manager, then returns it.

Parameters:

name type description
key string

The key of the value to retrieve and delete.

Returns:
Description:

The value of the given key.

Type:
  • *
Inherited from: Phaser.Data.DataManager#pop
Since: 3.0.0
Source: src/data/DataManager.js (Line 549)
Focus
Focus
query(search)
Focus
Focus

Description:

Queries the DataManager for the values of keys matching the given regular expression.

Parameters:

name type description
search RegExp

A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

Returns:
Description:

The values of the keys matching the search string.

Type:
  • Object.<string, *>
Inherited from: Phaser.Data.DataManager#query
Since: 3.0.0
Source: src/data/DataManager.js (Line 190)
Focus
Focus
remove(key)
Focus
Focus

Description:

Remove the value for the given key.

If the key is found in this Data Manager it is removed from the internal lists and a removedata event is emitted.

You can also pass in an array of keys, in which case all keys in the array will be removed:

this.data.remove([ 'gold', 'armor', 'health' ]);

Parameters:

name type description
key string | Array.<string>

The key to remove, or an array of keys to remove.

Returns:
Description:

This DataManager object.

Inherited from: Phaser.Data.DataManager#remove
Since: 3.0.0
Source: src/data/DataManager.js (Line 480)
Focus
Focus
<private> removeValue(key)
Focus
Focus

Description:

Internal value remover, called automatically by the remove method.

Parameters:

name type description
key string

The key to set the value for.

Returns:
Description:

This DataManager object.

Inherited from: Phaser.Data.DataManager#removeValue
Since: 3.10.0
Source: src/data/DataManager.js (Line 522)
Focus
Focus
reset()
Focus
Focus

Description:

Delete all data in this Data Manager and unfreeze it.

Returns:
Description:

This DataManager object.

Inherited from: Phaser.Data.DataManager#reset
Since: 3.0.0
Source: src/data/DataManager.js (Line 613)
Focus
Focus
set(key, data)
Focus
Focus

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)
Focus
Focus
setFreeze(value)
Focus
Focus

Description:

Freeze or unfreeze this Data Manager. A frozen Data Manager will block all attempts to create new values or update existing ones.

Parameters:

name type description
value boolean

Whether to freeze or unfreeze the Data Manager.

Returns:
Description:

This DataManager object.

Inherited from: Phaser.Data.DataManager#setFreeze
Since: 3.0.0
Source: src/data/DataManager.js (Line 595)
Focus
Focus
<private> setValue(key, data)
Focus
Focus

Description:

Internal value setter, called automatically by the set method.

Parameters:

name type description
key string

The key to set the value for.

data *

The value to set.

Returns:
Description:

This DataManager object.

Inherited from: Phaser.Data.DataManager#setValue
Since: 3.10.0
Source: src/data/DataManager.js (Line 348)
Focus
Focus
<private> shutdown()
Focus
Focus

Description:

The Scene that owns this plugin is shutting down. We need to kill and reset all internal properties as well as stop listening to Scene events.

Since: 3.5.0
Focus
Focus
<private> start()
Focus
Focus

Description:

This method is called automatically by the Scene when it is starting up. It is responsible for creating local systems, properties and listening for Scene events. Do not invoke it directly.

Since: 3.5.0
Focus
Focus
toggle(key)
Focus
Focus

Description:

Toggle a boolean value for the given key. If the key doesn't already exist in the Data Manager then it is toggled from false.

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

Parameters:

name type description
key string | object

The key to toggle the value for.

Returns:
Description:

This DataManager object.

Inherited from: Phaser.Data.DataManager#toggle
Since: 3.23.0
Source: src/data/DataManager.js (Line 321)
Focus
Focus