Skip to content

SHORTTUTORIAL: Extend Thingdef

roxxploxx edited this page May 1, 2017 · 11 revisions

So you want to be able to... add new tags to when you're defining a new Thing?

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, create a new Thing (ex. "AThing") using the above ThingDef (ex. "AThingDef").
  4. In XML, set the value of that attribute (ex. "2").
  5. In code, when referencing your attribute on the def, cast the def 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='...'.
  • Properties are nice to have since you can store multiple values (think Comps). To add something link Comps, to your newly defined Def (ex. "AThingDef") use: public List aprops = new List();

External Refs

Clone this wiki locally