Modding Tutorials/Weapons Guns

From RimWorld Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Modding Tutorials

This page was originally created by Alistaire.

In this tutorial we will take a look at a specific item and break down its components, link this knowledge with other items and ultimately learn how to create your own items.

Requirements

What you'll learn

You'll learn what most tags inside Weapons_Guns.xml do.

BaseGun and BaseBullet parent

To further understand the BaseGun and BaseBullet parents, we will now do the following:

  • Firstly we'll break down the simple parts of their code;
  • Next up we're breaking down the more complicated part;
  • Lastly I'll post the source code for both of them - they shouldn't be or have to be edited.

Breakdown

Now that we know about inheritance, we can take another look at the BaseGun Parent. All the code inside this parent is inherited by BaseHumanGun, which is in turn inherited by almost all weapons in Weapons_Guns.xml.
Let's break down the rest of the code in BaseGun.

The following tags are relatively straightforward:

<ThingDef Name="BaseGun" Abstract="True">
	<category>Item</category>
	<altitudeLayer>Item</altitudeLayer>
	<useHitPoints>True</useHitPoints>
	<selectable>True</selectable>
	<alwaysHaulable>True</alwaysHaulable>
	<techLevel>Midworld</techLevel>
	<thingCategories>
		<li>WeaponsRanged</li>
	</thingCategories>
	<smeltProducts>
		<Steel>20</Steel>
	</smeltProducts>
	<statBases>
		<MaxHitPoints>100</MaxHitPoints>
		<Flammability>1.0</Flammability>
		<DeteriorationRate>1</DeteriorationRate>
		<SellPriceFactor>0.5</SellPriceFactor>
	</statBases>
	<graphicData>
		<onGroundRandomRotateAngle>35</onGroundRandomRotateAngle>
	</graphicData>
</ThingDef>

<ThingDef Name="BaseHumanGun" ParentName="BaseGun" Abstract="True">
	<weaponTags>
		<li>Gun</li>
	</weaponTags>
</ThingDef>

<ThingDef Name="BaseBullet" Abstract="True">
	<category>Projectile</category>
	<altitudeLayer>Projectile</altitudeLayer>
	<useHitPoints>False</useHitPoints>
	<label>bullet</label>
	<neverMultiSelect>True</neverMultiSelect>
	<graphicData>
		<shaderType>Transparent</shaderType>
	</graphicData>
</ThingDef>
<thingDef>
Tag Values Explanation
<category> Item
Projectile
The game uses the contents of this tag to load the rest of this <thingDef> in a specific way.
All accepted values can be found in Category:Defs.
<altitudeLayer> Item
Projectile
The layer this thing is rendered on. Usually the same value as category.
All accepted values can be found in Category:Defs.
<useHitPoints> boolean Whether this thing has hitpoints and can be damaged. Bullets aren't damaged, weapons can deteriorate and use hitpoints for that.
<selectable> boolean Whether this thing can be selected by clicking on it. Bullets can't be selected, so this value defaults to False.
<alwaysHaulable> boolean Whether this thing can be hauled by anyone. Bullets can't be hauled, so this value defaults to False.
<neverMultiSelect> boolean Whether clicking twice on this thing won't select all similar things on screen. Defaults to False, making multiselecting a default behaviour.
<techLevel> Midworld
...
What tech level the weapon represents. Different tech levels make it possible for different enemies to spawn with this weapon.
<thingCategories> thingCatories Any arbitrary category this weapon represents.
Some pawns can only spawn with weapons in the WeaponsRanged and WeaponsMelee categories.
<smeltProducts> <resource defName> This tag has another tag as contents, in this case <Steel>.
This sub-tag's name is that of a resource's defName (see Category:Defs).
The contents of the sub-tag are integers above zero, and represent the amount of some resource gained when this weapon is thrown in an Electric smelter.
<statBases>
<MaxHitPoints> integer The hitpoints maximum this weapon has. Weapons deteriorate over time, slowly dropping from this value to 0 at which point it's destroyed.
<Flammability> decimal A number denoting how hard it is for fire to jump over to this weapon and how much effect this fire will have on its hitpoints.
A thing with a Flammability of 0.0 won't start burning at all, while one with a Flammability of 1.0 will start burning very quickly.
<DeteriorationRate> decimal A number denoting how much this weapon deteriorates with every unit of deterioration time.
Things with 0.0 DeteriorationRate won't take any damage over time, ones with a value of 2.0 will deteriorate twice as fast as usual.
A DeteriorationRate of 0.0 won't prevent the weapon from taking fire- or explosion damage.
<SellPriceFactor> decimal A factor denoting how much traders will pay you less for this weapon than they would sell it for (when negotiating at maximal social skill).
The default value of this factor on weaponry is 0.5.
<graphicData>
<onGroundRandomRotateAngle> degrees How many degrees this thing will randomly be rotated at when placed on the ground. This is usually 35 degrees for guns.
<shaderType> ... How the thing is shaded in-game. The meaning and effect of each shader is slightly different, so;
All accepted values can be found in Category:Defs.


