This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
smutbook:classes:persistence:persistentobject [2023/08/07 16:02] – [PersistentObject.define( id, objectClass=PersistentObject, defaults={}, allowRedefine=false )] lee | smutbook:classes:persistence:persistentobject [2023/08/08 18:39] (current) – lee | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PersistentObject ====== | ====== PersistentObject ====== | ||
- | **Inherits from:** Nothing | + | **Extends:** 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. | 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. | ||
- | // | + | ===== Usage Notes ===== |
- | //**You should never instantiate a PersistentObject | + | ==== Subclassing |
- | 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, | + | TODO |
- | ===== Static Functions ===== | + | ==== PersistentObject Behavioral Differences vs. Regular Objects ==== |
+ | |||
+ | TODO | ||
+ | * Deleting a property resets it to whatever was set in the defaults or class definition, rather than actually deleting it. If you want to legitimately delete a property, just set it to ' | ||
+ | * Funny business with subclass constructors. | ||
+ | * Don't use strict equality because objects may be reallocated differently between game moments, because of the way SugarCube' | ||
+ | |||
+ | ===== Static | ||
Two static utility functions are defined on PersistentObject. | Two static utility functions are defined on PersistentObject. | ||
Line 18: | Line 25: | ||
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. | 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. | ||
- | See the [[Tutorial]] for more details. | + | All PersistentObjects (and subclasses) must be defined using this method before they can be used within the game. |
- | * id: the Persistence ID of the object being defined. | + | * **Type:** Static Function |
- | * objectClass: | + | * **Arguments: |
- | * defaults: a mapping of property names to data for the object' | + | * //id//: the Persistence ID of the object being defined. |
- | * allowRedefine: | + | * //objectClass//: the class of the object being defined. |
- | + | * //defaults//: a mapping of property names to data for the object' | |
- | * Returns: An instantiated instance of the object that was defined. | + | * //allowRedefine//: if true, the object may be redefined. |
- | * Throws: Error if the object is being redefined (and allowRedefine=false), | + | |
- | + | | |
- | 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:< | Example:< | ||
Line 41: | Line 47: | ||
Given a persistence ID, returns the appropriate instantiated object (a PersistentObject or subclass). | Given a persistence ID, returns the appropriate instantiated object (a PersistentObject or subclass). | ||
- | * id: the Persistence ID of the object | + | The returned object is not guaranteed to be the same cached |
- | * forceClass: If set, overrides the class associated with the object | + | |
- | * 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. | + | * **Arguments: |
+ | * //id//: the Persistence ID of the object being fetched. | ||
+ | * // | ||
+ | * **Returns:** An instantiated instance of the object defined with the given id. | ||
+ | | ||
- | ===== Properties ===== | + | Example:< |
+ | FIXME | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Constructor( id ) ===== | ||
+ | |||
+ | // | ||
+ | |||
+ | //**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, | ||
+ | |||
+ | //**Again, do not ever use " | ||
+ | |||
+ | ===== Properties/ | ||
==== .id ==== | ==== .id ==== | ||
This object' | This object' | ||
- | * Type: String | + | |
- | * Automatic | + | |
- | * ReadOnly | + | |
==== .parent ==== | ==== .parent ==== | ||
- | This object' | + | This object' |
- | * Type: PersistentObject or undefined | + | |
- | * Automatic | + | |
- | * ReadOnly | + | |
==== .name ==== | ==== .name ==== | ||
The display name of the object, without any article. For example: " | The display name of the object, without any article. For example: " | ||
- | * Type: String | + | |
- | | + | |
==== .nameIsProper ==== | ==== .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. | 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 | + | |
- | | + | |
==== .nameIrregularArticle ==== | ==== .nameIrregularArticle ==== | ||
Set this field to override the default article choice in .aName and generic versions of that field. Setting it back to undefined or null will return things to normal. | Set this field to override the default article choice in .aName and generic versions of that field. Setting it back to undefined or null will return things to normal. | ||
- | * Type: String | + | |
- | | + | |
- | ==== .genericName | + | ==== .aName() ==== |
- | This is the generic non-detailed | + | The display |
- | * Type: String | + | If the first character of the .name field is capitalized, |
- | * Required | + | |
- | ==== .genericNameIsProper ==== | + | These rules do not always work. For example, "an honorable man" or "a European swallow" |
- | Same as .nameIsProper, but operates on the genericName instead. | + | |
- | * Type: Boolean | + | |
- | * Optional | + | * **Arguments: |
+ | * **Returns: | ||
+ | * **Throws:** Nothing | ||
- | ==== .genericNameIrregularArticle | + | ==== .AName() |
- | Same as .nameIrregularArticle, but operates on the genericName instead. | + | Same as .aName(), but with the first character capitalized. |
- | * Type: String | + | |
- | * Optional | + | * **Arguments: |
+ | * **Returns: | ||
+ | * **Throws:** Nothing | ||
- | ==== .aName ==== | + | ==== .theName() |
- | The display name of the object, with the correct article | + | The display name of the object, with the " |
- | If the first character of the .name field is capitalized, the name will be treated as proper, and no article will be prepended. Ie. "Joe" | + | These rules do not always work. For example, "the European swallow" |
- | These rules do not always work. For example, "an honorable man" or "a European swallow" | + | * **Type:** Function |
+ | * **Arguments: | ||
+ | * **Returns: | ||
+ | * **Throws:** Nothing | ||
- | * Type: String | + | ==== .TheName() ==== |
- | * Automatic | + | Identical |
- | ==== .AName ==== | + | * **Type:** Function |
- | Same as .aName, but with the first character capitalized. | + | * **Arguments: |
+ | * **Returns: | ||
+ | * **Throws:** Nothing | ||
- | * Type: String (getter) FIXME change to function so it can be overridden in story code more easily. | + | ==== .genericName === |
- | * Automatic | + | This is the generic non-detailed name of the object, without any article. |
- | ==== .aGenericName ==== | + | * **Type:** String |
- | 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. | + | ==== .genericNameIsProper ==== |
- | * Automatic | + | Same as .nameIsProper, |
- | ==== .AGenericName ==== | + | * **Type:** Boolean |
- | 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. | + | ==== .genericNameIrregularArticle ==== |
- | * Automatic | + | Same as .nameIrregularArticle, |
- | ==== .theName ==== | + | * **Type:** String |
- | The display name of the object, with the " | + | |
- | These rules do not always work. For example, "the European swallow" | + | ==== .aGenericName() ==== |
+ | Same as .aName(), but operates on the genericName instead. | ||
- | * Type: String | + | |
- | * Automatic | + | * **Arguments: |
+ | * **Returns: | ||
+ | * **Throws:** Nothing | ||
- | ==== .TheName | + | ==== .AGenericName() |
- | Identical to .theName, but with the first character capitalized. | + | Same as .AName(), but operates on the genericName instead. |
- | * Type: String | + | |
- | * Automatic | + | * **Arguments: |
+ | * **Returns: | ||
+ | * **Throws:** Nothing | ||
- | ==== .theGenericName ==== | + | ==== .theGenericName() ==== |
- | Same as .theName, but operates on the genericName instead. | + | Same as .theName(), but operates on the genericName instead. |
- | * Type: String | + | |
- | * Automatic | + | * **Arguments: |
+ | * **Returns: | ||
+ | * **Throws:** Nothing | ||
- | ==== .TheGenericName ==== | + | ==== .TheGenericName() ==== |
- | Same as .TheName, but operates on the genericName instead. | + | Same as .TheName(), but operates on the genericName instead. |
- | * Type: String | + | |
- | * Automatic | + | * **Arguments: |
+ | * **Returns: | ||
+ | * **Throws:** Nothing | ||
==== .image ==== | ==== .image ==== | ||
- | A detail image name for the object (used in object details displays, and in inventory thumbnails if no thumbnail is defined). | + | 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 |
- | + | ||
- | | + | |
- | | + | |
==== .thumbnail ==== | ==== .thumbnail ==== | ||
- | A thumbnail image name for the object (used in inventory displays). | + | A thumbnail image name for the object (used in inventory displays). |
- | FIXME Make this work with external images instead of just image passages. | + | * **Type:** Getter returning String |
- | + | ||
- | | + | |
- | | + | |
==== .description ==== | ==== .description ==== | ||
FIXME unimplemented | FIXME unimplemented | ||
- | The name of a passage to display with the detail description of the object. | + | The name of a passage to display with the detail description of the object. |
+ | |||
+ | By default it will use some variant of the object id (FIXME what exactly?), but this can be overridden | ||
* Type: String | * Type: String | ||
Line 194: | Line 224: | ||
==== .toJSON() ==== | ==== .toJSON() ==== | ||
Overridden to provide a reviveWrapper that grabs an instantiated copy of the object via PersistentObject.fetch(). | 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 | ||
- | |||