• Please note that we've updated the Mount & Blade II: Bannerlord save file system which requires you to take certain steps in order for your save files to be compatible with e1.7.1 and any later updates. You can find the instructions here.

BL Coding Adding custom methods to InventoryManager

Users who are viewing this thread

zervion

Recruit
Hello everyone!

I looked around but couldn’t find an answer online or on the forums. I have been getting back into creating simple mods. So I am familiar with things like Harmony but also adding menu options in towns.

Now I have run into the problem that I want to add my own custom methods to the InventoryManager (located in TaleWorlds.CampaignSystem). At this moment in time I cant figure out how to do it. Since I cant create my own method with custom logic that sets the private setters in the manager.

InventoryManager.Instance._currentMode = InventoryMode.Default;
InventoryManager.Instance._inventoryLogic = new InventoryLogic(Campaign.Current, (PartyBase) null);

Those are two examples that are currently set inside the class but I cant extend or overwrite those in a custom class. Does anyone have a solution?

Kind regards,
Zervion
 
C#:
[HarmonyPatch(typeof(InventoryManager), "MethodYouWantToPatch")]
public static void Postfix(ref InventoryMode ____currentMode, ref InventoryLogic ____inventoryLogic)
{
    ____currentMode = InventoryMode.Default;
    ____inventoryLogic = new InventoryLogic(Campaign.Current, (PartyBase) null);
}
 
Upvote 0

zervion

Recruit
Thank you for your reply!
I understand you can patch existing methods, but how do I add a new method with more parameters?
Otherwise I will override logic that I dont really want or need to change.
 
Upvote 0
What do you mean by add a new method with more parameters? You can put if statements in the patch and have it call another method if you want to make it more conditional.

C#:
[HarmonyPatch(typeof(InventoryManager), "MethodYouWantToPatch")]
public static void Postfix(ref InventoryMode ____currentMode, ref InventoryLogic ____inventoryLogic)
{
    if (YourCondition)
    {
        ____currentMode = InventoryMode.Default;
        ____inventoryLogic = new InventoryLogic(Campaign.Current, (PartyBase) null);
        MethodYouWantToCall();
    }
}
public static void MethodYouWantToCall()
{
    // Do your stuff.
}
 
Upvote 0
Top Bottom