Changes

→‎Advanced: + Let other mods edit your internal assets
Line 476: Line 476:     
See [[#Edit a game asset|''edit a game asset'']] for a description of the available patch helpers.
 
See [[#Edit a game asset|''edit a game asset'']] for a description of the available patch helpers.
 +
 +
===Let other mods edit your internal assets===
 +
Other mods can't edit your internal mod files (including data or texture files), but they ''can'' edit custom assets you provide through the content pipeline. This technique consists of three steps:
 +
# Define a custom asset based on the internal file using the [[Modding:Modder Guide/APIs/Events#Content.AssetRequested|<samp>AssetRequested</samp> event]].
 +
# Detect when it's loaded/changed using the [[Modding:Modder Guide/APIs/Events#Content.AssetReady|<samp>AssetReady</samp> event]].
 +
# Load it through the content pipeline when you need it.
 +
 +
For example, this mod just loads a data asset (a dictionary of model entries):
 +
 +
<syntaxhighlight lang="c#">
 +
public class ModEntry : Mod
 +
{
 +
    /// <summary>The loaded data.</summary>
 +
    private Dictionary<string, ExampleModel> Data;
 +
 +
    /// <inheritdoc/>
 +
    public override void Entry(IModHelper helper)
 +
    {
 +
        helper.Events.Content.AssetRequested += this.OnAssetRequested;
 +
        helper.Events.Content.AssetReady += this.OnAssetReady;
 +
        helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
 +
    }
 +
 +
    private void OnAssetRequested(object sender, AssetRequestedEventArgs e)
 +
    {
 +
        //
 +
        // 1. define the custom asset based on the internal file
 +
        //
 +
        if (e.Name.IsEquivalentTo("Mods/Your.ModId/Data"))
 +
        {
 +
            e.LoadFromModFile<Dictionary<string, ExampleModel>>("assets/default-data.json", AssetLoadPriority.Medium);
 +
        }
 +
    }
 +
 +
    private void OnAssetReady(object sender, AssetReadyEventArgs e)
 +
    {
 +
        //
 +
        // 2. update the data when it's reloaded
 +
        //
 +
        if (e.Name.IsEquivalentTo("Mods/Your.ModId/Data"))
 +
        {
 +
            this.Data = Game1.content.Load<Dictionary<string, ExampleModel>>("Mods/Your.ModId/Data");
 +
        }
 +
    }
 +
 +
    private void OnGameLaunched(object sender, GameLaunchedEventArgs e)
 +
    {
 +
        //
 +
        // 3. load the data
 +
        //    (This doesn't need to be in OnGameLaunched, you can load it later depending on your mod logic.)
 +
        //
 +
        this.Data = Game1.content.Load<Dictionary<string, ExampleModel>>("Mods/Your.ModId/Data");
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 +
This works for any asset type (e.g. maps or textures), and you can do this even without an internal file (e.g. using <code>e.LoadFrom(() => new Dictionary<string, ExampleModel>(), AssetLoadPriority.Medium)</code>).
    
[[ru:Модификации:Моддер гайд/APIs/Контент]]
 
[[ru:Модификации:Моддер гайд/APIs/Контент]]
translators
8,447

edits