Difference between revisions of "Modding Tutorials/Modifying classes"
Jump to navigation
Jump to search
(Created page with "{{BackToTutorials}} =Changing methods= Use Harmony. =Adding fields or methods to classes= You can't. Not even with Modding Tutorials/Harmony...") |
|||
Line 15: | Line 15: | ||
=Adding behaviour to the Hediff class= | =Adding behaviour to the Hediff class= | ||
HediffWithComps also support comps. HediffComps are different from [[Modding Tutorials/ThingComp|ThingComp]]s, but their general use is the same. | HediffWithComps also support comps. HediffComps are different from [[Modding Tutorials/ThingComp|ThingComp]]s, but their general use is the same. | ||
+ | |||
+ | =Patching the base class of a subclass= | ||
+ | [[Modding Tutorials/Harmony|Harmony]] can only patch methods that actually have IL code. What you can do is patch the parent class. Let your patch take the __instance of the parent and pattern match. | ||
+ | <source lang="csharp"> | ||
+ | if (!(__instance is SubClass)) | ||
+ | return; | ||
+ | </source> |
Revision as of 10:01, 30 January 2019
Changing methods
Use Harmony.
Adding fields or methods to classes
You can't. Not even with Harmony. Maybe you want to subclass instead?
Adding fields to Defs
You can. The game support something called a DefModExtension. If you have a Def, you can get your mod extension from it. This allows you to add any custom field to any def.
Adding behaviour to the Thing class
If it's a ThingWithComps, you can add a ThingComp instead. Almost all Things are a ThingWithComps. ThingComps can also be used to store whatever data you need.
Adding behaviour to the Hediff class
HediffWithComps also support comps. HediffComps are different from ThingComps, but their general use is the same.
Patching the base class of a subclass
Harmony can only patch methods that actually have IL code. What you can do is patch the parent class. Let your patch take the __instance of the parent and pattern match.
if (!(__instance is SubClass)) return;