Storage
Very simple way to get and set values, which will persist when the browser is closed also.
Storage wraps around HTML5 Web Storage, which is well-supported across browsers and platforms, but limited to 5MB total storage per domain.
Storage is also available for node, which is permanently persisted to the ./localStorage
folder - take care of removing entries. Note that multiple Crafty instances use the same storage, so care has to be taken not to overwrite existing entries.
Methods
Crafty.storage()
Crafty.storage(String key)
- key
a key you would like to get from the storage.
- [Returns]
The stored value, or
null
if none saved under that key exists
Crafty.storage(String key, String value)
- key
the key you would like to save the data under.
- value
the value you would like to save.
Crafty.storage(String key, [Object value, Array value, Boolean value])
- key
the key you would like to save the data under.
- value
the value you would like to save, can be an Object or an Array.
Crafty.storage
is used synchronously to either get or set values.
You can store booleans, strings, objects and arrays.
Note: Because the underlying method is synchronous, it can cause slowdowns if used frequently during gameplay. You should aim to load or save data at reasonable times such as on level load, or in response to specific user actions.
Note: If used in a cross-domain context, the localStorage might not be accessible.
Example
Get an already stored value
var playername = Crafty.storage('playername');
Example
Save a value
Crafty.storage('playername', 'Hero');
Example
Test to see if a value is already there.
var heroname = Crafty.storage('name');
if(!heroname){
// Maybe ask the player what their name is here
heroname = 'Guest';
}
// Do something with heroname
Crafty.storage.remove()
Crafty.storage.remove(String key)
- key
a key where you will like to delete the value of.
Generally you do not need to remove values from localStorage, but if you do store large amount of text, or want to unset something you can do that with this function.
Example
Get an already stored value
Crafty.storage.remove('playername');