Modding:Moddable Mod: Difference between revisions
From Vintage Story Wiki
mNo edit summary |
No edit summary |
||
Line 21: | Line 21: | ||
resolve at game runtime. | resolve at game runtime. | ||
Note that, excluding the use of reflection or the use of an intermediary library, which is outside the scope of this article, | <small>Note that, excluding the use of reflection or the use of an intermediary library, which is outside the scope of this article, | ||
<b>only compiled mods can interact with eachother</b>. | <b>only compiled mods can interact with eachother</b>.</small> | ||
== TipMod & SpawnMod == | == TipMod & SpawnMod == | ||
Line 37: | Line 37: | ||
* Create a class named after the mod it's contained it - <code>TipMod</code> and <code>SpawnMod</code>. | * Create a class named after the mod it's contained it - <code>TipMod</code> and <code>SpawnMod</code>. | ||
* Have our classes inherit <code>ModSystem</code> - the base for any VintageStory code mod. | * Have our classes inherit <code>ModSystem</code> - the base for any VintageStory code mod. | ||
* Override the <code>StartServerSide</code> method to access the <code> | * Override the <code>StartServerSide</code> method to access the [http://apidocs.vintagestory.at/api/Vintagestory.API.Common.ICoreServerAPI.html <code>ICoreServerAPI</code>]. | ||
* Make the <code>TipMod</code> class <code>public</code> so that it's accessible by other mods. | * Make the <code>TipMod</code> class <code>public</code> so that it's accessible by other mods. | ||
Line 117: | Line 117: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
We are now ready to start adding the functionality of our mods | We are now ready to start adding the functionality of our mods, starting with "TipMod", as that will be our | ||
point of interaction. | point of interaction. | ||
Line 124: | Line 124: | ||
* Store the <code>ICoreServerAPI</code> object as a field so that we can access it throughout our class. | * Store the <code>ICoreServerAPI</code> object as a field so that we can access it throughout our class. | ||
* Define a <code>List</code> that stores <code>Tip</code> objects. | * Define a <code>List</code> that stores <code>Tip</code> objects. | ||
* | * Define a method that selects a random <code>Tip</code> from the <code>List</code> and prints it in chat for all players, printing a default message if the <code>List</code> is empty. | ||
* Register that method to be called at set intervals by the [http://apidocs.vintagestory.at/api/Vintagestory.API.Server.IServerEventAPI.html#Vintagestory_API_Server_IServerEventAPI_Timer_Vintagestory_API_Common_Action_System_Double_ <code>Timer</code>], which is available in the [http://apidocs.vintagestory.at/api/Vintagestory.API.Server.IServerEventAPI.html <code> | * Register that method to be called at set intervals by the [http://apidocs.vintagestory.at/api/Vintagestory.API.Server.IServerEventAPI.html#Vintagestory_API_Server_IServerEventAPI_Timer_Vintagestory_API_Common_Action_System_Double_ <code>Timer</code>], which is available in the [http://apidocs.vintagestory.at/api/Vintagestory.API.Server.IServerEventAPI.html <code>IServerEventAPI</code>]. | ||
* Define a public method for adding new <code>Tip</code> objects. | * Define a <code>public</code> method for adding new <code>Tip</code> objects. | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
Line 175: | Line 175: | ||
Remember, both <b>classes and methods which other mods will interact with have to be declared <code>public</code>.</b> | Remember, both <b>classes and methods which other mods will interact with have to be declared <code>public</code>.</b> | ||
In C#, classes and methods are not public by default. You can | In C#, classes and methods are not public by default. You can [https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers read more about this here.] | ||
We can now compile our mod, add it to our VintageStory mod folder and test it ingame. If the there are occasional chat messages | We can now compile our mod, add it to our VintageStory mod folder and test it ingame. If the there are occasional chat messages | ||
Line 198: | Line 198: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
"SpawnMod" is ready. Let's test it. The command <code>/spawn</code> should now teleport us to spawn. | "SpawnMod" is ready. Let's test it. The command <code>/spawn</code> should now teleport us to spawn. | ||
If our mods are working, we're ready to make "SpawnMod" interact with "TipMod". | |||
== Mods interacting == | == Mods interacting == | ||
Before we get started, we have to add a reference in "SpawnMod" to "TipMod". If you are using Visual Studio, in your Solution Explorer, directly under your project, right-click "References" and select "Add Reference". | Before we get started, we have to add a reference in "SpawnMod" to "TipMod". | ||
=== Adding a reference to an external mod === | |||
If you are using Visual Studio, in your Solution Explorer, directly under your project, right-click "References" and select "Add Reference". | |||
[[File:ADDINGAREFERENCERIGHTCLICK.png|none]] | |||
Here we locate either the "TipMod" project or the compiled .dll file, and add it. | |||
[[File:FindingReference.png|none]] | |||
After adding the reference, make sure it is not copying the file to the output folder when compiling - | After adding the reference, make sure it is not copying the file to the output folder when compiling - | ||
having multiple .dll files with <code>ModSystems</code> in them will break your mod. | having multiple .dll files with <code>ModSystems</code> in them will break your mod. | ||
[[File:SettingCopyLocalToFalse.png|none]] | |||
=== Accessing the ModLoader === | |||
Now we're ready to have "SpawnMod" add tips. Let's go ahead and do the following. | Now we're ready to have "SpawnMod" add tips. Let's go ahead and do the following. | ||
* Add a <code>using</code> directive for tipmod.src. | * Add a <code>using</code> directive for the namespace <code>tipmod.src</code>. | ||
* Retrieve <code>TipMod</code> through the <code>ModLoader</code> by passing it's type. | * Retrieve <code>TipMod</code> through the <code>ModLoader</code> by passing it's type. | ||
* Call the <code>AddTip</code> method provided by <code>TipMod</code> and add a variety of tips. | * Call the <code>AddTip</code> method provided by <code>TipMod</code> and add a variety of tips. | ||
Line 356: | Line 359: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
We're now ready to test our mods to see if they're interacting successfully. After placing both our compiled mods in the mods folder and | We're now ready to test our mods to see if they're interacting successfully. After placing both our compiled mods in the mods folder and booting into a world, we should see a random tip pop up in the chat every 10 seconds. If so, well done! You've successfully followed the article and have created a moddable mod. | ||
[[File:ModdableModSuccess.png|none]] | |||
== Troubleshooting == | == Troubleshooting == | ||
Line 364: | Line 367: | ||
If you've ran into problems setting up either mod, check the error logs located at <code>VintagestoryData/Logs</code> in <code>server-main.txt</code>. | If you've ran into problems setting up either mod, check the error logs located at <code>VintagestoryData/Logs</code> in <code>server-main.txt</code>. | ||
Possible causes for either mod not working: | Possible causes for either mod not working: | ||
* It's a .cs (source) mod and not a .dll (compiled) mod. Refer here [https://wiki.vintagestory.at/index.php?title=Modding:Setting_up_your_Development_Environment#Set_Mod for how to set | * It's a .cs (source) mod and not a .dll (compiled) mod. Refer here [https://wiki.vintagestory.at/index.php?title=Modding:Setting_up_your_Development_Environment#Set_Mod for how to set a compiled mod.] | ||
* It's missing <code>modinfo.json</code>. | * It's missing <code>modinfo.json</code>. | ||
* Your version of VintageStory is outdated. | * Your version of VintageStory is outdated. |