Discussion Suggestion Modifying The Native Encounter Menu

Users who are viewing this thread

I'm trying to add two options to the encounter menu for when the player encounters a village being raided and he is neither a part of the attacking nor defending faction. The menu is called "join_encounter" and it is created with the non-serialized listener OnSessionLaunchedEvent in the EncounterGameMenuBehavior class. In my mod I use the same listener to add the menu options, but get a KeyNotFoundException, and debugging further reveals that by the time my code is called, only 46 menus have been created, none of them being the one I want to add options to. I tried the other listeners that have access to an instance of CampaignGameStarter, but the same error happens, sometimes with even less menus having been loaded.

Looking further into how the listeners are called shows that they are called in a linked list in an instance of MbEvent<T> and are executed in a first-in-last-out order. So basically, since my OnSessionLaunchedEvent listener is added last, it's called before the OnSessionLaunchedEvent listener in EncounterGameMenuBehavior, hence the KeyNotFoundException since the code that creates that menu hasn't yet been called by the time my code is called. I found a workaround by accessing the current CampaignGameStarter instance with SandBoxManager.Instance.GameStarter in a TickEvent listener, but this doesn't seem like the right way to do it, and adding an extra TickEvent listener probably isn't too good for performance. This problem doesn't seem to happen when modifying other menus from what I've heard in the modding discord.

If the call order of listeners is somehow integral to other parts of bannerlord and can't be changed, I want to know what the proper way to modify the "join_encounter" menu is, since the solution I have now doesn't feel right, but it also doesn't seem right that my listeners should be called before native ones. If this behavior is not intended by the devs, then I hope they change it to make modding the "join_encounter" menu as easy as modding other native menus.
 
I'm trying to add two options to the encounter menu for when the player encounters a village being raided and he is neither a part of the attacking nor defending faction. The menu is called "join_encounter" and it is created with the non-serialized listener OnSessionLaunchedEvent in the EncounterGameMenuBehavior class. In my mod I use the same listener to add the menu options, but get a KeyNotFoundException, and debugging further reveals that by the time my code is called, only 46 menus have been created, none of them being the one I want to add options to. I tried the other listeners that have access to an instance of CampaignGameStarter, but the same error happens, sometimes with even less menus having been loaded.

Looking further into how the listeners are called shows that they are called in a linked list in an instance of MbEvent<T> and are executed in a first-in-last-out order. So basically, since my OnSessionLaunchedEvent listener is added last, it's called before the OnSessionLaunchedEvent listener in EncounterGameMenuBehavior, hence the KeyNotFoundException since the code that creates that menu hasn't yet been called by the time my code is called. I found a workaround by accessing the current CampaignGameStarter instance with SandBoxManager.Instance.GameStarter in a TickEvent listener, but this doesn't seem like the right way to do it, and adding an extra TickEvent listener probably isn't too good for performance. This problem doesn't seem to happen when modifying other menus from what I've heard in the modding discord.

If the call order of listeners is somehow integral to other parts of bannerlord and can't be changed, I want to know what the proper way to modify the "join_encounter" menu is, since the solution I have now doesn't feel right, but it also doesn't seem right that my listeners should be called before native ones. If this behavior is not intended by the devs, then I hope they change it to make modding the "join_encounter" menu as easy as modding other native menus.
I may be wrong but this issue appeared with 1.6.1 when they changed the loading order.
After that patch it was no more possible to add additional menu (AddGameMenu) & options (AddGameMenuOption) to the EncounterGameMenuBehavior menus ("town_outside", "castle_outside").
Apparently with 1.6.4 they changed something again and I'm wondering if we can once again mod those menus.
 
Top Bottom