Skip to content

SHORTTUTORIAL: Things and ThingDef and Components

Roxx Ploxx edited this page May 24, 2017 · 3 revisions

Confused?

If you are not confused about how Thing and ThingDef and CompProperties and ThingComp and ThingWithComps and Def and comps and def and so on, you haven't thought about this enough. It's confusing and only after a good bit of time did it mesh for me. If you understand this diagram, you can figure most everything out in Modding.

ThingDef and Thing Diagram

Bite Sized Chunks

  • The trick is that RimWorld stores the XML in ThingDef and then creates Thing (or ThingWithComps) as instances of the XML. It is akin to Classes(ThingDef) and Objects(Thing/ThingWithComps).

  • This same pattern applies to CompProperties and ThingComp, where ThingComps are created from a CompProperties which is referenced in the future as props.

  • The ExposeData method is used to copy data from a ThingDef to a Thing from XML or from a save file.

  • The PostExposeData method is used to copy data from CompProperties to ThingComp.

  • For your classes, create methods and local data on the Thing, ThingWithComp and ThingComp classes. Data to be treated as constant is referenced by "this.def" in Things and "this.props" in ThingComps.

  • Why do ThingDef and Thing both have comps? Well, to confuse you. It makes sense, but basically, one is for CompProperties and the other ThingComps. If you are in a ThingWithComps, use this.comps to get to your instantiated comps. But, if for some reason you want to use this.def.comps to see the XML data for your ThingDef, go for it. Or do this.comps[i].props to get a ThingComp's CompProperties. Or this.parent.def.comps[i]... nevermind... I'll stop.

  • Properties? Well, they store constant values like the BuildingProperties class. So, in Thing or ThingWithComps, you do this.def.building to reference a building's property as defined in a ThingDef's XML tag of '<building>'. If you want to add a new property, then create a new class extending ThingDef (see Extend Thingdef).

Drink from the Firehose of Knowledge

I will break this pdf into sections but if you can understand this, you're doing it right!

Modding Overview PDF