Line 4: |
Line 4: |
| | | |
| ==Dependencies== | | ==Dependencies== |
− | You can define dependencies in your <tt>manifest.json</tt> file, which are other mods that must be loaded before yours. If the dependency is required and missing, SMAPI will show a friendly error to the player. See [[../Manifest|the manifest page]] for more info. | + | You can define dependencies in your <samp>manifest.json</samp> file, which are other mods that must be loaded before yours. If the dependency is required and missing, SMAPI will show a friendly error to the player. See [[../Manifest|the manifest page]] for more info. |
| | | |
| ==Mod registry== | | ==Mod registry== |
− | Your mod can get information about loaded mods, or check if a particular mod is loaded. (All mods are loaded by the time your mod's <tt>Entry(…)</tt> method is called.) | + | Your mod can get information about loaded mods, or check if a particular mod is loaded. (All mods are loaded by the time your mod's <samp>Entry(…)</samp> method is called.) |
| | | |
| <syntaxhighlight lang="c#"> | | <syntaxhighlight lang="c#"> |
Line 41: |
Line 41: |
| </syntaxhighlight> | | </syntaxhighlight> |
| (You can use a [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors constructor] to initialise the API if desired.)</li> | | (You can use a [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors constructor] to initialise the API if desired.)</li> |
− | <li>Override <tt>GetApi</tt> in your mod's entry class and return an instance of your API: | + | <li>Override <samp>GetApi</samp> in your mod's entry class and return an instance of your API: |
| <syntaxhighlight lang="c#"> | | <syntaxhighlight lang="c#"> |
| public override object GetApi() | | public override object GetApi() |
Line 53: |
Line 53: |
| | | |
| Notes: | | Notes: |
− | * <tt>GetApi</tt> is always called after <tt>Entry</tt>, so it's safe to pass in your mod's initialised fields. | + | * <samp>GetApi</samp> is always called after <samp>Entry</samp>, so it's safe to pass in your mod's initialised fields. |
| * Be careful when changing your API! If you make a breaking change, other mods may need an update before they can access your API again. | | * Be careful when changing your API! If you make a breaking change, other mods may need an update before they can access your API again. |
| * You can optionally add a public [https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface interface] to your API. If a mod directly references your mod DLL, they'll be able to use your interface instead of creating their own. | | * You can optionally add a public [https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface interface] to your API. If a mod directly references your mod DLL, they'll be able to use your interface instead of creating their own. |
Line 67: |
Line 67: |
| } | | } |
| </syntaxhighlight></li> | | </syntaxhighlight></li> |
− | <li>In your mod code (after <tt>Entry</tt>), you can get the API by specifying the interface you created in step 1, and the [[../Manifest#Basic fields|mod's unique ID]]: | + | <li>In your mod code (after <samp>Entry</samp>), you can get the API by specifying the interface you created in step 1, and the [[../Manifest#Basic fields|mod's unique ID]]: |
| <syntaxhighlight lang="c#"> | | <syntaxhighlight lang="c#"> |
| ISomeModApi api = helper.ModRegistry.GetApi<ISomeModApi>("other-mod-ID"); | | ISomeModApi api = helper.ModRegistry.GetApi<ISomeModApi>("other-mod-ID"); |
Line 83: |
Line 83: |
| | | |
| Notes: | | Notes: |
− | * You can't call <tt>GetApi</tt> until all mods are initialised and their <tt>Entry</tt> methods called. You can use the <tt>GameLoop.GameLaunched</tt> [[#Events|event]] if you need to access mod APIs early; this is guaranteed to happen after all mods are initialised. | + | * You can't call <samp>GetApi</samp> until all mods are initialised and their <samp>Entry</samp> methods called. You can use the <samp>GameLoop.GameLaunched</samp> [[#Events|event]] if you need to access mod APIs early; this is guaranteed to happen after all mods are initialised. |
− | * You should always null-check APIs you consume. <tt>GetApi</tt> will return <tt>null</tt> if the API isn't available (e.g. because the mod isn't already installed or doesn't have one), or if an error occurs fetching the API. | + | * You should always null-check APIs you consume. <samp>GetApi</samp> will return <samp>null</samp> if the API isn't available (e.g. because the mod isn't already installed or doesn't have one), or if an error occurs fetching the API. |
| | | |
| ===Known limitations=== | | ===Known limitations=== |
| * When providing an API, the interface and implementation must be public. | | * When providing an API, the interface and implementation must be public. |
− | * When mapping an API to a custom interface using <tt>GetApi<T></tt> (i.e. not using the mod's original interface), only the API interface itself can be proxied. Method return values and parameters must be types that both mods have access to (e.g. built-in types like <tt>bool</tt>, SMAPI types like <tt>IManifest</tt>, or game types like <tt>GameLocation</tt>). | + | * When mapping an API to a custom interface using <samp>GetApi<T></samp> (i.e. not using the mod's original interface), only the API interface itself can be proxied. Method return values and parameters must be types that both mods have access to (e.g. built-in types like <samp>bool</samp>, SMAPI types like <samp>IManifest</samp>, or game types like <samp>GameLocation</samp>). |
| | | |
| ==Message sending== | | ==Message sending== |