Changes

→‎Mod data: rewrite, add more info from migration guide
Line 4: Line 4:     
==Storage options==
 
==Storage options==
===Player modData===
  −
As mentioned in the [[https://stardewvalleywiki.com/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.
  −
  −
<syntaxhighlight lang="c#">
  −
public static class PerPlayerData {
  −
  −
static string HomeComputerNameKey {
  −
get {  return $"{ModEntry.instance.ModManifest.UniqueID}/HomeComputerName"; }
  −
}
  −
  −
public static string HomeComputerName {
  −
get {
  −
string result;
  −
if (Game1.player.modData.TryGetValue(HomeComputerNameKey, out result)) {
  −
return result;
  −
}
  −
return "Home Computer";
  −
}
  −
set {
  −
Game1.player.modData[HomeComputerNameKey] = value;
  −
}
  −
}
  −
}
  −
</syntaxhighlight>
  −
   
===JSON files===
 
===JSON files===
 
You can store data in arbitrary <samp>.json</samp> files in your mod folder. Note that these files will be lost if the player deletes them (''e.g.,'' when updating your mod), so this is mainly useful for bundled files, cache files, etc.
 
You can store data in arbitrary <samp>.json</samp> files in your mod folder. Note that these files will be lost if the player deletes them (''e.g.,'' when updating your mod), so this is mainly useful for bundled files, cache files, etc.
Line 144: Line 119:  
// delete entry (if present)
 
// delete entry (if present)
 
this.Helper.Data.WriteSaveData<DataModel>("example-key", null);
 
this.Helper.Data.WriteSaveData<DataModel>("example-key", null);
 +
</syntaxhighlight>
 +
 +
==See also==
 +
===Mod data===
 +
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#">
 +
// write a custom value
 +
item.modData[$"{this.ModManifest.UniqueID}/item-age"] = "30";
 +
 +
// read it
 +
if (item.modData.TryGetValue($"{this.ModManifest.UniqueID}/item-age", out string rawAge) && int.TryParse(rawAge, int age))
 +
  ...
 
</syntaxhighlight>
 
</syntaxhighlight>
translators
8,437

edits