Line 123: |
Line 123: |
| ==See also== | | ==See also== |
| ===Mod data=== | | ===Mod data=== |
− | As mentioned in the [[Modding:Migrate to Stardew Valley 1.5#Custom mod data|1.5 Migration Guide]], you can store arbitrary string data on a per-game, per-player basis in the <samp>Game1.player.modData</samp> dictionary. This is automatically serialized/deserialized with the game, and works correctly in multiplayer.
| + | You can also store custom data for individual game entities which have a <samp>modData</samp> dictionary field. That includes NPCs and players (<samp>Character</samp>), <samp>GameLocation</samp>, <samp>Item</samp>, and <samp>TerrainFeature</samp>. This is persisted to the save file and synchronized in multiplayer. |
| | | |
| + | Usage notes: |
| + | * To avoid mod conflicts, prefixing data fields with your mod ID is strongly recommended (see the example below). |
| + | * When you split an item stack, the new stack copies the previous one's <samp>modData</samp> field; when merged into another stack, the merged items adopt the target stack's mod data. Otherwise mod data has no effect on item split/merge logic (''e.g.,'' you can still merge items with different mod data).</li> |
| + | |
| + | For example, this writes and then reads a custom 'age' value for an item: |
| <syntaxhighlight lang="c#"> | | <syntaxhighlight lang="c#"> |
− | public static class PerPlayerData {
| + | // write a custom value |
− | | + | item.modData[$"{this.ModManifest.UniqueID}/item-age"] = "30"; |
− | static string HomeComputerNameKey {
| |
− | get { return $"{ModEntry.instance.ModManifest.UniqueID}/HomeComputerName"; }
| |
− | }
| |
| | | |
− | public static string HomeComputerName {
| + | // read it |
− | get {
| + | if (item.modData.TryGetValue($"{this.ModManifest.UniqueID}/item-age", out string rawAge) && int.TryParse(rawAge, int age)) |
− | string result;
| + | ... |
− | if (Game1.player.modData.TryGetValue(HomeComputerNameKey, out result)) {
| |
− | return result;
| |
− | }
| |
− | return "Home Computer";
| |
− | }
| |
− | set {
| |
− | Game1.player.modData[HomeComputerNameKey] = value;
| |
− | }
| |
− | }
| |
− | }
| |
| </syntaxhighlight> | | </syntaxhighlight> |