# Basic C# Mod
# Introduction
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.
# Preparation
# For this tutorial, we will be naming our project ExampleMod
.
# Setting up your Module (SubModule.xml)
Go to your game files and locate the
Modules
directory.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
bin
and inside this directory, create a new folder calledWin64_Shipping_Client
.Create a new
SubModule.xml
file (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:
MySubModule
is 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.
# Setting up your Project
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.2
as theFramework
. If this option is not available for you, Download it here (opens new window) (Developer Pack). - Now that your project is setup, set your build path (opens new window) to the
Modules/ExampleMod/bin/Win64_Shipping_Client
directory in your game files. - Reference (opens new window) the
TaleWorlds.*
DLLs in thebin\Win64_Shipping_Client
directory of your game files (not your module directory). - Reference (opens new window) the DLLs for each official module in
Modules\ModuleName\bin\Win64_Shipping_Client
.
# Debugging your Project (Optional)
# Way 1 (Preferred)
- Open your project properties and go to the
Debug
tab. - Select the
Start external program
option and then browse forBannerlord.exe
located in thebin\Win64_Shipping_Client
directory in your game files (not your module directory). - Set your working directory to the
bin\Win64_Shipping_Client
directory 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_
# Way 2 (If you want to start your debugging from launcher window)
- Open your project properties and go to the
Debug
tab. - Select the
Start external program
option and then browse forTaleWorlds.MountAndBlade.Launcher.exe
located in thebin\Win64_Shipping_Client
directory in your game files (not your module directory). - Set your working directory to the
bin\Win64_Shipping_Client
directory in your game files (not your module directory).
# Programming
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
MBSubModuleBase
class.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
>Mods
then 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 World
displayed in the bottom-left corner of your screen (in chat).You have now successfully created your first Bannerlord mod!
# Module Templates
If you use Visual Studio 2019 or higher, you can also use this Bannerlord Module Template (opens new window) to create a basic C# module automatically!