How do I access disease defs in C# code?

Started by HeroShadow, November 17, 2018, 07:32:05 PM

Previous topic - Next topic

HeroShadow

In xml I have an item whose code I copied from HealerMechSerum. I made my own CompUseEffect class and was able to make it so that I can tell a colonist to "use the item" and receive a Hediff(such as Flu, Malaria, AlcoholHigh, etc.). However, I can only access some diseases, specifically Flu, Malaria, and Plague with HediffDefOf.Flu. But what I really want to access is the fibrous and sensory mechanite diseases. In the xml files, I found the defs for them: SensoryMechanites and FibrousMechanites, which have the parent MechaniteBase. But there is no such thing as HediffDefOf.SensoryMechanites for some reason.

In other parts of the C# code I've found parts of what I'm looking for, like the HediffGiverUtility.TryApply() function, which takes in a HediffDef as an argument. I found it being referenced like this:
HediffGiverUtility.TryApply(pawn, this.def.diseaseIncident, this.def.diseasePartsToAffect, false, 1, null);
The diseaseIncident property is of type HediffDef, even though there is no such thing as a diseaseIncident in HediffDef or HediffDefOf!!

My goal is to be able to do this:

var disease = SomeClass.SensoryMechanites;
HediffGiverUtility.TryApply(pawn, disease, ...);


Any help is appreciated.

aRandomKiwi

Hi,

Try with that :


HediffDef disease = DefDatabase<HediffDef>.GetNamed("SensoryMechanites");   // in parameter here the defName of your desease
//and then :
HediffGiverUtility.TryApply(pawn, disease, ...);


Generally all def class can be accessed with DefDatabase<DefClass>.GetNamed("defNameHere");

Fluffy (l2032)

it's also worth checking if a HediffDefOf class exists (it does). Most (if not all) defs that are directly referenced in the code will have a 'DefOf' class associated with them. Annoyingly, it appears that SensoryMechanites is not exposed, so you'll have to go with Kiwy404's suggestion.

Alternatively, you can create your own DefOf class by applying the [DefOf] attribute; any public static members will then get populated by the game - which actually uses exactly the same DefDatabase<Def>.GetNamed( x ) method behind the scenes.