static void Postfix(LordJob_FormAndSendCaravan __instance, ref StateGraph __result) { // This patch modifies the return value of LordJob_FormAndSendCaravan.CreateGraph // The CreateGraph function generates a finite state machine that controls caravan formation. // Each state is a LordToil. // We need to make a few changes to that state machine. Traverse tthis = Traverse.Create(__instance); IntVec3 meetingPoint = tthis.Field("meetingPoint").GetValue <IntVec3>(); // All we need to do is replace the GatherAnimals and GatherSlaves LordToils with our versions. // OLD VERSIONS LordToil oldGatherAnimals = tthis.Field("gatherAnimals").GetValue <LordToil>(); LordToil oldGatherSlaves = tthis.Field("gatherSlaves").GetValue <LordToil>(); // OUR NEW VERSIONS LordToil_BillyCaravan_GatherAnimals billyGatherAnimals = new LordToil_BillyCaravan_GatherAnimals(meetingPoint); LordToil_BillyCaravan_GatherSlaves billyGatherSlaves = new LordToil_BillyCaravan_GatherSlaves(meetingPoint); // REPLACE member variables with new versions tthis.Field("gatherAnimals").SetValue(billyGatherAnimals); tthis.Field("gatherSlaves").SetValue(billyGatherSlaves); // REPLACE states in state machine with new versions __result = __result.ReplaceState(oldGatherAnimals, billyGatherAnimals); __result = __result.ReplaceState(oldGatherSlaves, billyGatherSlaves); }