Learning C# by making a mod for fun

Users who are viewing this thread

Kimmaz

Recruit
Hello, I want to try and make a mod for Bannerlord and learn the basics of C# along the way. I have downloaded Windows Visual Studio Community as per w3schools suggestion, and I used a free program "dotpeek" to look inside the .dll files of the game. So far I don't understand that much of the code but I can see the patterns. So far I have copied a few codes and saved them so I can look at them later.

I first want to make a simple mod, that I probably won't use. Here is the plan: I want to alter or add to the code for desertion so that there is a new way that causes soldiers to leave the party in addition to the ways the game intends, not instead of them. So far I have a submodule file, and I start editing in Visual Studio.

Am I on to something, does this make sense to you so far? It's not finished. How much of the game file's code do I need to repeat? does it gather stuff from the original or do I need to put everything in my file?
C#:
using Helpers;
using Helpers;
using SandBox;
using System;
using System.IO;
using System.Reflection;
using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;
using TaleWorlds.Engine;
using TaleWorlds.Engine.Screens;
using TaleWorlds.InputSystem;
using TaleWorlds.MountAndBlade;

namespace DeadlyHorses
{
    public void DailyTickParty(MobileParty mobileParty)
    {
      if (!Campaign.Current.DesertionEnabled || !mobileParty.IsActive || (mobileParty.IsDisbanding || mobileParty.Party.MapEvent != null) || !mobileParty.IsLordParty && (!mobileParty.IsGarrison || mobileParty.CurrentSettlement == null) && !mobileParty.IsCaravan)
        return;
      TroopRoster desertedTroopList = (TroopRoster) null;
      if (mobileParty.IsLordParty || mobileParty.IsCaravan)
        this.PartiesCheckDesertionDueToDeadlyHorses(mobileParty, ref desertedTroopList);
      if (desertedTroopList != (TroopRoster) null && desertedTroopList.Count > 0)
        CampaignEventDispatcher.Instance.OnTroopsDeserted(mobileParty, desertedTroopList);
      if (mobileParty.Party.NumberOfAllMembers > 0)
        return;
      mobileParty.RemoveParty();
    }
}

    public bool PartiesCheckDesertionDueToHorses(
      MobileParty mobileParty,
      ref TroopRoster desertedTroopList)
    {
      int MenWithHorse = CampaignSystem.Track.NumberOfMenWithHorse
      int PackAnimals = CampaignSystem.Track.NumberOfPackAnimals
      if (mobileParty.Party.MenWithHorse <= mobileParty.Party.PackAnimals)
      {
      ++this._numberOfDesertersFromLordParty;
      flag = true;
      }
      return flag;
    }
  }
}
}
 
Last edited:
The best way to learn C# is to go through a proper tutorial. For practicing, I would advise against trying to create a game mod and instead experiment with console applications, which will offer much faster feedback. Doing things this way will be much more beneficial in the long run.
 
Agreed with Ster, there are WAY too many moving parts in this mod system that will cause issues you won't be able to find the source of. What's more, you're basing your code not off of the original code but the decompiled DLL which looks quite different--it will not exhibit great coding practices to learn from. Unfortunately, this kind of modding just isn't for people who lack programming experience. I'm a professional software engineer (albeit not a C# one) and it's a huge pain in the ass even for me. And that's just the way it's going to be until the game is complete and the mod tools have been released.

Before I could afford to go to school for computer science, I taught myself with online resources. To drive that learning, I tried to make something that I wanted, something that would make my life even just a little bit easier. That let me go past hello world tutorials and start making something from scratch, because I envisioned what I wanted and sought out everything I needed to learn to execute on it.

I know that what you want to make right now is a C# mod for Bannerlord, so this isn't a satisfying answer for you. But it is I think the best answer I can give you: Find something else that you can make for yourself. Something from scratch.
 