And the last part is somewhat more complicated:

<ThingDef Name="BaseGun" Abstract="True">
	<thingClass>ThingWithComps</thingClass>
	<equipmentType>Primary</equipmentType>
	<pathCost>10</pathCost>
	<drawGUIOverlay>true</drawGUIOverlay>
	<tickerType>Never</tickerType>
	<inspectorTabs>
		<li>ITab_Art</li>
	</inspectorTabs>
	<comps>
		<li>
			<compClass>CompForbiddable</compClass>
		</li>
		<li>
			<compClass>CompEquippable</compClass>
		</li>
		<li>
			<compClass>CompQuality</compClass>
		</li>
		<li>
			<compClass>CompArt</compClass>
			<nameMaker>NamerGun</nameMaker>
			<descriptionMaker>ArtWeaponGun</descriptionMaker>
			<minQualityForArtistic>Excellent</minQualityForArtistic>
		</li>
	</comps>
</ThingDef>

<ThingDef Name="BaseBullet" Abstract="True">
	<thingClass>Bullet</thingClass>
	<tickerType>Normal</tickerType>
</ThingDef>
<thingDef>
Tag Values Explanation
<thingClass> ThingWithComps
Bullet
...
What C# class to call when doing anything with this thing. Unless you know what you're doing, it's suggested to use the same thingClass similar things use.
<equipmentType> Primary
Secondary
Any pawn can have a primary and secondary weapon. Guns and melee weapons are usually primary, while grenades are usually secondary.
<pathCost> int Subtracted from a floor's movement speed when this thing lies on it.
High values make it harder for pawns to walk past this thing, lower values make it easier.
<drawGUIOverlay> boolean Whether GUI will show up when you select this thing.
<tickerType> Never
Normal
...
Whether this thing has a C# Tick() function; whether something happens to the thing every in-game Tick.
<inspectorTabs> ITab list A list of ITabs that will show up in the inspector when you select this item. These ITabs are for example the "Bills" buttons (ITab_Bills) and the art description (ITab_Art).
<comps> <li> </comps>
<compClass> ... A C# class describing what should happen when the thing is interacted with in a specific way. This class might need additional tags to work.
<nameMaker> ... The defName of a RulePackDef which describes how this thing will be named under specific conditions, which in turn are described in a C# class provided in <compClass>.
<descriptionMaker> ... Similar to the nameMaker, but for generating a description.
<minQualityForArtistic> ... At what Quality this thing earns an artistic title and description. This behaviour is unique to the CompArt <compClass>, and it won't work with other classes.

Source

It's not recommended to edit these parents. A recent copy of them can be taken from ../Mods/Core/Defs/ThingDefs/Weapons_Guns.xml, and the RimWorld834Win (Version 0.11.834) version is shown below:

<ThingDef Name="BaseGun" Abstract="True">
	<category>Item</category>
	<thingClass>ThingWithComps</thingClass>
	<equipmentType>Primary</equipmentType>
	<pathCost>10</pathCost>
	<useHitPoints>True</useHitPoints>
	<selectable>True</selectable>
	<graphicData>
		<onGroundRandomRotateAngle>35</onGroundRandomRotateAngle>
	</graphicData>
	<drawGUIOverlay>true</drawGUIOverlay>
	<statBases>
		<MaxHitPoints>100</MaxHitPoints>
		<Flammability>1.0</Flammability>
		<DeteriorationRate>1</DeteriorationRate>
		<SellPriceFactor>0.5</SellPriceFactor>
	</statBases>
	<altitudeLayer>Item</altitudeLayer>
	<alwaysHaulable>True</alwaysHaulable>
	<tickerType>Never</tickerType>
	<techLevel>Midworld</techLevel>
	<thingCategories>
		<li>WeaponsRanged</li>
	</thingCategories>
	<inspectorTabs>
		<li>ITab_Art</li>
	</inspectorTabs>
	<comps>
		<li>
			<compClass>CompForbiddable</compClass>
		</li>
		<li>
			<compClass>CompEquippable</compClass>
		</li>
		<li>
			<compClass>CompQuality</compClass>
		</li>
		<li>
			<compClass>CompArt</compClass>
			<nameMaker>NamerGun</nameMaker>
			<descriptionMaker>ArtWeaponGun</descriptionMaker>
			<minQualityForArtistic>Excellent</minQualityForArtistic>
		</li>
	</comps>
	<smeltProducts>
		<Steel>20</Steel>
	</smeltProducts>
