Plague Gun/Connecting XML and Csharp

From RimWorld Wiki
Jump to navigation Jump to search

Modding Tutorials

This page is obsolete for the current version of RimWorld and is kept as a resource for modders of previous versions. For the current version see: Plague Gun (1.1) This tutorial was originally written by Jecrell. Thread.

Connecting XML and C#

For the next part of the tutorial, we are going to use C# code to create a custom ThingDef blueprint type, and we're going to create a custom class for our Thing to use in-game when it spawns. Before we can get to C# however, we need to "bridge" XML and C# by pointing the XML to use our C# code. First, decide your mod's namespace. This prevents RimWorld from being confused by other mods. This is the name RimWorld knows to use for your C# code. For the purposes of this tutorial, we're going to use Plague.

The following XML will not work in-game until we've written the C# code and created an assembly (.dll file) for our mod.

  1. Change the line for the ThingDef to reference a custom ThingDef class.
    <ThingDef Class="Plague.ThingDef_PlagueBullet" ParentName="BaseBullet">
      <defName>TST_Bullet_PlagueGun</defName>
  2. Add three more lines to the thing def before the closing tag (</ThingDef>).
    <AddHediffChance>0.05</AddHediffChance>
    <HediffToAdd>Plague</HediffToAdd>
    <thingClass>Plague.Projectile_PlagueBullet</thingClass>
    • AddHediffChance and HediffToAdd will give us a percentage of success to add a Hediff of our choice in XML (in this case we're using Plague). We will code how these parameters are used in the C# Coding section.

Completed example

<?xml version="1.0" encoding="utf-8" ?>
<Defs>

  <!-- ================================= Industrial ==================================== -->

  <ThingDef Class="Plague.ThingDef_PlagueBullet" ParentName="BaseBullet">
    <defName>TST_Bullet_PlagueGun</defName>
    <label>plague bullet</label>
    <graphicData>
      <texPath>Things/Projectile/Bullet_Small</texPath>
      <graphicClass>Graphic_Single</graphicClass>
    </graphicData>
    <projectile>
      <flyOverhead>false</flyOverhead>
      <damageDef>Bullet</damageDef>
      <damageAmountBase>12</damageAmountBase>
      <stoppingPower>1</stoppingPower>
      <speed>55</speed>
    </projectile>
    <AddHediffChance>0.05</AddHediffChance>
    <HediffToAdd>Plague</HediffToAdd>
    <thingClass>Plague.Projectile_PlagueBullet</thingClass>
  </ThingDef>

  <!-- The Plague Gun has been left out of this example for the sake of brevity.  -->

</Defs>

See also

  1. Required Items
  2. XML Stage
  3. Connecting XML and C# <- You are here.
  4. C# Assembly Setup
  5. C# Coding
  6. Localisation

More in-depth