I am in the process of learning C#. This langage is bulky and grammatically unbearable as hell compared to other langages, but no choice. From my experience in code modding many games (especially Rigale for Warband, but many others), the best results I got were from learning from others, and reverse engineering stuff. I usually identified a mod that was close to what I wanted, or that had part of the effect, would read the code, and then tweak it a tiny bit. I would be in awe, that would give me the joy and energy to keep going, then, a little more, discovering new stuff in the process, then more, until I could create a function, then, a system. However, this was slow, and not quite time efficient, although really nice for intellectual challenge.
However, C# is another beast. I am right now int the process of learning it from scratch, because the way it works in Bannerlord is full of stuff I don't get. So after trying the old way with some little success, https://www.nexusmods.com/mountandblade2bannerlord/mods/182 (adding a few gold and XP to random skill every day) I am reading and watching (and practising) C# tuts. There are LOADS of them, on every subjects. Sometimes I go back to BL, and I discover that some stuff make a little more sens. And I go back to my tuts.
From my experience and for me, learning a new coding langage is really a "read and practise until it clicks" process.
If that confinment can give me that, it will have been well invested.

Anyways, keep up the good work mate. I wish you all the best.
 
You do need to understand not just basic but also some advanced features of C# if I should speak for the current state of Bannerlord modding. The developers don't use only basic stuff, naturally, in the .dll files of Bannerlord that one references for modding. A modder needs to understand OOP first of all, then stuff like generics, annotations, object referencing & pointers and more. So, you do need to grasp C# to a certain degree before daring to make proper mods, I would say.

As I read some of the posts here and in other threads, I realized people are being quite unfair about C# as the modding language. My observation is that they complain about the very abstract and "not easy to learn" features and concepts of C# that junior programmers usually are usually unfamiliar with. For experienced programmers and for those who study fields such as Computer Science, using C# for modding is a bliss compared to that "Python" compiler we used as module system in Warband. It was like a damn assembler :grin:

I advice learning C# instead of complaining about its hardness to learn. It is a very popular language and capable of doing many things. Once you know enough of it, you will realize you can do whatever you want with Bannerlord now. I even consider making a voice chat mod after they allow us to host dedicated servers. Imagine trying to make that with Warband module system...
 
To everyone who wants to learn C# i suggest taking advantage of the free month of training offered by Pluralsight.com ("Free April").
 
It won't be an easy road, but a very rewarding one. Best of luck, and should you have any questions about C#. fire away at my discord: kommissar#8990
 
The best way to learn C# is to go through a proper tutorial. For practicing, I would advise against trying to create a game mod and instead experiment with console applications, which will offer much faster feedback. Doing things this way will be much more beneficial in the long run.

I learned c# by modding gta… some mod I installed was broken and I wanted to fix it, had no idea what I was doing, went on irc for help, got it fixed then kept trying to add more stuff... did that for a few years. I would suggest skip the tutorials. If something in front of you is foreign, you have google...

these websites will solve any problem you have in c#...

MSDN
Stackoverflow
dotnetperls (for beginners, this will be your go to, not MSDN)

If you put in the time you will learn while modding. Any time I came across a weird word like "namespace" or "delegate" … dotnetperls explained it. if you do it everyday and read a lot you can skip hello world. The problem with tutorials were the examples they always use, I never felt like you could apply them anywhere. Sometimes learning something and just how to make it work is enough and when you can make functional code then the underworkings and how things actually work reveal themself over time as you become familiar... it is somehting that will happen naturally instead of stressing with tutorials and trying to learn NOW. I learned I guess by attempting to make my ideas and instead writing tons of bugs that i had to then learn how to kill and i spent many hours on stackoverflow trying to get things straight. You drop someone off in mexico with an iphone translator... that is the best way to make them learn spanish :wink:
 
Last edited:
I learned c# by modding gta… some mod I installed was broken and I wanted to fix it, had no idea what I was doing, went on irc for help, got it fixed then kept trying to add more stuff... did that for a few years. I would suggest skip the tutorials. If something in front of you is foreign, you have google...

these websites will solve any problem you have in c#...

MSDN
Stackoverflow
dotnetperls (for beginners, this will be your go to, not MSDN)

