Utility to allow data to be saved to a permanent storage solution: IndexedDB, WebSql, localstorage or cookies
.open(String gameName)
Opens a connection to the database. If the best they have is localstorage or lower, it does nothing
Open a database
Crafty.storage.open('MyGame');
.save(String key, String type, Mixed data)
Saves a piece of data to the database. Can be anything, although entities are preferred. For all storage methods but IndexedDB, the data will be serialized as a string During serialization, an entity's SaveData event will be triggered. Components should implement a SaveData handler and attach the necessary information to the passed object
Saves an entity to the database
var ent = Crafty.e("2D, DOM")
.attr({x: 20, y: 20, w: 100, h:100});
Crafty.storage.open('MyGame');
Crafty.storage.save('MyEntity', 'save', ent);
.load(String key, String type)
Loads a piece of data from the database. Entities will be reconstructed from the serialized string
Loads an entity from the database
Crafty.storage.open('MyGame');
Crafty.storage.load('MyEntity', 'save', function (data) { // do things });
.getAllKeys(String type)
Gets all the keys for a given type
Gets all the save games saved
Crafty.storage.open('MyGame');
var saves = Crafty.storage.getAllKeys('save');
.external(String url)
Enables and sets the url for saving games to an external server
Save an entity to an external server
Crafty.storage.external('http://somewhere.com/server.php');
Crafty.storage.open('MyGame');
var ent = Crafty.e('2D, DOM')
.attr({x: 20, y: 20, w: 100, h:100});
Crafty.storage.save('save01', 'save', ent);
Any data a component wants to save when it's serialized should be added to this object. Straight attribute should be set in data.attr. Anything that requires a special handler should be set in a unique property.
Saves the innerHTML of an entity
Crafty.e("2D DOM").bind("SaveData", function (data, prepare) {
data.attr.x = this.x;
data.attr.y = this.y;
data.dom = this.element.innerHTML;
});