The following guide will walk you through step-by-step on how to create a basic C# mod. This mod will add a button to the singleplayer title screen called Message. When clicked, this button will output Hello World to chat.
-
Go to your game files and locate the
Modulesdirectory. -
Create a new folder and name it
ExampleMod(Must be the same as the Id you use for Step #4). -
Create a new folder named
binand inside this directory, create a new folder calledWin64_Shipping_Client. -
Create a new
SubModule.xmlfile (must be named this) inside the folder you created in Step #2 and then paste the following into it:<Module> <Name value="Example Mod"/> <Id value="ExampleMod"/> <Version value="v1.0.0"/> <SingleplayerModule value="true"/> <MultiplayerModule value="false"/> <DependedModules> <DependedModule Id="Native"/> <DependedModule Id="SandBoxCore"/> <DependedModule Id="Sandbox"/> <DependedModule Id="CustomBattle"/> <DependedModule Id="StoryMode" /> </DependedModules> <SubModules> <SubModule> <Name value="ExampleMod"/> <DLLName value="ExampleMod.dll"/> <SubModuleClassType value="ExampleMod.MySubModule"/> <Tags> <Tag key="DedicatedServerType" value="none" /> <Tag key="IsNoRenderModeElement" value="false" /> </Tags> </SubModule> </SubModules> <Xmls/> </Module>
Note:
MySubModuleis the name of the class we will be using in the Programming section of the tutorial. -
If you are using different names, change the above values to match that of your Module/SubModule.
-
Start the launcher and make sure your mod appears under
Singleplayer>Mods.
For more information on the Module folder structure, Click Here.
Before setting up a project, it is important to know that this is not required for basic mods (e.g. changing or adding items/characters/scenes).
- Start Microsoft Visual Studio and select
Create New Project. - Choose
Class Library (.NET Framework). - Name your project
ExampleMod(if you choose another name make sure that your namespace and assembly name are correct).NET Framework 4.7.2as theFramework. If this option is not available for you, Download it here (Developer Pack). - Now that your project is setup, set your build path to the
Modules/ExampleMod/bin/Win64_Shipping_Clientdirectory in your game files. - Reference the
TaleWorlds.*DLLs in thebin\Win64_Shipping_Clientdirectory of your game files (not your module directory). - Reference the DLLs for each official module in
Modules\ModuleName\bin\Win64_Shipping_Client.
Before setting up a project, it is important to know that this is not required for basic mods (e.g. changing or adding items/characters/scenes). It is assumed you have basic VSCode knowledge(how to install extensions, how to create .NET projects).
SDK
Since Mount & Blade 2: Bannerlord is written using the .NET Framework 4.7.2 you'll have to install the .NET SDK(don't worry about how we'll compile for .NET Framework we'll come back to it). Luckily it is rather easy to install as Microsoft has a comprehensive tutorial here on how to do it both manually and scripted. You can choose either but the scirpted one is easier to do.
After installing the latest .NET version run the following command in the terminal:
which dotnet
If a path is printed the SDK was successfully installed. If not check if the directory /home/<YourUserName>/.dotnet exists. If it does run the following command to add it to your PATH variable.
Note that this will not be carried over into the next session. Please check how to permanently add PATH variables if you wish to do that.
export PATH=$PATH:/home/<YourUserName>/.dotnet/
If you now run the previous which command again you should see a path being printed.
IDE
As Visual Studio is a Windows-only application we'll use Visual Studio Code. It is of the utmost importance that you install VSCode using the command line as it is otherwise possible that VSCode is unable to open a terminal instance, making it impossible for it to find the installed .NET SDK. Luckily Visual Studio Code has a tutorial here which covers multiple distros such as Ubuntu, Fedora, Arch, etc.
Once you've followed your distobution specific installation guide run the following command:
code --version
A result simiar to this should be printed:
1.102.3
488a1f239235055e34e673291fb8d8c810886f81
x64
This will confirm that Visual Studio Code has been successfully installed.
Once done start Visual Studio Code by typing:
code
Final setup
Once Visual Studio Code has started and you have not received an error message of the .NET SDK not being located a few extensions have to be installed:
- C# Dev Kit
- vscode-solution-explorer(Optional but very helpful)
Once all extensions are installed create a new Class Library project. Edit the *.csproj file(either provided as a plain text file in the Explorer or Right Click->'Open file' using the vscode-solution-explorer) and modify it to look like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- We target multiple Frameworks just to make sure we can at least compile for .NET 8.0(might be a different version depending on what you've installed) -->
<TargetFrameworks>net8.0;net472</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<!-- This NuGet Package enables us to compile for the .NET Framework 4.7.2 by creating a dependency to the requested version -->
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" PrivateAssets="All" Version="1.0.0-preview.2"/>
<!-- You can also reference the TaleWorlds.*.dll files in the actual bin/Win64_Shipping_Client/ directory of the game files but you'll get some unnecessary Warnings because of some C++ *.dlls that way -->
<Reference Include="bin/Debug/net472/TaleWorlds.*.dll"/>
</ItemGroup>
</Project>Now you should be set up to make Mount & Blade 2: Bannerlord on Linux.
- Open your project properties and go to the
Debugtab. - Select the
Start external programoption and then browse forBannerlord.exelocated in thebin\Win64_Shipping_Clientdirectory in your game files (not your module directory). - Set your working directory to the
bin\Win64_Shipping_Clientdirectory in your game files (not your module directory). - Add the following command line arguments (be sure to replace ExampleMod with the name of your module):
/singleplayer _MODULES_*Native*SandBoxCore*CustomBattle*SandBox*StoryMode*ExampleMod*_MODULES_
- Open your project properties and go to the
Debugtab. - Select the
Start external programoption and then browse forTaleWorlds.MountAndBlade.Launcher.exelocated in thebin\Win64_Shipping_Clientdirectory in your game files (not your module directory). - Set your working directory to the
bin\Win64_Shipping_Clientdirectory in your game files (not your module directory).
-
Create a new class in your VS Project and name it
MySubModule, then open it. -
Add the following using directives to your class:
using TaleWorlds.Library; using TaleWorlds.Localization; using TaleWorlds.MountAndBlade;
-
Inherit from the
MBSubModuleBaseclass. -
Setup an override for the
OnSubModuleLoad()inherited method. -
Add the following code to your override method:
Module.CurrentModule.AddInitialStateOption(new InitialStateOption("Message", new TextObject("Message", null), 9990, () => { InformationManager.DisplayMessage(new InformationMessage("Hello World!")); }, () => { return (false, null); }));
-
Compile your project and confirm that it was outputted to
Modules\ExampleMod\bin\Win64_Shipping_Client. -
Open the Bannerlord launcher and navigate to
Singleplayer>Modsthen make sure that your mod is ticked and start the game. -
On the title screen, you should now see a button called
Message, click it and you should seeHello Worlddisplayed in the bottom-left corner of your screen (in chat). -
You have now successfully created your first Bannerlord mod!
If you use Visual Studio 2019 or higher, you can also use this Bannerlord Module Template to create a basic C# module automatically!