Skip to content

SHORTTUTORIAL: Make a Component

roxxploxx edited this page May 2, 2017 · 5 revisions

Work In Progress

I'm in the process of learning how to do this myself so gimme a bit.

TL;DR

  • A Component X is two classes: ComponentProperties_X and CompX.
  • XML defined const data goes into ComponentProperties_X and functionality and local variables in CompX.
  • The class that uses the Component will find it in this.comps and will interpret what the data means to itself.

Introduction to Components

Components are nice, in that they can be added to a ThingDef for reusable functionality. Examples are CompProperties_Flickable and CompProperties_Refuelable that are used on multiple Things. For Doors, the CompProperties_Flickable means people can't go through them and for other objects, it means they can't be picked up. CompProperties_Refuelable is used by power generators as well as torches as a way to manage fuel to a Thing.

Learn about Things and ThingsDefs

Before you go on, you really need to understand Things and ThingDefs since much of the oddities of Component Duality comes from generating ThingWithComps from ThingDefs.

Component Duality

If you are only working in XML, your components always have the form of CompProperties_X, with X defining the functionality. Then in XML, you modify the tags to change functionality. If you are developing C#, components are more tricky, because your Thing has to determine how to use the components. As well, you have this duality to deal with.

A CompProperties_X always has a CompX and they work together to take information from XML and put it on an instance of a Thing. You don't have to worry about it in XML, but in c# code, you need to determine where to put your data and your methods. The CompProperties_X should always be seen as constant values and the CompX as the instantiation of the Component functionality, along with data and functionality on the data and constant values.

Back to the c# class, this is where the Component values are interpreted. So for a Torch thing, when it runs out of fuel, the light goes out. For a PowerGenerator thing, the power flow stops. These Things, in their Tick() method, will determine what actions to take in response to the Component. The component will determine what to do over time, using it's own Tick handlers.

Your Thing and Your Component

If you are creating a new Component, you will have three classes to manage: CompProperties_X, CompX and a class extending Thing or ThingWithComps that has this Component on it.