Need help patching specific method with harmony

Users who are viewing this thread

Marek15

Veteran
I'm completely new to harmony and C# so i apologize if this is a silly issue.
While i have somehow managed patch a couple of methods, there is one i can't figure out inside TaleWorlds.CampaignSystem.CharacterDevelopment.Managers.SkillLevelingManager:
C#:
    internal static void OnHighMorale(MobileParty party) => SkillLevelingManager.OnLeaderSkillExercised(party.Party, DefaultSkills.Leadership, (float) MathF.Round((float) (0.00999999977648258 * (double) party.MemberRoster.TotalManCount * ((double) party.Morale - 70.0))));
All examples for how to use harmony show a original method with a return value or variable, but this method seems to have neither.
My goal is to alter the formula within MathF.Round() but i have no clue how to do so. I would be very grateful if someone could point me in the right direction.
 
Managed to figure it out on my own. Definitely a silly issue due to being completely new to C# and misunderstanding how harmony works.
1st mistake: harmony can patch methods without variables or return values. Just because all examples i could find use them doesn't mean they are required.
2nd mistake: the reason why the compiler gives the error " 'SkillLevelingManager' does not contain a definition for 'OnLeaderSkillExercised' " is because 'OnLeaderSkillExercised' is a private method. To fix it i just copied 'OnLeaderSkillExercised' into my own class and called that instead of the original.
Here is the working code i finally managed to write:
C#:
    [HarmonyPatch(typeof(SkillLevelingManager), "OnHighMorale")]
    internal class PatchOnHighMorale
    {
        private static void OnLeaderSkillExercised(PartyBase party, SkillObject skill, float skillXp)
        {
            party.LeaderHero?.HeroDeveloper.AddSkillXp(skill, skillXp, true, true);
        }

        public static bool Prefix(MobileParty party)
        {
            PatchOnHighMorale.OnLeaderSkillExercised(party.Party, DefaultSkills.Leadership, (float)MathF.Round((float)(0.00999999977648258 * (double)party.MemberRoster.TotalManCount * ((double)party.Morale - 70.0))));
            return false;
        }
    }
 
Upvote 0
Back
Top Bottom