The object that this DataManager belongs to.
OptionaleventEmitter: EventEmitter
The DataManager's event emitter.
Return the total number of entries in this Data Manager.
The DataManager's event emitter.
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.
The data list.
The object that this DataManager belongs to.
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 changedata 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.
Destroys this Data Manager. All stored data is deleted, all event listeners are removed,
and the reference to the parent object is cleared. This is called automatically when the
parent emits a destroy event.
Passes all data entries to the given callback. The callback is invoked for every entry in the Data Manager, receiving the parent object, the key, the value, and any additional arguments provided to this method.
The function to call.
Optionalcontext: any
Value to use as this when executing callback.
Additional arguments that will be passed to the callback, after the game object, key, and data.
This Data Manager instance.
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.
The key of the value to retrieve, or an array of keys.
The value belonging to the given key, or an array of values, the order of which will match the input array.
Retrieves all data values in a new object.undefined
All data values.
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.
The key to check.
Returns true if the key exists, otherwise false.
Increases a value for the given key. If the key doesn't already exist in the Data Manager then it is created with a value of 0 before being increased.
When the key is first created, a setdata event is emitted. If the key already exists, a changedata event
and a changedata-key event are emitted instead, where key is replaced with the actual key name.
The key to change the value for.
Optionalamount: number
The amount to increase the given key by. Pass a negative value to decrease the key. Default 1.
This Data Manager instance.
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.
The data to merge.
Optionaloverwrite: boolean
Whether to overwrite existing data. Defaults to true. Default true.
This Data Manager instance.
Retrieves the data associated with the given 'key', deletes it from this Data Manager, then returns it.
The key of the value to retrieve and delete.
The value of the given key.
Queries the DataManager for the values of keys matching the given regular expression.
A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).
The values of the keys matching the search string.
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' ]);
The key to remove, or an array of keys to remove.
This Data Manager instance.
Delete all data in this Data Manager and unfreeze it.undefined
This Data Manager instance.
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.
The key to set the value for. Or an object of key value pairs. If an object the data argument is ignored.
Optionaldata: any
The value to set for the given key. If an object is provided as the key this argument is ignored.
This Data Manager instance.
Freeze or unfreeze this Data Manager. A frozen Data Manager will block all attempts to create new values or update existing ones.
Whether to freeze or unfreeze the Data Manager.
This Data Manager instance.
Toggles a boolean value for the given key. If the key doesn't already exist in the Data Manager then it is created with a value of false before being toggled.
When the key is first created, a setdata event is emitted. If the key already exists, a changedata event
and a changedata-key event are emitted instead, where key is replaced with the actual key name.
The key to toggle the value for.
This Data Manager instance.
The Data Manager provides a way to store, retrieve, and manage arbitrary key-value data on any Game Object, System, or Plugin. Each entry is stored by a string key and can hold any value type.
Data changes are communicated through events: a
setdataevent fires when a new key is created, andchangedata/changedata-keyevents fire when an existing value is updated. This makes the Data Manager well suited for driving UI, triggering game logic, or syncing state between systems.The parent object must either extend
EventEmitterdirectly, or expose a property calledeventsthat is an instance ofEventEmitter.