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

From RimWorld Wiki
Jump to navigation Jump to search
(more C#)
 
(19 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Creating a custom comp class is a convenient way to add new functionality to RimWorld.
+
{{BackToTutorials}}
 +
Creating a custom comp class is a convenient way to add new functionality to RimWorld. Comps aren't a formal concept in the code: they're a design pattern used in different places. They allow for a more modular approach to adding functionality to different objects.
  
== Prerequisites ==
+
=The types of Components=
* You need to have [[Modding Tutorials/Setting up a solution|set up your editor and environment]]
+
These are the places where the Comps design pattern is used, each with differing behaviour suited for their respective area. From most specific to most generic:
* You need to know [[Modding Tutorials/Writing custom code|how to write custom code]]
+
==HediffComp==
* You should probably understand Defs at least somewhat.
+
A relatively simple Comp for adding more complex behaviour to Hediffs.
  
== def (xml) and C# structure ==
+
==ThingComp==
 +
{{Main|Modding Tutorials/ThingComp}}
 +
A very powerful Component that is tied to a specific Thing. These are often used to store data, give special functionality to the Thing they're tied to and are one of the building blocks of RimWorld modding and RimWorld in general. While not as powerful as a [[Modding Tutorials/Def classes|fully custom class]], they provide plenty of functionality for a lot of general use cases without compatibility issues.
  
=== Basic outline ===
+
==WorldObjectComp==
You will have some custom def and it will have something like this:
+
Like a ThingComp, but for WorldObjects.
==== Defs (xml) ====
+
 
  <ThingDef ...>
+
==MapComponent==
    ...
+
{{Main|Modding Tutorials/GameComponent}}
    ...
+
Much like a ThingComp, except these exist at the Map level. They're most useful for keeping tracks of multiple things at once, storing data, and can serve as a coordinator or general managing entity.
    <comps>
+
 
      <<nowiki>li</nowiki> Class="MyNamespace.MyCompProperties">
+
==WorldComponent==
        <myCustomCompProperty>some value</myCustomCompProperty>
+
{{Main|Modding Tutorials/GameComponent}}
        <mySecondCompProp>4</mySecondCompProp>
+
Similar to a MapComponent, but lives on the World level.
      </<nowiki>li</nowiki>>
+
 
      <<nowiki>li</nowiki>>
+
==GameComponent==
        ''<<nowiki>!--</nowiki> this is kind of like <tag>MN.MyCustomTag</tag>:-->''
+
{{Main|Modding Tutorials/GameComponent}}
        <compClass>MyNamespace.MyCustomThingComp</compClass>
+
Similar to a WorldComponent, but lives at the Game level.
      </<nowiki>li</nowiki>>
+
 
    </comps>
+
The distinction between a GameComponent and a WorldComponent might not be too obvious, but a GameComponent gets instantiated when a new Game is started:
  </ThingDef>
+
* Upon start of the tutorial
==== C# ====
+
* When the player starts the Scenario Configuration (rolling for colonists)
  namespace MyNamespace ''// For example, LWM.ModName - by using your
+
* When a save is loaded from the main menu
                          //  handle/name/etc, you almost certainly guarantee uniqueness''
+
 
  {
+
==StorytellerComp==
  '''//////////// <<nowiki>li</nowiki Class="MyNamespace.MyCompProperties"> ////////////'''
+
These are a specific type of Component that determines the behaviour of the storyteller.
  public class MyCompProperties : CompProperties ''// Name this as you wish, of course''
+
 
  {
+
=Which one to use=
    public Properties() {
+
Use whatever is most appropriate, really. Does it deal with a Pawn's health? HediffComp. Is it functionality at Thing level? ThingComp. Does it have to do with two pawns, or multiple items on a map? Probably a MapComponent, or maybe a WorldComponent.
      this.compClass = typeof(MyNamespace.MyLinkedCompThing); ''// rename as appropriate''
+
 
    }
+
[[Category:Modding tutorials]][[Category:Modding]]
    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() //...
 
    public override ... // etc
 
  }
 
  } // end MyNamespace
 
[[Category:Modding tutorials]][[Category:Modding]][[Category:Defs]]
 

Latest revision as of 00:43, 12 September 2021

Modding Tutorials
Creating a custom comp class is a convenient way to add new functionality to RimWorld. Comps aren't a formal concept in the code: they're a design pattern used in different places. They allow for a more modular approach to adding functionality to different objects.

The types of Components[edit]

These are the places where the Comps design pattern is used, each with differing behaviour suited for their respective area. From most specific to most generic:

HediffComp[edit]

A relatively simple Comp for adding more complex behaviour to Hediffs.

ThingComp[edit]

A very powerful Component that is tied to a specific Thing. These are often used to store data, give special functionality to the Thing they're tied to and are one of the building blocks of RimWorld modding and RimWorld in general. While not as powerful as a fully custom class, they provide plenty of functionality for a lot of general use cases without compatibility issues.

WorldObjectComp[edit]

Like a ThingComp, but for WorldObjects.

MapComponent[edit]

Much like a ThingComp, except these exist at the Map level. They're most useful for keeping tracks of multiple things at once, storing data, and can serve as a coordinator or general managing entity.

WorldComponent[edit]

Similar to a MapComponent, but lives on the World level.

GameComponent[edit]

Similar to a WorldComponent, but lives at the Game level.

The distinction between a GameComponent and a WorldComponent might not be too obvious, but a GameComponent gets instantiated when a new Game is started:

  • Upon start of the tutorial
  • When the player starts the Scenario Configuration (rolling for colonists)
  • When a save is loaded from the main menu

StorytellerComp[edit]

These are a specific type of Component that determines the behaviour of the storyteller.

Which one to use[edit]

Use whatever is most appropriate, really. Does it deal with a Pawn's health? HediffComp. Is it functionality at Thing level? ThingComp. Does it have to do with two pawns, or multiple items on a map? Probably a MapComponent, or maybe a WorldComponent.