Difference between revisions of "Modding Tutorials/Custom Comp Classes"

From RimWorld Wiki
Jump to navigation Jump to search
(more C#)
m (look at thingcomp)
Line 60: Line 60:
 
       // do stuff
 
       // do stuff
 
     }
 
     }
     public override void CompTickRare() //...
+
     public override void CompTickRare() //etc
     public override ... // etc
+
     public override ... // Check out Verse/ThingComp.cs for more ideas
 
   }
 
   }
 
   } // end MyNamespace
 
   } // end MyNamespace
 
[[Category:Modding tutorials]][[Category:Modding]][[Category:Defs]]
 
[[Category:Modding tutorials]][[Category:Modding]][[Category:Defs]]

Revision as of 17:01, 29 November 2018

Creating a custom comp class is a convenient way to add new functionality to RimWorld.

Prerequisites

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