|
| 1 | +--- |
| 2 | +title: Serializable |
| 3 | +sections: |
| 4 | +- Motivation |
| 5 | +- Usage |
| 6 | +- Complete Example |
| 7 | +--- |
| 8 | + |
| 9 | +## Motivation |
| 10 | + |
| 11 | +Of course usually we don't want to save just strings, but data that is stored in some object. |
| 12 | +For this purpose there is the `abstract class Serializable` for simple saving and loading of primitive attribute values. |
| 13 | +The `serialize()` function returns a `ChunkedString`, which then can be passed to the data save function from above. |
| 14 | + |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +Import `Serializable` Make your data class extend `Serializable` and implement the required functions. |
| 19 | + |
| 20 | +```wurst |
| 21 | +import Serializable |
| 22 | +
|
| 23 | +class MyClass extends Serializable |
| 24 | + var amount = 0 |
| 25 | +
|
| 26 | + override function serializeProperties() |
| 27 | +
|
| 28 | + override function deserializeProperties() |
| 29 | +``` |
| 30 | + |
| 31 | +### Saving Attributes |
| 32 | + |
| 33 | +Inside the `serializeProperties` register all attributes that should be serialized using `addProperty`. |
| 34 | + |
| 35 | +```wurst |
| 36 | + override function serializeProperties() |
| 37 | + addProperty("amount", amount) |
| 38 | +``` |
| 39 | + |
| 40 | +### Loading attributes |
| 41 | + |
| 42 | +Every serialized attribute should also be deserialized again in the `deserializeProperties` function using the appropriate `getXXProperty` function and assigned to the attribute. |
| 43 | +Make sure to use the same name used for saving. |
| 44 | + |
| 45 | +```wurst |
| 46 | + override function deserializeProperties() |
| 47 | + amount = getIntProperty("amount") |
| 48 | +``` |
| 49 | + |
| 50 | +### Serializing |
| 51 | + |
| 52 | +Serialize the data class object to a `ChunkedString` using `serialize`. You can pass this chunked string directly to the save functions from above. |
| 53 | + |
| 54 | +```wurst |
| 55 | + let saveData = new MyClass() |
| 56 | + saveData.amount = 1337 |
| 57 | + let result = saveData.serialize() |
| 58 | +``` |
| 59 | + |
| 60 | +### Deserializing |
| 61 | + |
| 62 | +To load from a `ChunkedString`, which could be obtained by loading using the function above, use `deserialize`. |
| 63 | + |
| 64 | +```wurst |
| 65 | + let loadedData = new MyClass() |
| 66 | + loadedData.deserialize(inputChunkedString) |
| 67 | +``` |
| 68 | + |
| 69 | +## Complete Example |
| 70 | + |
| 71 | +Find a full working example below. The first time you run the map a new save file will be created saving the value `1337` in the `amount` attribute. |
| 72 | +The second time you run the map it will load the existing save file and assign `1337` to the loaded object's `amount` variable, which is printed on the screen. |
| 73 | + |
| 74 | +```wurst |
| 75 | +package SerTest |
| 76 | +import Serializable |
| 77 | +import SaveLoadData |
| 78 | +import ClosureTimers |
| 79 | +
|
| 80 | +constant FILE_NAME = "MyFileName" |
| 81 | +
|
| 82 | +class MyClass extends Serializable |
| 83 | + var amount = 0 |
| 84 | +
|
| 85 | + override function serializeProperties() |
| 86 | + addProperty("amount", amount) |
| 87 | +
|
| 88 | + override function deserializeProperties() |
| 89 | + amount = getIntProperty("amount") |
| 90 | +
|
| 91 | +init |
| 92 | + doAfter(1.) -> |
| 93 | + players[0].loadData(FILE_NAME) (status, data) -> |
| 94 | + if status == LoadStatus.SUCCESS |
| 95 | + let loadedData = new MyClass()..deserialize(data) |
| 96 | + print("loaded: " + loadedData.amount.toString()) |
| 97 | + else |
| 98 | + let saveData = new MyClass() |
| 99 | + saveData.amount = 1337 |
| 100 | + let saveString = saveData.serialize() |
| 101 | + players[0].saveData(FILE_NAME, saveString) |
| 102 | + print("created new save file") |
| 103 | +
|
| 104 | +``` |
0 commit comments