</ThingDef>

<ThingDef Name="BaseHumanGun" ParentName="BaseGun" Abstract="True">
	<weaponTags>
		<li>Gun</li>
	</weaponTags>
</ThingDef>

<ThingDef Name="BaseBullet" Abstract="True">
	<category>Projectile</category>
	<tickerType>Normal</tickerType>
	<altitudeLayer>Projectile</altitudeLayer>
	<thingClass>Bullet</thingClass>
	<label>bullet</label>
	<useHitPoints>False</useHitPoints>
	<neverMultiSelect>True</neverMultiSelect>
	<graphicData>
		<shaderType>Transparent</shaderType>
	</graphicData>
</ThingDef>


Gun definitions

Guns inherit from BaseHumanGun or sometimes from BaseGun. Each gun requires a bunch of tags and additionally accepts some other tags.

Breakdown

Each gun requires the following tags:

<ThingDef ParentName="BaseHumanGun">
	<defName>Gun_Pistol</defName>
	<label>pistol</label>
	<description>Ancient pattern automatic pistol. Weak and short range, but quick.</description>
	<graphicData>
		<texPath>Things/Item/Equipment/WeaponRanged/Pistol</texPath>
		<graphicClass>Graphic_Single</graphicClass>
	</graphicData>
	<soundInteract>InteractPistol</soundInteract>
	<statBases>
		<MarketValue>200</MarketValue>
		<AccuracyTouch>0.91</AccuracyTouch>
		<AccuracyShort>0.71</AccuracyShort>
		<AccuracyMedium>0.50</AccuracyMedium>
		<AccuracyLong>0.32</AccuracyLong>
		<RangedWeapon_Cooldown>0.66</RangedWeapon_Cooldown>
	</statBases>
	<verbs>
		<li>
			<verbClass>Verb_Shoot</verbClass>
			<hasStandardCommand>true</hasStandardCommand>
			<projectileDef>Bullet_Pistol</projectileDef>
			<warmupTicks>54</warmupTicks>
			<range>24</range>
			<soundCast>ShotPistol</soundCast>
			<soundCastTail>GunTail_Light</soundCastTail>
			<muzzleFlashScale>9</muzzleFlashScale>
		</li>
	</verbs>
</ThingDef>
<thingDef>
Tag Values Explanation
<defName> string The defName, in-script name, of this item that can be referred to by C# or other XML files.
For guns this usually starts with Gun_, and for bullets it usually starts with Bullet_.
<label> string The in-game (as opposed to in-script defName) name for this item in lowercase. The game automatically makes the first character uppercase.
<description> string The description for this item, shown when the item's information tab is opened. You can use escape characters in this string, like \n for a newline.
<soundInteract> defName
(from soundDef)
What sound is played whenever someone takes or drops the item.
<graphicData>
<texPath> path The path relative to ../Textures/ in which this thing's texture is stored.
<graphicClass> C# class The class that is called when generating an image for this thing. Guns in the base game always use Graphic_Single, but there's different values like Graphic_Random etc.
<statBases>
<MarketValue> integer The "market value" of this thing.
Please note that sellPriceFactor halves this value when selling the thing, and that the Social skill influences the actual value.
This value is modified by the weapon's quality.
<Accuracy...> float The 0.0-1.0 chance this gun hits its target at a certain range. The Weapons page has an explanation for the effects of different accuracy tags.
This value is modified by the weapon's quality.
<RangedWeapon_Cooldown> seconds The amount of seconds the pawn will stand around doing nothing after firing a burst of this weapon.
This value is modified by the weapon's quality.
<verbs> <li> </verbs>
<verbClass> C# class The class that is called whenever this weapon is shot. Vanilla Rimworld only uses Verb_Shoot, while mods might call their own custom verbClass.
<hasStandardCommand> boolean
<projectileDef> defName The defName of the bullet this gun fires.
<warmupTicks> ticks The amount of game ticks it takes to aim this gun.
This aiming time is visualized with a shrinking grey circle in-game - large values increase the circle's starting circumference.
<range> tiles How far the weapon can aim.
This value is modified by the weapon's quality.
<soundCast> defName
(from soundDef)
What sound is played whenever the weapon spawns a bullet (can be read as fires).
<soundCastTail> defName
(from soundDef)
What sound is played after the weapon is fired. This value is not necessarily required, but vanilla Rimworld uses it a lot.
<muzzleFlashScale> int The size of the muzzle flash effect which is displayed when the weapon fires.


