Modding Tutorials/Custom Comp Classes
Creating a custom comp class is a convenient way to add new functionality to RimWorld.
Prerequisites
- You need to have set up your editor and environment
- You need to know how to write custom code
- You should probably understand Defs at least somewhat.
def (xml) and C# structure
Basic outline
You will have some custom def and it will have something like this:
Defs (xml)
<ThingDef ...> ... ... <comps> <li Class="MyNamespace.MyCompProperties"> <myCustomCompProperty>some value</myCustomCompProperty> <mySecondCompProp>4</mySecondCompProp> </li> <li> <!-- this is kind of like <tag>MN.MyCustomTag</tag>:--> <compClass>MyNamespace.MyCustomThingComp</compClass> </li> </comps> </ThingDef>
C#
namespace MyNamespace // For example, LWM.ModName - by using your // handle/name/etc, you almost certainly guarantee uniqueness { //////////// <<nowiki>li</nowiki Class="MyNamespace.MyCompProperties"> //////////// public class MyCompProperties : CompProperties // Name this as you wish, of course { public Properties() { this.compClass = typeof(MyNamespace.MyLinkedCompThing); // rename as appropriate } public string myCustomCompProperty; // Name matches def, of course public int mySecondCompProp = 1; // Can set default values } // this ThingComp is used to actually access the comp property defined above // this is not "<compClass>MyNamespace.MyCustomThingComp</compClass>" public class MyLinkedCompThing : ThingComp { public string myCustomCompProperty { get { return ((MyCompProperties)this.props).myCustomCompProperty; } } public int mySecondCompProperty // Have to get all the names right { get { return ((MyCompProperties)this.props).mySecondCompProperty; } } //etc } //////////// <compClass>MyNamespace.MyCustomThingComp</compClass> //////////// public class MyCustomThingComp : ThingComp { public override void CompTick() { // do stuff } public override void CompTickRare() //etc public override ... // Check out Verse/ThingComp.cs for more ideas } } // end MyNamespace