Changes

Jump to navigation Jump to search
→‎Mod data: rewrite, add more info from migration guide
Line 83: Line 83:  
</syntaxhighlight>
 
</syntaxhighlight>
   −
Only public properties and fields will be serialized, and your class needs a zero-parameter constructor.
+
Only public properties and fields will be serialized, and your class needs a zero-parameter constructor. SMAPI uses NewtonSoft to serialize these models, so if you need finer-grained control over what gets serialized, use NewtonSoft annotations.
    
===Default values===
 
===Default values===
Line 119: 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,446

edits

Navigation menu