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

From RimWorld Wiki
Jump to navigation Jump to search
m (Fixed formatting :p)
(C#)
Line 10: Line 10:
 
=== Basic outline ===
 
=== Basic outline ===
 
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) ====
 
   <ThingDef ...>
 
   <ThingDef ...>
 
     ...
 
     ...
Line 24: Line 25:
 
     </comps>
 
     </comps>
 
   </ThingDef>
 
   </ThingDef>
 
+
==== C# ====
 +
  namespace MyNamespace ''// For example, LWM.ModName - by using your
 +
                          //  handle/name/etc, you almost certainly guarantee uniqueness''
 +
 
 +
  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''
 +
  }
  
 
[[Category:Modding tutorials]][[Category:Modding]][[Category:Defs]]
 
[[Category:Modding tutorials]][[Category:Modding]][[Category:Defs]]

Revision as of 16:45, 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
 
 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
 }