This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
libtf:documentation:api:persistence [2018/02/25 08:45] – lee | libtf:documentation:api:persistence [2018/09/24 15:45] (current) – removed lee | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | [[libtf: | ||
- | |||
- | ====== LibTF.Persistence ====== | ||
- | |||
- | Most web browsers only allow for 10 megs (though some are as low as 5) of on-disk storage per origin. | ||
- | |||
- | In an attempt to minimize this issue, LibTF uses a defaults and delta persistence system. | ||
- | |||
- | In order to be persistent, all persistent objects must ultimately derive from the LibTF.PersistentObject class. | ||
- | |||
- | Each PersistentObject that is tracked by LibTF.Persistence has an " | ||
- | |||
- | A PersistentObject may be the " | ||
- | |||
- | You won't generally need to worry too much about this id hierarchy, though. | ||
- | |||
- | |||
- | ===== Fields ===== | ||
- | |||
- | None. | ||
- | |||
- | |||
- | ===== Methods ===== | ||
- | |||
- | ==== LibTF.Persistence.setDefaults( id, objectClass, | ||
- | |||
- | * **Arguments: | ||
- | * **//id://** a toplevel persistence id which will be used to fetch the object later. | ||
- | * **// | ||
- | * **// | ||
- | * **Returns: | ||
- | * **Throws:** Error if: //id// contains invalid characters, // | ||
- | |||
- | Defines the default data for a PersistentObject (or subclassed object thereof) within your game. setDefaults() must only be called at initialization time, from story javascript or initialization passages. | ||
- | |||
- | All PersistentObjects (and subclassed objects thereof) //must// be defined using this method before they can be used within the game. | ||
- | |||
- | <code javascript> | ||
- | LibTF.Persistence.setDefaults( " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | ], | ||
- | " | ||
- | } ); | ||
- | </ | ||
- | |||
- | |||
- | ==== LibTF.Persistence.instantiate( id, forceClass=null ) ==== | ||
- | |||
- | * **Arguments: | ||
- | * **//id://** a persistence id denoting an object (whose class is ultimately derived from PersistentObject) to instantiate. | ||
- | * **// | ||
- | * **Returns: | ||
- | * **Throws:** Error if: //id// contains invalid characters, // | ||
- | |||
- | Instantiates a working copy of an object (whose class was ultimately derived from PersistentObject), | ||
- | |||
- | //DO NOT// store these objects themselves in the story format' | ||
- | |||
- | In the Sugarcube 2 and Snowman story formats, you may use the tfObj() function as a shorthand for this, since it is used so much. | ||
- | |||
- | <code javascript> | ||
- | var joe = LibTF.Persistence.instantiate( " | ||
- | alert( " | ||
- | </ | ||
- | |||
- | Sugarcube/ | ||
- | <code javascript> | ||
- | alert( " | ||
- | </ | ||
- | |||
- | |||
- | ==== LibTF.Persistence.has( id ) ==== | ||
- | |||
- | * **Arguments: | ||
- | * **//id://** a persistence id to check for. | ||
- | * **Returns: | ||
- | * **Throws:** Error if: //id// contains invalid characters. | ||
- | |||
- | Checks to see if there is a record for //id// in the LibTF persistence engine. | ||
- | |||
- | <code javascript> | ||
- | if( LibTF.Persistence.has(" | ||
- | alert( "It would appear that european_swallow has an irregular article defined!" | ||
- | </ | ||
- | |||
- | |||
- | ==== LibTF.Persistence.get( id ) ==== | ||
- | |||
- | * **Arguments: | ||
- | * **//id://** the persistence id whose data record we want to retrieve. | ||
- | * **Returns: | ||
- | * **Throws:** Error if: //id// contains invalid characters. | ||
- | |||
- | Checks to see if there is a record for //id// in the LibTF persistence engine. | ||
- | |||
- | <code javascript> | ||
- | var article = LibTF.Persistence.get( " | ||
- | alert( "You see " + article | ||
- | </ | ||
- | (Would print "You see a european swallow here!", | ||
- | |||
- | |||
- | ==== LibTF.Persistence.set( id, value ) ==== | ||
- | |||
- | * **Arguments: | ||
- | * **//id://** the persistence id whose data record we want to set. | ||
- | * **// | ||
- | * **Returns: | ||
- | * **Throws:** Error if: //id// contains invalid characters. | ||
- | |||
- | Sets the data record associated with //id// in the LibTF persistence engine. | ||
- | |||
- | <code javascript> | ||
- | LibTF.Persistence.set( " | ||
- | </ | ||
- | |||
- | |||
- | ==== LibTF.Persistence.reset( id ) ==== | ||
- | |||
- | * **Arguments: | ||
- | * **//id://** the persistence id whose data record we want to reset. | ||
- | * **Returns: | ||
- | * **Throws:** Error if: //id// contains invalid characters. | ||
- | |||
- | Resets the data record associated with //id// to whatever it was defined with in setDefaults(). | ||
- | |||
- | <code javascript> | ||
- | LibTF.Persistence.reset( " | ||
- | </ | ||
- | |||
- | [[libtf: | ||