Anonymous

Mod Creation/Mod Context API Reference: Difference between revisions

From Melvor Idle
Line 524: Line 524:


== Character Data Storage ==
== Character Data Storage ==
The character storage API can be accessed through the <code>characterStorage</code> property on the root context object.
The character storage API can be accessed through the <code>characterStorage</code> property on the root context object.


=== Limitations ===
=== Limitations ===
The character storage can only be used once a character has been loaded (after lifecycle hook <code>onCharacterLoaded</code>).
The character storage can only be used once a character has been loaded (after lifecycle hook <code>onCharacterLoaded</code>).


Each character can store up to 8,192 bytes (8kb) of data per mod, including keys. Only JSON-serializable data can be stored. This includes primitive types (<code>string</code>, <code>number</code>, <code>boolean</code>) and objects and arrays that contain only primitive types or other objects or arrays that fit this description. This serialization/deserialization is handled automatically.
Each character can store up to 8,192 bytes (8kb) of data per mod, including keys. Only JSON-serializable data can be stored. This includes primitive types (<code>string</code>, <code>number</code>, <code>boolean</code>) and objects and arrays that contain only primitive types or other objects or arrays that fit this description. This serialization/deserialization is handled automatically.


=== <code>setItem(key: string, data: any): void</code> ===
=== setItem(key: string, data: any): void ===
 
Sets a key/value pair in character storage.
Sets a key/value pair in character storage.


Line 541: Line 544:


'''Example'''
'''Example'''
  <nowiki>ctx.characterStorage.setItem('coolThings', ['rocks']);</nowiki>
  <nowiki>ctx.characterStorage.setItem('coolThings', ['rocks']);</nowiki>


=== <code>getItem(key: string): any</code> ===
=== getItem(key: string): any ===
 
Gets a value by its key from character storage.
Gets a value by its key from character storage.


Line 555: Line 560:


'''Example'''
'''Example'''
  <nowiki>ctx.characterStorage.getItem('coolThings'); // returns ['rocks']</nowiki>
  <nowiki>ctx.characterStorage.getItem('coolThings'); // returns ['rocks']</nowiki>


=== <code>removeItem(key: string): void</code> ===
=== removeItem(key: string): void ===
 
Removes a key/value pair by key from character storage.
Removes a key/value pair by key from character storage.


Line 565: Line 572:


'''Example'''
'''Example'''
  <nowiki>ctx.characterStorage.removeItem('coolThings');
  <nowiki>ctx.characterStorage.removeItem('coolThings');
ctx.characterStorage.getItem('coolThings'); // returns undefined</nowiki>
ctx.characterStorage.getItem('coolThings'); // returns undefined</nowiki>


=== <code>clear(): void</code> ===
=== clear(): void ===
 
Removes all key/value pairs from character storage.
Removes all key/value pairs from character storage.


'''Example'''
'''Example'''
  <nowiki>ctx.characterStorage.clear();</nowiki>
  <nowiki>ctx.characterStorage.clear();</nowiki>


91

edits