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

From RimWorld Wiki
Jump to navigation Jump to search
m (look at thingcomp)
m
Line 8: Line 8:
 
== def (xml) and C# structure ==
 
== def (xml) and C# structure ==
  
=== Basic outline ===
+
=== Setup, Defs, and Classes ===
 
You will have some custom def and it will have something like this:
 
You will have some custom def and it will have something like this:
 
==== Defs (xml) ====
 
==== Defs (xml) ====
Line 64: Line 64:
 
   }
 
   }
 
   } // end MyNamespace
 
   } // end MyNamespace
 +
 +
=== Accessing your Comps ===
 +
Just setting up your custom comps doesn't do you a lot of good if you can't access them!
 +
 +
'''To do'''
 +
 +
 
[[Category:Modding tutorials]][[Category:Modding]][[Category:Defs]]
 
[[Category:Modding tutorials]][[Category:Modding]][[Category:Defs]]

Revision as of 17:06, 29 November 2018

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

Prerequisites

def (xml) and C# structure

Setup, Defs, and Classes

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

Accessing your Comps

Just setting up your custom comps doesn't do you a lot of good if you can't access them!

To do