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.
 

punchkid

Recruit
Did you find a solution to this?
I am trying to add an extra menu under the arena in towns, but get the same KeyNotFoundException you are getting.
I am using the OnGameLoadedEvent and OnNewGameCreatedEvent which are the same events that are used by the game to add all the town, village, keep menus. Seems very odd that those aren't called first.
This is on 1.6.4 btw
 
Did you find a solution to this?
I am trying to add an extra menu under the arena in towns, but get the same KeyNotFoundException you are getting.
I am using the OnGameLoadedEvent and OnNewGameCreatedEvent which are the same events that are used by the game to add all the town, village, keep menus. Seems very odd that those aren't called first.
This is on 1.6.4 btw
Use OnSessionLaunchedEvent in your case.
Town, Castle, Village and Hideout menus are all loaded first, no particular issue with those menus.
But if you try to add sub menus to the encounter menus linked with EncounterGameMenuBehavior (like "town_outside", "castle_outside"), then it will fail because the mod menus are called first.
Look here for similar issue
 

Dejan

Community Manager
WBNWVCM&B
Hey, the following change has been implemented with v165 in relation to your request:
  • Added OnAfterSessionStart event.
 

punchkid

Recruit
Use OnSessionLaunchedEvent in your case.
Town, Castle, Village and Hideout menus are all loaded first, no particular issue with those menus.
But if you try to add sub menus to the encounter menus linked with EncounterGameMenuBehavior (like "town_outside", "castle_outside"), then it will fail because the mod menus are called first.
Look here for similar issue
Thanks, exactly what I wanted, appreciate it
 
Top Bottom