This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
libecho:classes:persistence [2022/05/20 18:27] – lee | libecho:classes:persistence [2023/08/06 19:36] (current) – removed lee | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== LibEcho.Persistence ====== | ||
- | |||
- | Most web browsers only allow for 10 megs (though some are as low as 5) of on-disk storage per origin. This not only has to contain the current game state, but also all previous game states in the game history. This is a particular problem with very large and/or complicated Twine games. | ||
- | |||
- | In an attempt to minimize this issue, LibEcho uses a defaults and delta Persistence system. " | ||
- | |||
- | All persistent objects must ultimately derive from the PersistentObject class. | ||
- | |||
- | Each PersistentObject has a readonly " | ||
- | |||
- | A PersistentObject may be the " | ||
- | |||
- | You won't generally need to worry too much about this id hierarchy, though. LibEcho takes care of most of it for you. The vast majority of the time, you'll only need to know the ids of toplevel objects that you have explicitly created: " | ||
- | |||
- | ===== How It Works ===== | ||
- | |||
- | The implementation of PersistentObject isn't very straightforward to read. Here's a high level overview of how it works. | ||
- | |||
- | There are three layers of data associated with a PersistentObject. | ||
- | |||
- | The lowest layer is called the " | ||
- | |||
- | Unless you are actually extending LibEcho itself, you will probably never really need to deal with the object layer in your story. | ||
- | |||
- | The middle layer is called the " | ||
- | |||
- | Finally, the top layer is called the " | ||
- | |||
- | In this way, only the changes from the default data are stored in Sugarcube' | ||
- | |||
- | These three layers are implemented by returning a Proxy object from the PersistentObject constructor. | ||
- | |||
- | This handler also replaces all references to PersistentObjects stored within Sugarcube' | ||
- | |||
- | Please look at the [[Tutorial]] for more details. | ||
- | |||
- | ===== LibEcho.Persistence.PersistentObject ===== | ||
- | |||
- | Inherits from: Nothing | ||
- | |||
- | The PersistentObject is the class from which all of the more complex persistent data types are derived. People, Apparel, Transformable attributes, Inventories of various sorts, and many other classes of objects, are all are ultimately derived from PersistentObject. | ||
- | |||
- | // | ||
- | |||
- | //**You should never instantiate a PersistentObject directly! | ||
- | |||
- | If you try to instantiate a PersistentObject directly, rather than wrapping it in a call to fetch(), the Proxy object will get confused about what to do with properties that are set in subclass constructors, | ||
- | |||
- | ==== Static Functions ==== | ||
- | |||
- | Two static utility functions are defined on PersistentObject. | ||
- | |||
- | === PersistentObject.define( id, objectClass=PersistentObject, | ||
- | Defines the default data for a PersistentObject (or subclass thereof) within your game. PersistentObject.define() must only be called at initialization time, from story javascript or initialization passages. | ||
- | |||
- | * id: the Persistence ID of the object being defined. | ||
- | * objectClass: | ||
- | * defaults: a mapping of property names to data for the object' | ||
- | * allowRedefine: | ||
- | |||
- | * Returns: An instantiated instance of the object that was defined. | ||
- | * Throws: Error if the object is being redefined (and allowRedefine=false) or if the previously defined object does not have a valid class associated with it. | ||
- | |||
- | All PersistentObjects (and subclasses) must be defined using this method before they can be used within the game. See the [[Tutorial]] for a better explanation of all of this. | ||
- | |||
- | Example:< | ||
- | LibEcho.PersistentObject.define( " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | });</ | ||
- | |||
- | === PersistentObject.fetch( id, forceClass=undefined ) === | ||
- | Given a persistence ID, returns the appropriate instantiated object (a PersistentObject or subclass). | ||
- | |||
- | * id: the Persistence ID of the object being fetched. | ||
- | * forceClass: If set, overrides the class associated with the object ID and uses the given class instead. | ||
- | |||
- | * Returns: An instantiated instance of the object defined with the given id. | ||
- | * Throws: Error if the object does not have a valid class associated with it. | ||
- | |||
- | ==== Properties ==== | ||
- | |||
- | === .id === | ||
- | This object' | ||
- | |||
- | * Type: String | ||
- | * Automatic | ||
- | * ReadOnly | ||
- | |||
- | === .parent === | ||
- | This object' | ||
- | |||
- | * Type: PersistentObject or undefined | ||
- | * Automatic | ||
- | * ReadOnly | ||
- | |||
- | === .name === | ||
- | The display name of the object, without any article. For example: " | ||
- | |||
- | * Type: String | ||
- | * Required | ||
- | |||
- | === .nameIsProper === | ||
- | Set this field to True to suppress the printing of an article in the .aName, .theName, and generic versions of these fields. Useful for proper names, book titles, etc. See the [[Tutorial]] for more information. | ||
- | |||
- | * Type: Boolean | ||
- | * Optional | ||
- | |||
- | === .nameIrregularArticle === | ||
- | Set this field to override the default article choice in .aNameand generic versions of that field. Setting it back to undefined or null will return things to normal. | ||
- | |||
- | * Type: String | ||
- | * Optional | ||
- | |||
- | === .genericName === | ||
- | This is the generic non-detailed name of the object, without any article. | ||
- | |||
- | * Type: String | ||
- | * Required | ||
- | |||
- | === .genericNameIsProper === | ||
- | Same as .nameIsProper, | ||
- | |||
- | * Type: Boolean | ||
- | * Optional | ||
- | |||
- | === .genericNameIrregularArticle === | ||
- | Same as .nameIrregularArticle, | ||
- | |||
- | * Type: String | ||
- | * Optional | ||
- | |||
- | === .aName === | ||
- | The display name of the object, with the correct article prepended. For example: " | ||
- | |||
- | If the first character of the .name field is capitalized, | ||
- | |||
- | These rules do not always work. For example, "an honorable man" or "a European swallow" | ||
- | |||
- | * Type: String (getter) FIXME change to function so it can be overridden in story code more easily. | ||
- | * Automatic | ||
- | |||
- | === .AName === | ||
- | Same as .aName, but with the first character capitalized. | ||
- | |||
- | * Type: String (getter) FIXME change to function so it can be overridden in story code more easily. | ||
- | * Automatic | ||
- | |||
- | === .aGenericName === | ||
- | Same as .aName, but operates on the genericName instead. | ||
- | |||
- | * Type: String (getter) FIXME change to function so it can be overridden in story code more easily. | ||
- | * Automatic | ||
- | |||
- | === .AGenericName === | ||
- | Same as .AName, but operates on the genericName instead. | ||
- | |||
- | * Type: String (getter) FIXME change to function so it can be overridden in story code more easily. | ||
- | * Automatic | ||
- | |||
- | === .theName === | ||
- | The display name of the object, with the " | ||
- | |||
- | These rules do not always work. For example, "the European swallow" | ||
- | |||
- | * Type: String (getter) FIXME change to function so it can be overridden in story code more easily. | ||
- | * Automatic | ||
- | |||
- | === .TheName === | ||
- | Identical to .theName, but with the first character capitalized. | ||
- | |||
- | * Type: String (getter) FIXME change to function so it can be overridden in story code more easily. | ||
- | * Automatic | ||
- | |||
- | === .theGenericName === | ||
- | Same as .theName, but operates on the genericName instead. | ||
- | |||
- | * Type: String (getter) FIXME change to function so it can be overridden in story code more easily. | ||
- | * Automatic | ||
- | |||
- | === .TheGenericName === | ||
- | Same as .TheName, but operates on the genericName instead. | ||
- | |||
- | * Type: String (getter) FIXME change to function so it can be overridden in story code more easily. | ||
- | * Automatic | ||
- | |||
- | === .image === | ||
- | A detail image name for the object (used in object details displays, and in inventory thumbnails if no thumbnail is defined). | ||
- | |||
- | FIXME Make this work with external images instead of just image passages. | ||
- | |||
- | * Type: String | ||
- | * Required (if you use the Inventory and Apparel UIs) | ||
- | |||
- | === .thumbnail === | ||
- | A thumbnail image name for the object (used in inventory displays). | ||
- | |||
- | FIXME Make this work with external images instead of just image passages. | ||
- | |||
- | * Type: String (getter) FIXME change to function so it can be overridden in story code more easily. | ||
- | * Optional | ||
- | |||
- | === .description === | ||
- | FIXME unimplemented | ||
- | |||
- | The name of a passage to display with the detail description of the object. | ||
- | |||
- | * Type: String | ||
- | * Optional | ||
- | |||
- | === .inventoryCategory === | ||
- | The inventory category of this object. | ||
- | |||
- | * Type: String | ||
- | * Required (if the item can be picked up or worn by the player) | ||
- | |||
- | === .equals( o ) === | ||
- | Overridden to compare object id fields rather than references. | ||
- | |||
- | === .toString() === | ||
- | Overridden to return this.aName. | ||
- | |||
- | === .clone() === | ||
- | Overridden to grab an instantiated copy of the object via PersistentObject.fetch(). | ||
- | |||
- | === .toJSON() === | ||
- | Overridden to provide a reviveWrapper that grabs an instantiated copy of the object via PersistentObject.fetch(). | ||
- | |||
- | === .someProperty === | ||
- | FIXME | ||
- | |||
- | * Type: type | ||
- | * Optional Required | ||
- | |||
- | === .someProperty === | ||
- | FIXME | ||
- | |||
- | * Type: type | ||
- | * Optional Required | ||
- | |||
- | === .someProperty === | ||
- | FIXME | ||
- | |||
- | * Type: type | ||
- | * Optional Required | ||
- | |||
- | === .someProperty === | ||
- | FIXME | ||
- | |||
- | * Type: type | ||
- | * Optional Required | ||
- | |||
- | === .someProperty === | ||
- | FIXME | ||
- | |||
- | * Type: type | ||
- | * Optional Required | ||
- | |||
- | === .someProperty === | ||
- | FIXME | ||
- | |||
- | * Type: type | ||
- | * Optional Required | ||
- | |||
- | === .someProperty === | ||
- | FIXME | ||
- | |||
- | * Type: type | ||
- | * Optional Required | ||
- | |||