Difference between revisions of "Modding:Modder Guide/APIs/Data"

From Stardew Valley Wiki
Jump to navigation Jump to search
(create page (rewritten from content in Modding:Modder Guide/APIs/Config; only author for the affected content is Pathoschild))
 
(expand for new SMAPI 2.8 API)
Line 2: Line 2:
  
 
The data API lets you store arbitrary data and retrieve it later. For example, you can use this to store player progression, custom items that can't be saved by the game, etc. The data is stored in JSON files in your mod folder.
 
The data API lets you store arbitrary data and retrieve it later. For example, you can use this to store player progression, custom items that can't be saved by the game, etc. The data is stored in JSON files in your mod folder.
 +
 +
==Storage options==
 +
===JSON files===
 +
You can store data in arbitrary <tt>.json</tt> 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.
 +
 +
<ol>
 +
<li>[[#Data model|Create your data model]].</li>
 +
<li>In your mod code, use the mod helper to read/write a named file. This example assumes you created a class named <tt>ModData</tt>, but you can use different names too.
 +
 +
<source lang="c#">
 +
// read file
 +
var model = this.Helper.ReadJsonFile<ModData>("data.json") ?? new ModData();
 +
 +
// save file (if needed)
 +
this.Helper.WriteJsonFile("data.json", model);
 +
</source>
 +
 +
Note that <tt>ReadJsonFile</tt> will return <tt>null</tt> if the file doesn't exist. The above example will create a default instance if that happens; if you don't want to do that, just remove the <code>?? new ModData()</code> part.</li>
 +
</ol>
 +
 +
By changing the file path you specify, you can...
 +
* store JSON files in a subfolder, by specifying a path relative to your mod folder (like <code>"data/some-file.json"</code>). SMAPI will create the folders automatically if needed.
 +
* create per-save JSON files by using the save ID in the name, like <code>$"data/{Constants.SaveFolderName}.json"</code>.
 +
 +
{{SMAPI upcoming|2.8|content=The methods are now <tt>this.Helper.Data.ReadJsonFile</tt> and <tt>this.Helper.Data.WriteJsonFile</tt>.}}
 +
 +
===Save data===
 +
{{SMAPI upcoming|2.8}}
 +
You can store arbitrary data in the current save file. This is mainly useful for save-specific data, like player progression and custom items. Note that a save file must be loaded (e.g. it won't work on the title screen), and the data won't be persisted until the player saves the current day.
 +
 +
<ol>
 +
<li>[[#Data model|Create your data model]].</li>
 +
<li>[[#Data key|Choose your data key]].</li>
 +
<li>In your mod code, use the data API to read/write a named entry. This example assumes your data model is <tt>ModData</tt> and your key is <tt>example-key</tt>, but you can use different values.
 +
 +
<source lang="c#">
 +
// read file
 +
var model = this.Helper.ReadSaveData<ModData>("example-key");
 +
 +
// save file (if needed)
 +
this.Helper.WriteSaveData("example-key", model);
 +
</source>
 +
 +
Note that <tt>ReadSaveData</tt> will return <tt>null</tt> if the data doesn't exist.</li>
 +
</ol>
 +
 +
===Global app data===
 +
{{SMAPI upcoming|2.8}}
 +
You can store arbitrary data on the local computer, synchronised by GOG/Steam if applicable. This data is global (not per-save) and changes are saved immediately.
 +
 +
<ol>
 +
<li>[[#Data model|Create your data model]].</li>
 +
<li>[[#Data key|Choose your data key]].</li>
 +
<li>In your mod code, use the data API to read/write a named entry. This example assumes your data model is <tt>ModData</tt> and your key is <tt>example-key</tt>, but you can use different values.
 +
 +
<source lang="c#">
 +
// read file
 +
var model = this.Helper.ReadGlobalData<ModData>("example-key");
 +
 +
// save file (if needed)
 +
this.Helper.WriteGlobalData("example-key", model);
 +
</source>
 +
 +
Note that <tt>ReadSaveData</tt> will return <tt>null</tt> if the data doesn't exist.</li>
 +
</ol>
  
 
==Data model==
 
==Data model==
Line 48: Line 113:
 
</source>
 
</source>
  
==Storage options==
+
==Data key==
===JSON files===
+
A ''data key'' identifies your data, which lets you access the data again later. It should be unique within your mod, but there's no need to worry about conflicts with other mods (SMAPI will namespace the key internally). A data key can only contain letters, numbers, underscores, hyphens, or dots.
You can store data in arbitrary <tt>.json</tt> files in your mod folder (SMAPI will automatically parse and normalise them):
 
<ol>
 
<li>[[#Data model|Create your data model]].</li>
 
<li>In your mod code, use the mod helper to read/write a named file. This example assumes you created a class named <tt>ModData</tt>, but you can use different names too.
 
 
 
<source lang="c#">
 
// read file
 
var model = this.Helper.ReadJsonFile<ModData>("data.json") ?? new ModData();
 
 
 
// save file (if needed)
 
this.Helper.WriteJsonFile("data.json", model);
 
</source>
 
 
 
Note that <tt>ReadJsonFile</tt> will return <tt>null</tt> if the file doesn't exist. The above example will create a default instance if that happens; if you don't want to do that, just remove the <code>?? new ModData()</code> part.</li>
 
</ol>
 
 
 
By changing the file path you specify, you can...
 
* store JSON files in a subfolder, by specifying a path relative to your mod folder (like <code>"data/some-file.json"</code>). SMAPI will create the folders automatically if needed.
 
* create per-save JSON files by using the save ID in the name, like <code>$"data/{Constants.SaveFolderName}.json"</code>.
 
  
 
{{modding guide footer
 
{{modding guide footer

Revision as of 20:19, 18 August 2018

Creating SMAPI mods SMAPI mascot.png


Modding:Index

The data API lets you store arbitrary data and retrieve it later. For example, you can use this to store player progression, custom items that can't be saved by the game, etc. The data is stored in JSON files in your mod folder.

Storage options

JSON files

You can store data in arbitrary .json 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.

  1. Create your data model.
  2. In your mod code, use the mod helper to read/write a named file. This example assumes you created a class named ModData, but you can use different names too.
    // read file
    var model = this.Helper.ReadJsonFile<ModData>("data.json") ?? new ModData();
    
    // save file (if needed)
    this.Helper.WriteJsonFile("data.json", model);
    
    Note that ReadJsonFile will return null if the file doesn't exist. The above example will create a default instance if that happens; if you don't want to do that, just remove the ?? new ModData() part.

By changing the file path you specify, you can...

  • store JSON files in a subfolder, by specifying a path relative to your mod folder (like "data/some-file.json"). SMAPI will create the folders automatically if needed.
  • create per-save JSON files by using the save ID in the name, like $"data/{Constants.SaveFolderName}.json".
The following describes the upcoming SMAPI 2.8, and may change before release.
The methods are now this.Helper.Data.ReadJsonFile and this.Helper.Data.WriteJsonFile.

Save data

The following describes the upcoming SMAPI 2.8, and may change before release.

You can store arbitrary data in the current save file. This is mainly useful for save-specific data, like player progression and custom items. Note that a save file must be loaded (e.g. it won't work on the title screen), and the data won't be persisted until the player saves the current day.

  1. Create your data model.
  2. Choose your data key.
  3. In your mod code, use the data API to read/write a named entry. This example assumes your data model is ModData and your key is example-key, but you can use different values.
    // read file
    var model = this.Helper.ReadSaveData<ModData>("example-key");
    
    // save file (if needed)
    this.Helper.WriteSaveData("example-key", model);
    
    Note that ReadSaveData will return null if the data doesn't exist.

Global app data

The following describes the upcoming SMAPI 2.8, and may change before release.

You can store arbitrary data on the local computer, synchronised by GOG/Steam if applicable. This data is global (not per-save) and changes are saved immediately.

  1. Create your data model.
  2. Choose your data key.
  3. In your mod code, use the data API to read/write a named entry. This example assumes your data model is ModData and your key is example-key, but you can use different values.
    // read file
    var model = this.Helper.ReadGlobalData<ModData>("example-key");
    
    // save file (if needed)
    this.Helper.WriteGlobalData("example-key", model);
    
    Note that ReadSaveData will return null if the data doesn't exist.

Data model

Creating a data model

The data model is a C# class you create, with properties representing the data you want to store. It can contain almost anything from a few boolean fields to a complex object graph. Here's a simple data model:

class ModData
{
   public bool ExampleBoolean { get; set; }
   public float ExampleFloat { get; set; }
}

If you save that model to a JSON file, the file would look like this:

{
   "ExampleBoolean": false,
   "ExampleFloat": 0
}

Default values

You can optionally set default values in your data model:

class ModData
{
   public bool ExampleBoolean { get; set; } = true;
   public float ExampleFloat { get; set; } = 0.5f;
}

...or set defaults with a constructor:

class ModData
{
   public bool ExampleBoolean { get; set; }
   public float ExampleFloat { get; set; }

   public ModData()
   {
      this.ExampleBoolean = true;
      this.ExampleFloat = 0.5f;
   }
}

Data key

A data key identifies your data, which lets you access the data again later. It should be unique within your mod, but there's no need to worry about conflicts with other mods (SMAPI will namespace the key internally). A data key can only contain letters, numbers, underscores, hyphens, or dots.