SHORTTUTORIAL: Extend Thingdef - roxxploxx/RimWorldModGuide GitHub Wiki

So you want to be able to... add new tags to a <Def> when you're defining a new Def (ex. ThingDef)?

Quick Steps

  1. In code, create a new class (ex. "AThingDef") in your namespace (ex. "A") that inherits from ThingDef.
  2. In code, add an attribute to this class (ex. "public int A = 1;").
  3. In XML, define a new ThingDef with your new thingClass (ex. "AThing"), setting the Class of ThingDef to the above ThingDef (ex. "AThingDef").
  4. In XML, set the value of that new attribute (ex. "2").
  5. In code, when referencing your attribute on the def, cast the ThingDef appropriately (ex. " int localA = ((AThingDef)this.def.A).A;").

C# Example

[StaticConstructorOnStartup]
public class AThingDef: Verse.ThingDef { public int A = 1; }

XML Example

<ThingDef Class="A.AThingDef">
    ...
    <thingClass>A.AThing</thingClass>
    <A>2</A>
    ...
</ThingDef>

Additional Comments

  • 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 of Class='...'.

External Refs

⚠️ **GitHub.com Fallback** ⚠️