If you put in the time you will learn while modding. Any time I came across a weird word like "namespace" or "delegate" … dotnetperls explained it. if you do it everyday and read a lot you can skip hello world. The problem with tutorials were the examples they always use, I never felt like you could apply them anywhere. Sometimes learning something and just how to make it work is enough and when you can make functional code then the underworkings and how things actually work reveal themself over time as you become familiar... it is somehting that will happen naturally instead of stressing with tutorials and trying to learn NOW. I learned I guess by attempting to make my ideas and instead writing tons of bugs that i had to then learn how to kill and i spent many hours on stackoverflow trying to get things straight. You drop someone off in mexico with an iphone translator... that is the best way to make them learn spanish :wink:
I learned C# by modding games as well. The issue with this is that you don't get exposed to everything. It took me over a year to finally find out about reflection and 2 years to properly understand how delegates and events actually work. Mostly because I never really knew about them or felt that I needed to use them. This also taught me bad habits such as never using properties (fields only), writing only synchronous code, and never using proper design patterns. With over 6 years of experience in C# now, I can say that the best way to learn is to actually go through a proper tutorial first, then experiment with basic programs that offer fast feedback (e.g. Console app). This will expose you to many features of C# that will make you a much better and more efficient programmer.
 
I learned C# by modding games as well. The issue with this is that you don't get exposed to everything. It took me over a year to finally find out about reflection and 2 years to properly understand how delegates and events actually work. Mostly because I never really knew about them or felt that I needed to use them. This also taught me bad habits such as never using properties (fields only), writing only synchronous code, and never using proper design patterns. With over 6 years of experience in C# now, I can say that the best way to learn is to actually go through a proper tutorial first, then experiment with basic programs that offer fast feedback (e.g. Console app). This will expose you to many features of C# that will make you a much better and more efficient programmer.
maybe I had a little luck because when I went to irc I kept sharing my code that I was making with people... and they would ask me what the **** i was doing and constantly correct my nonsense. They even would tell me exactly what to do and even then it was all gibberish to me, I didn't want to be "that guy" so I only ever asked a question once and when they told me stuff I didn't understand I would read it a million times and google every word that didn't make sense... eventually it clicked. This way you are hands on doing something that you have a vision for and you experience every speed bump. Tutorials do not teach you how to write bugs and correct them. Only jumping right in and deciphering nonsense is how I connected the dots. I just feel like tutorials are so abstract. And if you tell a beginner to work on console apps then you will get nothing out of them. You need ideas for stuff to make in order for that to work. Following steps in tutorials will not help absorb the mechanics because you are just following steps in a cookbook. You need to make stuff from scratch and figure out what tastes good and what tastes bad and why it tastes bad. I feel like tutorials might be a good place if you have a concept in your mind already that you are unfamiliar with... like "how to read text from a file and store it somewhere" … when you need to know stuff like that then I think tutorials help big time.
It is true everyone learns different so what worked for me may not for others but in my opinion exposure to actual coding teaches you so much more than following tutorials where you simply learn step 1, step 2, etc.

All the class stuff and using properties is technically not necessary... this is more for the developer to stay organized... you can write code with all public fields or whatever, none of that matters as long as it works... hell you can put your entire program in the main function and just execute each line and use no methods or wrappers and be inefficient... for beginners it won't matter... eventually they will keep doing it and realize there are better ways but at least they are creating functional code. That's all that matters.
 
I really like C#, but I'm coming from php/javascript. One of the best learning resources for me coming over to C# in a gaming context was the oficcial Unity beginner and intermediate scripting tutorials. They are specifically for Unity, but they run over a lot of very important features that you absolutely need to understand when writing C#.

Beginner scripting:


Intermediate scripting:


Actually, my advice for anyone wanting to mod Bannerlord would be to spend a month learning Unity - and by learning I mean run through their training videos from start to finish, don't just try to figure it out by yourself. Make a tiny, tiny little beginner project. You'll learn heaps not only about how to write code syntax, but how to structure code, how a lot of these systems work, how to find bugs in your code and IDE/tooling options you didn't know were available. Of course, always have that awesome Bannerlord mod in mind though.
 
Back
Top Bottom