Modding Tutorials/GameComponent - RimWorld Wiki (2024)

<Modding Tutorials
<Modding Tutorials/Custom Comp Classes
Although neither explicitly nor formally linked, Map-, World- and GameComponents share a very similar design (and because of that they also share one tutorial). Similar to ThingComp, you can add any custom code to your Map-, World- or GameComponents for you to call manually. It also has methods called at specific occurrences that you can override.

Benefits:
+ Are very well supported by RimWorld.
+ Generally safe to add to existing saves.
+ Work at a very global level.
+ Can save data.
+ Certain functionality gets automatically called by RimWorld.
+ Can be employed to achieve lots of different types of functionality.
+ Are always accessible.

Downsides:
- Removing a mod with these types of components from a save-game results in a (mostly harmless) one-time error.
- Does not expose any of its functionality to XML.

Contents

  • 1 Requirements
  • 2 What you'll learn
  • 3 Setting up
  • 4 Example Code
  • 5 Accessing your Map-, World- or GameComponent
    • 5.1 MapComponent
      • 5.1.1 From a Thing on a map
      • 5.1.2 From the currently visible map
      • 5.1.3 From who knows where
    • 5.2 WorldComponent
    • 5.3 GameComponent
  • 6 Overridable methods of Map-, World- or GameComponent
    • 6.1 TYPEComponentUpdate
    • 6.2 TYPEComponentTick
    • 6.3 TYPEComponentOnGUI
    • 6.4 ExposeData
    • 6.5 FinalizeInit
    • 6.6 StartedNewGame
    • 6.7 LoadedGame
    • 6.8 MapGenerated
    • 6.9 MapRemoved

Requirements[edit]

  • You need to have set up your editor and environment
  • You need to know how to write custom code
  • A decompiler helps, because this tutorial isn't going to tell you the exact implementation details of the Map-, World- or GameComponents.

What you'll learn[edit]

How to use Map-, World- or GameComponents, for the price of one.

Setting up[edit]

RimWorld will automatically create an instance of all classes that inherit from Map-, World- or GameComponent.

To implement a Map-, World-, or GameComponent, we need to inherit from the respective class and implement a parametered constructor inheriting from base. See example. This constructor tells your Map-, World-, or GameComponent what Map, World, or Game it belongs to.

Note that you'll need using RimWorld.Planet; for WorldComponents.

Example Code[edit]

using RimWorld;using Verse;using RimWorld.Planet;namespace MyExampleMod{ public class MyExampleWorldComponent: WorldComponent { public bool myExampleBool = true; public MyExampleWorldComponent(World world): base(world) { } }}

And there we have it. That creates an instance of a WorldComponent. We can then put things like a tracker for "How often did we trade with Faction X" in, or override one or more of the accessible methods. Since these types of components are accessible from pretty much anywhere, they lend themselves to a wide variety of uses.

Accessing your Map-, World- or GameComponent[edit]

These all follow the same design that ThingComp follows; GetComponent<TYPE>().

MapComponent[edit]

First off: Know that null is a perfectly valid value for a Map:

  • Players can abandon all their maps and just caravan around.
  • A pawn on a Caravan is in a null map.
  • A pawn inside a ThingHolder (Carried by another pawn, inside a cryptosleep casket or transport pod, inside a Corpse) is in a null map.

Trying to access your Component from something that is null leads to bad things happening. Null-check.

From a Thing on a map[edit]

thing.Map.GetComponent<MyExampleMapComponent>();

Note: If the thing in question is inside a ThingHolder, use thing.MapHeld instead. That iterates through the ThingHolders until it finds a Map (or null) to return.

From the currently visible map[edit]

Find.CurrentMap.GetComponent<MyExampleMapComponent>();

Note: Ensure that you want your MapComponent to do stuff on the currently visible map. thing.Map is often better.

From who knows where[edit]

If you want to play it safe, you can add something like the below to your MapComponent class to return it like so:

 public static MyExampleMapComponent GetMapComponentFor(Map map) { if (map == null) { Log.Error("Called GetMapComponent on a null map"); return null; } if (map.GetComponent<MyExampleMapComponent>() == null) { map.components.Add(new MyExampleMapComponent(map)); } return map.GetComponent<MyExampleMapComponent>(); }

The safety this design offers is two-fold: It null checks the Map and gives a useful warning, and it handles the rare case where something went wrong with instantiating the MapComponent. This can happen when your MapComponent caused an exception during instantiation, or when a MapComponent that was loaded before yours threw an exception during itsinstantiation.

WorldComponent[edit]

Find.World.GetComponent<MyExampleWorldComponent>();

GameComponent[edit]

Current.Game.GetComponent<MyExampleGameComponent>();

