Changes

Jump to navigation Jump to search
415 bytes added ,  06:09, 3 May 2022
→‎Using an asset editor: update for SMAPI 3.14
Line 1,177: Line 1,177:  
This page mainly covers editing maps with Tiled, but you can make the same changes programmatically using a [[Modding:Modder Guide/Get Started|C# SMAPI mod]]. There are two main approaches to changing a location in C# code.
 
This page mainly covers editing maps with Tiled, but you can make the same changes programmatically using a [[Modding:Modder Guide/Get Started|C# SMAPI mod]]. There are two main approaches to changing a location in C# code.
   −
===Using an asset editor===
+
===Using the <samp>AssetRequested</samp> event===
Most map edits should be applied using an [[Modding:Modder Guide/APIs/Content|asset editor]], so your changes aren't lost if another mod reloads the map. This works with the <samp>Map</samp> asset directly before it's used by the [[Modding:Modder Guide/Game Fundamentals#GameLocation et al|in-game location]], so you can't use location methods at this point.
+
Most map edits should be applied using [[Modding:Modder Guide/APIs/Content|the <samp>AssetRequested</samp> event]], so your changes aren't lost if another mod reloads the map. This works with the <samp>Map</samp> asset directly before it's used by the [[Modding:Modder Guide/Game Fundamentals#GameLocation et al|in-game location]]; that means you can't use location methods at this point, but you can make any changes you want before the map is read (e.g. change warps). See the [[Modding:Modder Guide/APIs/Events|events page]] for help using events.
   −
First you'll need to set up an asset editor, which mainly involves implementing <samp>IAssetEditor</samp> on your mod class. For example, this mod code edits the town map:
+
For example, this mod code edits the town map and adds an optional <samp>GetTile</samp> helper method:
    
<syntaxhighlight lang="C#">
 
<syntaxhighlight lang="C#">
Line 1,187: Line 1,187:  
{
 
{
 
     /// <inheritdoc />
 
     /// <inheritdoc />
     public override void Entry(IModHelper helper) { }
+
     public override void Entry(IModHelper helper)
 
  −
    /// <inheritdoc />
  −
    public bool CanEdit<T>(IAssetInfo asset)
   
     {
 
     {
         return asset.AssetNameEquals("Maps/Town");
+
         helper.Events.Content.AssetRequested += this.OnAssetRequested;
 
     }
 
     }
   −
     /// <inheritdoc />
+
     /// <inheritdoc cref="IContentEvents.AssetRequested"/>
     public void Edit<T>(IAssetData asset)
+
     /// <param name="sender">The event sender.</param>
 +
    /// <param name="e">The event data.</param>
 +
    private void OnAssetRequested(object sender, AssetRequestedEventArgs e)
 
     {
 
     {
         IAssetDataForMap editor = asset.AsMap();
+
         if (e.Name.IsEquivalentTo("Maps/Town"))
        Map map = editor.Data;
+
        {
 +
            e.Edit(asset =>
 +
            {
 +
                IAssetDataForMap editor = asset.AsMap();
 +
                Map map = editor.Data;
   −
        // your code here
+
                // your code here
 +
            });
 +
        }
 
     }
 
     }
  
translators
8,458

edits

Navigation menu