SHORTTUTORIAL: Extend Thingdef - roxxploxx/RimWorldModGuide GitHub Wiki
- In code, create a new class (ex. "AThingDef") in your namespace (ex. "A") that inherits from ThingDef.
- In code, add an attribute to this class (ex. "public int A = 1;").
- In XML, define a new ThingDef with your new thingClass (ex. "AThing"), setting the Class of ThingDef to the above ThingDef (ex. "AThingDef").
- In XML, set the value of that new attribute (ex. "2").
- In code, when referencing your attribute on the def, cast the ThingDef appropriately (ex. " int localA = ((AThingDef)this.def.A).A;").
[StaticConstructorOnStartup]
public class AThingDef: Verse.ThingDef { public int A = 1; }
<ThingDef Class="A.AThingDef">
...
<thingClass>A.AThing</thingClass>
<A>2</A>
...
</ThingDef>
- Why or WTF? Since Things are generated from a ThingDef, and a ThingDef is pointed back to by each Thing (via 'this.def'), then by creating a new ThingDef, the Thing is created with these additional attributes.
- Why not
<AThingDef>...</AThingDef>
in the XML file directly? Well sure, go for it. But, if you intend to reference a your new object as a ThingDef or any other specific type, it won't work because it isn't aware of inheritance and you will get unable to resolve errors. That's the magic ofClass='...'
.