Changes

Jump to navigation Jump to search
→‎Events: + content events
Line 62: Line 62:  
==Events==
 
==Events==
 
The available events are documented below.
 
The available events are documented below.
 +
 +
===Content===
 +
{{SMAPI upcoming|4.0.0|content=
 +
 +
<samp>this.Helper.Events.Content</samp> has events related to assets loaded from the content pipeline.
 +
 +
{{{!}} class="wikitable"
 +
{{!}}-
 +
! event
 +
! summary
 +
{{/event
 +
|group = Content
 +
|name  = AssetRequested
 +
|desc  = Raised when an asset is being requested from the content pipeline. The asset isn't necessarily being loaded yet (''e.g.'' the game may be checking if it exists).
 +
 +
You can register the changes you want to apply using the event arguments below; they'll be applied when the asset is actually loaded. See the [[Modding:Modder Guide/APIs/Content|content API]] for more info.
 +
 +
If the asset is requested multiple times in the same tick (e.g. once to check if it exists and once to load it), SMAPI might only raise the event once and reuse the cached result.
 +
 +
|arg name 1 = <samp>e.Name</samp>
 +
|arg type 1 = <samp>IAssetName</samp>
 +
|arg desc 1 = The name of the asset being requested. This includes utility methods to parse the value, like <code>e.Name.IsEquivalentTo("Portraits/Abigail")</code> (which handles any differences in formatting for you).
 +
 +
|arg name 2 = <samp>e.LoadFrom(...)</samp>
 +
|arg type 2 = ''method''
 +
|arg desc 2 = Call this method to provide the initial instance for the asset, instead of trying to load it from the game's <samp>Content</samp> folder. For example:
 +
<syntaxhighlight lang="c#">
 +
e.LoadFrom(() => this.Helper.Content.Load<Texture2D>("assets/portraits.png"));
 +
</syntaxhighlight>
 +
 +
Usage notes:
 +
* The asset doesn't need to exist in the game's <samp>Content</samp> folder. If any mod loads the asset, the game will see it as an existing asset as if it was in that folder.
 +
* Each asset can logically only have one initial instance. If multiple loads apply at the same time, SMAPI will raise an error and ignore all of them. If you're making changes to the existing asset instead of replacing it, you should use the <samp>Edit</samp> method instead to avoid those limitations and improve mod compatibility.
 +
 +
|arg name 3 = <samp>e.LoadFromModFile<T>(...)</samp>
 +
|arg type 3 = ''method''
 +
|arg desc 3 = Call this method to provide the initial instance for the asset by loading a file from your mod folder. For example:
 +
<syntaxhighlight lang="c#">
 +
e.LoadFromModFile<Texture2D>("assets/portraits.png");
 +
</syntaxhighlight>
 +
 +
See usage notes on <samp>e.LoadFrom</samp>.
 +
 +
|arg name 4 = <samp>e.Edit(...)</samp>
 +
|arg type 4 = ''method''
 +
|arg desc 4 = Call this method to apply edits to the asset after it's loaded. For example:
 +
<syntaxhighlight lang="c#">
 +
e.Edit(asset =>
 +
{
 +
    Texture2D ribbon = this.Helper.Content.Load<Texture2D>("assets/ribbon.png");
 +
    asset.AsImage().PatchImage(source: overlay, patchMode: PatchMode.Overlay);
 +
});
 +
</syntaxhighlight>
 +
 +
Usage notes:
 +
* Editing an asset which doesn't exist has no effect. This is applied after the asset is loaded from the game's <samp>Content</samp> folder, or from any mod's <samp>LoadFrom</samp> or <samp>LoadFromModFile</samp>.
 +
* You can apply any number of edits to the asset. Each edit will be applied on top of the previous one (i.e. it'll see the merged asset from all previous edits as its input).
 +
}}
 +
{{!}}}
 +
}}
    
===Display===
 
===Display===
translators
8,446

edits

Navigation menu