Modding Tutorials/Weapons

From RimWorld Wiki
Jump to navigation Jump to search

Modding Tutorials

This page was originally created by Cala13er.

In this tutorial, we'll make a new weapon.

Prerequisites

You should have read the Getting Started tutorial, which gets you up to speed with how mods are structured in RimWorld. You should also have a good understanding of where files are located (such as About.xml, def XML files, where you should put textures, and so on).

Making the folders

If you haven't already created a ThingDef folder as taught in the Getting Started tutorial, you will need to create a new folder in which will contain your new item xml. Inside your Defs folder create a new subfolder called ThingDefs. You're now done as far as creating folders go.

Creating the new weapon

In this example, we'll be making a gun called the Scar-H.

First of all, we need to get the outline code sorted. Below is what your xml should look like.

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



</ThingDefs>

Next, we will need to make a new abstract ThingDef which will be the Base properties for the a gun.

	<ThingDef Name="BaseGun" Abstract="True">
		<category>Item</category>
		<eType>Equipment</eType>
		<thingClass>Equipment</thingClass>
		<label>Gun</label>
		<equipmentType>Primary</equipmentType>
		<isGun>True</isGun>
		<pathCost>10</pathCost>
		<useStandardHealth>True</useStandardHealth>
		<selectable>True</selectable>
		<maxHealth>100</maxHealth>
		<altitudeLayer>Item</altitudeLayer>
		<alwaysHaulable>True</alwaysHaulable>
		<tickerType>Never</tickerType>
		<techLevel>Midworld</techLevel>
		<storeCategories>
			<li>Weapons</li>
		</storeCategories>
		<weaponTags>
			<li>Gun</li>
		</weaponTags>
		<comps>
			<li>
				<compClass>CompForbiddable</compClass>
			</li>
		</comps>
		<verb>
			<id>Nonnative</id>
			<verbClass>Verb_Shoot</verbClass>
			<cooldownTicks>30</cooldownTicks>
			<label>VerbGun</label>
			<description>Fire a bullet.</description>
			<hasStandardCommand>true</hasStandardCommand>
			<targetParams>
				<canTargetPawns>true</canTargetPawns>
				<canTargetBuildings>true</canTargetBuildings>
				<worldObjectTargetsMustBeAutoAttackable>true</worldObjectTargetsMustBeAutoAttackable>
			</targetParams>
			<canMiss>true</canMiss>
		</verb>
	</ThingDef>

Now that we have an abstract ThingDef we will need to make another one for the bullet the gun will use.

	<ThingDef Name="BaseBullet" Abstract="True">
		<category>Projectile</category>
		<tickerType>Normal</tickerType>
		<altitudeLayer>Projectile</altitudeLayer>
		<thingClass>Bullet</thingClass>
		<label>Bullet</label>
		<useStandardHealth>False</useStandardHealth>
		<neverMultiSelect>True</neverMultiSelect>
		<baseMaterialType>Transparent</baseMaterialType>
	</ThingDef>

Note that both of these do NOT need to be edited.

Now, we're going to be making the bullet our gun will use. Here is my bullet def for the Scar-H.

 
	<ThingDef ParentName="BaseBullet">
		<defName>Bullet_ScarHC</defName>
		<label>7.62×51mm NATO</label>
		<texturePath>Things/Projectile/Bullet_Small</texturePath>
		<projectile>
			<damageType>Bullet</damageType>
			<DamageAmountBase>8</DamageAmountBase>
			<Speed>85</Speed>
		</projectile>
	</ThingDef>

Now all we have left to do is make the Gun def!

	<ThingDef ParentName="BaseGun">
		<defName>Gun_ScarHC</defName>
		<label>ScarH</label>
		<description></description>
		<texturePath>Things/Item/IRGuns/Gun_ScarH</texturePath>
		<interactSound>InteractBoltActionRifle</interactSound>
		<purchasable>True</purchasable>
		<basePrice>500</basePrice>
		<verb>
			<projectileDef>Bullet_ScarHC</projectileDef>
			<warmupTicks>180</warmupTicks>
			<range>39</range>
			<accuracy>8</accuracy>
			<burstShotCount>4</burstShotCount>
			<ticksBetweenBurstShots>6</ticksBetweenBurstShots>
			<fireSound>ShotM16Rifle</fireSound>
		</verb>
	</ThingDef>

And there you go! Your very own gun :)

Hopefully, you’ll be able to understand what each property is once you read the page on ThingDef.

Note that the way the game processes shooting a weapon with the above variables is as follows: Start trying to shoot -> wait for warmup ticks -> fire shot -> wait ticksBetweenBurstShots -> fire shot -> wait ticksBetweenBurstShots -> wait for cooldownTicks -> back to warmup ticks.

Obviously the number of shots in the burst can change.

Testing

Let’s test our new mod! Fire up RimWorld, making sure to turn on Development mode in the options menu. Open the mods menu and make sure your mod is ticked (For active), and then press the tilde key (~) to check for any errors thrown on runtime. That’s right, no errors! Now start a new game and activate God mode, then attempt to spawn your gun into the game, make your colonist pick the weapon up and try firing with it at a wall!

Conclusion

You now know how to:

  • make your own gun
  • know most of the thingDef's global attributes and their functions with the optional choices

Tutorial brought to you by Cala13er, If you have any questions about this tutorial. Message either Cala13er or Tynan on the forums.

Next we'll learn how to make a flooring!