Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
persistence [2015/07/27 23:45]
chris
persistence [2015/07/27 23:54] (current)
chris
Line 15: Line 15:
  
 ### Entities (NPCs, structures, etc; non-players) ### Entities (NPCs, structures, etc; non-players)
 +With entities, by default, all items marked with the `SyncableAttribute` or `PersistAttribute` are saved and restored to the disk.  No extra work needs to be done.
  
 ### Players ### Players
 Players persist their data in the same exact way that Entities do, but with one major difference: it doesn'​t happen automatically. ​ Players can be easily saved and loaded in the implementation of `UserControllerBase` as part of your [[Plugin Context]]. Players persist their data in the same exact way that Entities do, but with one major difference: it doesn'​t happen automatically. ​ Players can be easily saved and loaded in the implementation of `UserControllerBase` as part of your [[Plugin Context]].
 +
 +NOTE: This is subject to change in the future.
  
 Here's an example: Here's an example:
Line 51: Line 54:
 ``` ```
 ### Subsystems ### Subsystems
 +
 +By default, all fields or properties marked with the `PersistAttribute` are syncd to disk.
  
 ### Plugin Extensions ### Plugin Extensions
 +
 +Plugin extensions are not persisted by default. ​ You can set the Persist=true on a server-side plugin extension, and all fields and properties marked with `PersistAttribute` in the implementation will be persisted.
  
 ### Other Classes (Misc) ### Other Classes (Misc)
  
 +There are some cases where you may have a class that was not created in such a way that it falls under one of these categories. ​ For this category, simply inject the `ISavedGame` [[dependency]] into your class, and call ```this.SavedGame.RegisterObjectForPersistence(this)```.
 +
 +This will do three things
 +1. All persistable data in the class will be loaded from file, if possible
 +2. Every duration (set in config), properties and fields marked with `PersistAttribute` will be saved to disk
 +3. The class will be saved if the server is stopped
 +
 +
 +## What if my data is more complex than json?
 +
 +If you don't want to use the `PersistAttribute`,​ and would prefer to save and load data yourself, simply implement the `IPersistable` interface to use streams instead. ​ This is good when you have large amounts of data, or data that can not be serialized correctly.