Need help debugging

Users who are viewing this thread

bunta

Recruit
So currently I am making a mod that allows you to free captured clan members from a click of a button from any town you own. The menu works how I want it to its just what happens after the menu logic that is not working. My main problem is that after you select the hero from the menu to free it does not trigger the ransom offer popup. Instead it only works until you receive a ransom offer from a random person and then when you accept it, it ransoms the selected prisoner from the menu.

Here is the class that has all my problems:

internal class Behavior : CampaignBehaviorBase
{
public static Hero? Family;

public override void RegisterEvents()
{
CampaignEvents.OnSessionLaunchedEvent.AddNonSerializedListener(this, OnSessionLaunched);
CampaignEvents.OnRansomOfferedToPlayerEvent.AddNonSerializedListener(this, OnRansomOfferedToPlayer);
}

private void OnSessionLaunched(CampaignGameStarter starter)
{
AddGameMenus(starter);
}

private void AddGameMenus(CampaignGameStarter starter)
{
starter.AddGameMenuOption("town_keep", "free_family", "Ransom a family member", delegate (MenuCallbackArgs args)
{
args.optionLeaveType = GameMenuOption.LeaveType.Continue;
args.IsEnabled = true;
return true;
}, delegate
{
ShowCapturedMembers();
}, isLeave: false, 1);
}

private void ShowCapturedMembers()
{
if (Campaign.Current == null || Campaign.Current.AliveHeroes == null)
{
InformationManager.DisplayMessage(new InformationMessage("Error: Campaign data is not available."));
return;
}
List<InquiryElement> capturedFamilyMembers = new List<InquiryElement>();
foreach (Hero hero in Campaign.Current.AliveHeroes)
{
if (hero.Clan == Hero.MainHero?.Clan && hero.IsPrisoner && hero.IsLord && hero != null)
{
capturedFamilyMembers.Add(new InquiryElement(hero, $"{hero.Name} || Captured: {hero.CaptivityStartTime}", new ImageIdentifier(CharacterCode.CreateFrom(hero.CharacterObject))));
}
}
if (capturedFamilyMembers.Count < 1)
{
InformationManager.ShowInquiry(new InquiryData("No Family Members to Ransom", "You have no family members to ransom.", isAffirmativeOptionShown: true, isNegativeOptionShown: false, "Continue", "", null, null), pauseGameActiveState: true);
GameMenu.SwitchToMenu("town_keep");
return;
}
MBInformationManager.ShowMultiSelectionInquiry(new MultiSelectionInquiryData("Select Family Member to Ransom", "", capturedFamilyMembers, isExitShown: true, 1, 1, "Continue", null, delegate (List<InquiryElement> args)
{
if (args != null && args.Any())
{
InformationManager.HideInquiry();
ProcessSelectedFamilyMember(args.Select((InquiryElement element) => element.Identifier as Hero));
}
}, null));
}

private void ProcessSelectedFamilyMember(IEnumerable<Hero> selectedFamilyMembers)
{
try
{
Hero hero777 = selectedFamilyMembers?.First();
if (hero777 == null)
{
InformationManager.DisplayMessage(new InformationMessage("Error: No family member selected or the selection is invalid."));
}
else if (hero777.IsPrisoner && hero777 != Hero.MainHero && hero777 != null)
{
InformationManager.DisplayMessage(new InformationMessage($"Attempting to ransom {hero777.Name}, please wait some time."));
RansomOfferCampaignBehavior ransomOfferCampaignBehavior = Campaign.Current.GetCampaignBehavior<RansomOfferCampaignBehavior>();
ransomOfferCampaignBehavior.SetCurrentRansomHero(hero: hero777, ransomPayer: Hero.MainHero);
}
}
catch (Exception e)
{
InformationManager.DisplayMessage(new InformationMessage("Error: " + e.Message));
}
}

private void OnRansomOfferedToPlayer(Hero captiveHero)
{
InformationManager.DisplayMessage(new InformationMessage($"Ransom offered for {captiveHero.Name}"));
}

public override void SyncData(IDataStore dataStore)
{
}
}

I think the problem lies with OnRansomOffered event or SetCurrentRansomHero behavior but those are the only thing I could find that would possibly trigger the ransom offer popup.
I also have a working version with my own MapNotification but I don't want to have to do it this way. If anyone could help me figure out how I trigger the ransom offer popup that would be great.



I would also like to mention again that the code that creates the menu WORKS and is not the problem. its just what the menu is running is the problem.
 
Last edited:
Back
Top Bottom