Referencing to a custom HeddifDefOf inside C# code

Started by CrazyMalk, April 17, 2018, 07:42:34 PM

Previous topic - Next topic

CrazyMalk

So, I created a projectile that aplies a custom disease when it hits the target, but when i reffer to "HediffWithComps.HediffWithComps_Petrification", i get the following error:
Error CS0117 'HediffWithComps' does not contain a definition for 'HediffWithComps_Petrification'

What should I do?

jamaicancastle

You'll need to create a new instance of a hediff from its def in order to apply it, like so:
Hediff hediff = HediffMaker.MakeHediff(HediffDef, target, null);

To fetch the hediff def, there are two methods. One is by name:
HediffDef.Named("defName")

The other is to create a class to hold it:
[DefOf]
public static class MyDefs
{
    public static HediffDef defName;
}

In this case, "defName" must exactly match the name of a def in your mod, which will then be fetched when the game loads and saved in that spot (as opposed to the previous method, which fetches every time it's run, so this is faster). In code, you refer to it like so:
MyDefs.defName

A third option is to put a Hediff field in the def of your projectile (or one of its comps or mod extensions). This will perform a similar search by name, and then you can grab the value through something like this:
projectile.def.targetHediff
or
projectile.GetComp<Comp_GiveHediff>().Props.hediff
where the exact names of the variables will match whatever structure you give the def itself.

CrazyMalk

Thanks! Is there any mod that uses something simillar? I'm not a programmer, so I need an existing file to base myself on ._. I do not even know how to start the new class :/