Bullets

Bullets hold information regarding damage, bullet speed and whether the bullet will explode, burst into flames, etc.
Each bullet requires the following tags:

<ThingDef ParentName="BaseBullet">
	<defName>Bullet_Pistol</defName>
	<label>pistol bullet</label>
	<graphicData>
		<texPath>Things/Projectile/Bullet_Small</texPath>
		<graphicClass>Graphic_Single</graphicClass>
	</graphicData>
	<projectile>
		<flyOverhead>false</flyOverhead>
		<damageDef>Bullet</damageDef>
		<DamageAmountBase>9</DamageAmountBase>
		<Speed>55</Speed>
	</projectile>
</ThingDef>
<thingDef>
Tag Values Explanation
<defName> string The defName, in-script name, of this item that can be referred to by C# or other XML files.
For guns this usually starts with Gun_, and for bullets it usually starts with Bullet_.
<label> string The in-game (as opposed to in-script defName) name for this item in lowercase. The game automatically makes the first character uppercase.
<graphicData>
<texPath> path The path relative to ../Textures/ in which this thing's texture is stored.
<graphicClass> C# class The class that is called when generating an image for this thing. Guns in the base game always use Graphic_Single, but there's different values like Graphic_Random etc.
<projectile>
<flyOverhead> boolean Whether a missing bullet can fly over walls. Seriously.
<damageDef> defName
from damageDef
The defName of the damage type this bullet causes.
<damageAmountBase> int The amount of base damage the bullet can cause. Hitting a specific object might do a different amount of damage based on the value of damageDef.
<speed> int A value denoting some unit of speed at which the bullet travels. It's best to reference existing weapons when choosing a speed for your bullet.


Source

<ThingDef ParentName="BaseBullet">
	<defName>Bullet_Pistol</defName>
	<label>pistol bullet</label>
	<graphicData>
		<texPath>Things/Projectile/Bullet_Small</texPath>
		<graphicClass>Graphic_Single</graphicClass>
	</graphicData>
	<projectile>
		<flyOverhead>false</flyOverhead>
		<damageDef>Bullet</damageDef>
		<DamageAmountBase>9</DamageAmountBase>
		<Speed>55</Speed>
	</projectile>
</ThingDef>

<ThingDef ParentName="BaseHumanGun">
	<defName>Gun_Pistol</defName>
	<label>pistol</label>
	<description>Ancient pattern automatic pistol. Weak and short range, but quick.</description>
	<graphicData>
		<texPath>Things/Item/Equipment/WeaponRanged/Pistol</texPath>
		<graphicClass>Graphic_Single</graphicClass>
	</graphicData>
	<soundInteract>InteractPistol</soundInteract>
	<statBases>
		<MarketValue>200</MarketValue>
		<AccuracyTouch>0.91</AccuracyTouch>
		<AccuracyShort>0.71</AccuracyShort>
		<AccuracyMedium>0.50</AccuracyMedium>
		<AccuracyLong>0.32</AccuracyLong>
		<RangedWeapon_Cooldown>0.66</RangedWeapon_Cooldown>
	</statBases>
	<verbs>
		<li>
			<verbClass>Verb_Shoot</verbClass>
			<hasStandardCommand>true</hasStandardCommand>
			<projectileDef>Bullet_Pistol</projectileDef>
			<warmupTicks>54</warmupTicks>
			<range>24</range>
			<soundCast>ShotPistol</soundCast>
			<soundCastTail>GunTail_Light</soundCastTail>
			<muzzleFlashScale>9</muzzleFlashScale>
		</li>
	</verbs>
</ThingDef>

Special cases

There's a few cases where guns don't entirely follow the previously described structure.

Burstfire

Some guns, including the Assault rifle use the following code to add burstfire:

<ThingDef ParentName="BaseHumanGun">
	<verbs>
		<li>
			<burstShotCount>3</burstShotCount>
			<ticksBetweenBurstShots>7</ticksBetweenBurstShots>
		</li>
	</verbs>