Gotcha: The constructor should take a parameter of type Game (or you'll get errors saying "Constructor not found")

Note the difference in Current vs Find for Game vs Map- and World Components.

Overridable methods of Map-, World- or GameComponent[edit]

NOTE: This is up to date for version 1.0.2150. For the most up-to-date info, grab a decompiler.
NOTE: Not every method listed here is available to all three types of Component.
NOTE: Mentally replace TYPE with the type associated with your Component, be it Map, World or Game.

It does not make sense to override every method listed here. It therefor goes without saying you should only override those methods need.

TYPEComponentUpdate[edit]

Runs per frame, even when the game is paused. Best used for things that need updating regardless of tickrate, but it's unwise to put game logic here.

TYPEComponentTick[edit]

Runs per Tick.

TYPEComponentOnGUI[edit]

Runs per frame, when the TYPE is currently visible. Not available to WorldComponent. Useful for adding GUI stuff.

ExposeData[edit]

Main article: Modding Tutorials/ExposeData

Saves data.

FinalizeInit[edit]

Called when RimWorld is done instantiating the TYPE. This runs after your constructor.

FinalizeInit runs after creating a new instance of TYPE and when loading. If you need instance data from your TYPE (like a WeatherManager, LordManager whatever else) this is the place. This is also the place to initialise your TYPEComponent if your constructor is at risk of throwing: There is no try/catch logic in instantiating new components, but there is for FinalizeInit. Throwing an exception in your constructor will cause issues with generating the Game/World/Map, whereas throwing in FinalizeInit may cause issues.

StartedNewGame[edit]

Unique to GameComponent. Almost, but not quite, entirely unlike FinalizeInit.

LoadedGame[edit]

Unique to GameComponent. Left as an exercise to the reader.

MapGenerated[edit]

Unique to MapComponent. Left as an exercise to the reader.

MapRemoved[edit]

Unique to MapComponent. Left as an exercise to the reader.

Modding Tutorials/GameComponent - RimWorld Wiki (2024)

FAQs

What language are RimWorld mods written in? ›

While it is possible to create many mods by modifying or creating XML, writing custom C# code, either as stand-alone classes or as Harmony patches, allows changing almost anything in the game.

How do I make mods for RimWorld? ›

RimWorld mods are created using XML and C# code. XML is used to define the content of the mod, and C# is used to add functionality to the mod, and also to modify existing items. For now, we will focus on XML and later tutorials will cover C#. Tweaks are mostly done with XML; new mechanics uses C#.

What is RimWorld coded in? ›

We created our own custom object and time handling systems because RimWorld is a tile-based 2D game with thousands of objects, while Unity is better suited for smooth-space 3D games with hundreds of objects. The programming language was C#.

How do I upload RimWorld mods to workshop? ›

You can upload your mod to Steam Workshop by enabling Development Mode from your game Options and then using the Upload option under the Advanced button in the vanilla mod manager. Note that in order to upload to Steam Workshop, you must own the game on Steam Workshop. Owning RimWorld on GOG or Epic will not work.

Will Ludeon Studios make another game? ›

We know Ludeon is working on a new RimWorld DLC. That's it. Anything else is speculation. Stranded is a cool game for a couple of playthroughs.

Was RimWorld made by one person? ›

Tynan Sylvester (born 1986 in Canada), the lead developer for RimWorld, had his first experience with game design by creating custom levels for Unreal Tournament in 2000, as a middle schooler.

Should I play RimWorld without mods first? ›

RimWorld has a lot of depth and is playable without mods for a player's first few saves.

How do you activate Dev mod RimWorld? ›

Development mode can be enabled via the Options in the Menu, accessible via the toolbar or the ESCAPE key. Under Options, there is an option for "Development Mode". Toggle this option to a tick to turn on development mode.

Can you make robots in RimWorld? ›

You can create friendly mechanoids by gestating them. A mechanitor with enough bandwidth is required to both create them, and keep mechs from going feral. The Biotech DLC must be active for you to create mechanoids. These gestated variants require power and have to be recharged, which creates Pollution.

How many endings does RimWorld have? ›

While RimWorld can be played for as long as you want, the game can come to an end in a few different ways. Counting all available DLC, there are 4 distinct ways to see the credits screen: Finding or building a ship to take you to the stars. Joining the court of the High Stellarch of the Empire.

Does RimWorld use CPU or GPU? ›

Your PC should be fine for rimworld. The game uses barely if any GPU power at all so it's mostly about your CPU and RAM. This game has no limits so if you play and expand/grow long enough it will "lag" on even the best PC. It's going to depend entirely on what you mean by late game and on what size map you play on.

Where do I put RimWorld mods? ›

Manual Installation
Operating SystemDefault Folder Location
WindowsC:\Program Files (x86)\Steam\steamapps\common\RimWorld\Mods
MacLibrary/Application Support/Steam/steamapps/common/RimWorld
Linux (standalone)~/.steam/steam/steamapps/common/RimWorld/Mods
Linux (GoG)/home/<user>/GOG/Games/RimWorld/game/Mods/
Oct 24, 2023

Is RimWorld Anomaly out? ›

RimWorld - Anomaly is out! In this horror-themed expansion, face invisible hunters, mad cultists, shambling undead, and crushing darkness.

Will RimWorld console have mods? ›

Will there be mods in RimWorld Console Edition? This version of RimWorld was built for console, so mods created for PC would not be directly compatible given technical requirements, limitations and store policies that are different than the far more open PC environment.

What language are satisfactory mods written in? ›

Satisfactory mods can be written in Unreal Blueprint or C++.

What language are Sims 4 mods written in? ›

The Sims 4 uses XML Tuning to store game data, and Python to interpret that data. Modders can use Python as a basis to create their own XML Tuning (for example, module tuning or test sets), to run a command from the game's console, to store settings, to avoid overriding files, and more.

What language are Factorio mods written in? ›

Lua scripting

You need to use the Lua programming language (version 5.2. 1) to create any mods in Factorio. The game's mod system injects your code into the startup and to the data construction stage of the game. You can use any text editor to write the code for your mod.

How to translate RimWorld mods? ›

Open RimTrans. Click the 'Add Mod' button, select the Core or a mod and check languages which you need. Click the 'Extract' button, so the language files will been genenrated and output to corresponding folder. Open the languages folder and translate them!

References

Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 6490

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.