How do I edit the game code in my mod?

Started by Knight, July 20, 2018, 09:07:36 AM

Previous topic - Next topic

Knight

I'd like to use Harmony to patch a single field on the game start. For example, I want to patch the bool:

public bool requiresBedForSurgery = true; to false. I've tried using Harmony but it hasn't worked so far because I'm fairly certain you can only patch methods with it. Here's what I tried:

[HarmonyPatch(typeof(FleshTypeDef))]
[HarmonyPatch("requiresBedForSurgery")]
static class SurgeryBedPatch
{
    static void Postfix(ref bool __result)
    {
        __result = false;
        Log.Message("Patched SurgeryBed Bool");
    }
}


And this is the error that appears in the console:

Could not execute post-long-event action. Exception: System.TypeInitializationException:
An exception was thrown by the type initializer for Main ---> System.ArgumentException: No target method specified for class SurgeryBedPatch


Am I overlooking something simple or is this not possible? I'd like to universally change this bool at the game start and not just in one particular scenario so I can't just use code to make this happen (I already tried that anyway). Thanks!

Mehni

Wrong approach for the problem.

I recommend just making a [StaticConstructorOnStartUp] and change the def in there. Or an xpath patch on all 3(!!!) fleshtypedefs RimWorld has.

Knight

Thanks but I ended up using Harmony but instead referencing a different method which is more top-level. In the end I went with this:

[HarmonyPatch(typeof(Pawn))]
[HarmonyPatch("CurrentlyUsableForBills")]
static class SurgeryBedPatch
{
    static void Postfix(ref bool __result)
    {
        __result = true;
    }
}


Which achieves the same thing by literally making the pawn always ready for surgery, bed or not. It basically bypasses the need for the requiresBedForSurgery bool. That's worked perfectly so far but of course I've got to do some balancing to stop issues arising from the fact that pawns are always surgery ready now.