</ThingDef>


The weapon will fire burstShotCount times with a ticksBetweenBurstShots tick delay between each shot.

Sniper rifles

The Sniper rifle is only used by specific Raiders, and won't be used by the rest. This is because of the following code:

<ThingDef ParentName="BaseHumanGun">
	<weaponTags>
		<li>SniperRifle</li>
	</weaponTags>
</ThingDef>


Adding a weaponTag to both your weapon and a custom pawn makes it so the pawn will spawn in with that weapon.

Bomb projectiles

<ThingDef ParentName="BaseBullet">
	<thingClass>Projectile_Explosive</thingClass>
	<projectile>
		<damageDef>Bomb</damageDef>
		<explosionRadius>7.8</explosionRadius>
	</projectile>
</ThingDef>


Things with a Bomb damageDef need an explosionRadius and will explode upon impact.

Flame bullets

The Incendiary Launcher's bullet uses <damageDef>Flame</damageDef>:

<ThingDef ParentName="BaseBullet">
	<projectile>
		<damageDef>Flame</damageDef>
		<explosionRadius>1.1</explosionRadius>
		<postExplosionSpawnThingDef>Puddle_Fuel</postExplosionSpawnThingDef>
		<explosionSpawnChance>0.7</explosionSpawnChance>
	</projectile>
</ThingDef>


This bullet will explode into flames in a radius of 1.1 tiles, and it will spawn a Puddle_Fuel 70% of all impacts.

Bigger muzzleflash

Some weapons have a bigger muzzleflash:

<ThingDef ParentName="BaseHumanGun">
	<verbs>
		<li>
			<muzzleFlashScale>14</muzzleFlashScale>
		</li>
	</verbs>
</ThingDef>


Most weapons have a scale of 9, while some of the bigger weapons have a scale of 14.

Spacer technology

Guns like the Charge rifle only spawn on pawns with Spacer technology:

<ThingDef ParentName="BaseHumanGun">
	<techLevel>Spacer</techLevel>
</ThingDef>


Turret gun

Turrets use a gun from Weapons_Guns.xml which uses the following code:

<ThingDef ParentName="BaseHumanGun">
	<menuHidden>true</menuHidden>
	<canBeSpawningInventory>false</canBeSpawningInventory>
	<tradeability>Never</tradeability>
	<weaponTags>
		<li>TurretGun</li>
	</weaponTags>
</ThingDef>


This gun can't be seen in the colony inventory screen, can't be spawned in with a newly spawned pawn, isn't tradable (so it won't show up on traders) and its weaponTag is TurretGun to prevent it from being spawned in by accident.

Heavy weapons

Heavy weapons have additional weaponTags and equippedStatOffsets:

<ThingDef ParentName="BaseHumanGun">
	<weaponTags>
		<li>MechanoidGunHeavy</li>
		<li>GunHeavy</li>
	</weaponTags>
	<equippedStatOffsets>
		<MoveSpeed>-0.25</MoveSpeed>
	</equippedStatOffsets>
</ThingDef>


Centipedes will spawn with MechanoidGunHeavy weaponry. Heavy raiders will spawn with GunHeavy. The equippedStatOffsets of MoveSpeed of -0.25 slows its wearer down by 0.25 c/s

Doomsday rocket

<ThingDef ParentName="BaseBullet">
	<shaderType>TransparentPostLight</shaderType>
	<thingClass>Projectile_DoomsdayRocket</thingClass>
</ThingDef>


The Doomsday rocket uses a different shaderType which isn't effected by light levels to simulate it giving off light itself. It also has a different thingClass.

<ThingDef ParentName="BaseGun">
	<tradeTags>
		<li>Exotic</li>
	</tradeTags>
	<verbs>
		<li>
			<verbClass>Verb_ShootOneUse</verbClass>
			<onlyManualCast>true</onlyManualCast>
			<targetParams>
				<canTargetLocations>true</canTargetLocations>
			</targetParams>
		</li>
	</verbs>
</ThingDef>


The gun is only traded by Exotic traders due to its tradeTags, is a single use (Verb_ShootOneUse) weapon which can only be shot by the player itself. It can target floor tiles, not only wall tiles.

Miss radius

<ThingDef ParentName="BaseGun">
	<verbs>
		<li>
			<forcedMissRadius>2.0</forcedMissRadius>
		</li>
	</verbs>
</ThingDef>


Even after applying the accuracy checks, this gun is still going to miss its target by 2.0 tiles.