Difference between revisions of "User:Alistaire/Tutorials/Weapons Guns"

From RimWorld Wiki
Jump to navigation Jump to search
(Added "What you'll learn")
(Link to "Mod folder structure")
Line 1: Line 1:
 
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.<br/>
 
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.<br/>
  
It requires you to have [[User:Alistaire/Tutorials/Folder structure|set up a folder structure]] for your mod.<br/><br/>
+
It requires you to have [[Modding Tutorials/Mod folder structure|set up a folder structure]] for your mod.<br/><br/>
  
 
=What you'll learn=
 
=What you'll learn=

Revision as of 18:28, 11 July 2015

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.

It requires you to have set up a folder structure for your mod.

What you'll learn

You'll learn the default structure of thingDef files:

<?xml version="1.0" encoding="utf-8"?>
<ThingDefs>
	<thingDef Name="Parent" Abstract="True">
	</thingDef>

	<thingDef ParentName="Parent">
	</thingDef>

	(... more <thingDef>s ...)
</ThingDefs>


.. Along with what most tags inside Weapons_Guns.xml do, and how inheritance using Name, ParentName and Abstract works.

Default thingDef structure

Introduction

Create a .xml file in your mod's folder. A recommended location and name for this file, starting from your Rimworld download location and "MOD NAME" replaced with your mod's name, is:

../Mods/MOD NAME/Defs/ThingDefs/Weapons_Guns.xml


The first thing in this file should be the following line:

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


After that, the structure of the file consists of <ThingDefs> and <thingDef> tags:

<ThingDefs>
	<thingDef>
	</thingDef>

	(... more <thingDef>s ...)

	<thingDef>
	</thingDef>
</ThingDefs>


Each <thingDef> contains something's def (or definition), which can be used to specify each and every modifiable property for a certain thing.

Source

It is important that your file follows this structure to the point where the Weapons_Guns.xml file won't work with multiple <ThingDefs> or <thingDef>s outside of <ThingDefs>:

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

	(... more <thingDef>s ...)

	<thingDef>
	</thingDef>
</ThingDefs>


BaseGun and BaseBullet parent

Introduction

The first thing in Weapons_Guns.xml is a <thingDef> with a Name and Abstract type:

<ThingDef Name="BaseGun" Abstract="True">


The Name type means the contents of this <thingDef> can be inherited by (read: copied by) another <thingDef>. This way you can write everything you're going to repeat a lot throughout the file in a single location, such as the following:

<category>Item</category>


Which notifies that this <thingDef> is of the Item category, as opposed to those of the Building category. Because there's a lot of tags repeated throughout every gun, this greatly compacts the XML file.
The full BaseGun parent only has to be defined once in a file, and can then be inherited (copied) with:

<ThingDef ParentName="BaseGun">


<thingDef ParentName="X"> inherits all contents from <thingDef Name="X">. It is common practice to copy the vanilla BaseGun parent and paste it on top of a mod's Weapons_Guns.xml file.
Another parent is BaseBullet which holds every standard bullet's commonly repeated properties, such as the property that bullets don't use hitpoints:

<useHitPoints>False</useHitPoints>


The last <thingDef> on top of the file is one with the Name type BaseHumanGun and the ParentName type BaseGun. It inherits the contents of BaseGun and is inherited by everything with <thingDef ParentName="BaseHumanGun">:

<ThingDef Name="BaseHumanGun" ParentName="BaseGun" Abstract="True">


Breakdown

Let's start off with the first chunks of this code:

<ThingDef Name="BaseGun" Abstract="True">
</ThingDef>

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

<ThingDef Name="BaseBullet" Abstract="True">
</ThingDef>
<ThingDef Name="BaseGun" Abstract="True">
<ThingDef> The name of this tag, which is read by the game and processed into a correct definition based on this name.
All tags in ../Mods/Core/Defs/ThingDefs/ can be of this type.
Name="BaseGun" The Name type of this tag. This tag is a parent with the Name value of "BaseGun".
If you find the only purpose of a <thingDef> is to be inherited from, make sure is has the Abstract="True" type.
Abstract="True" The Abstract type of this tag is True.
This makes it so that the contents of this tag aren't instantiated, which in practice means the contents of it can only be inherited by other tags and won't be loaded into the game because its only purpose is in inheritance, in being a parent.

"Is the only use of this <thingDef> to be inherited from? Yes: add Abstract="True". No: don't."
<ThingDef Name="BaseHumanGun" ParentName="BaseGun" Abstract="True">
ParentName="BaseGun" The ParentName type of this tag. This tag inherits from a parent with the Name value of "BaseGun".
If you find the only purpose of a <thingDef> is to be inherited from, make sure is has the Abstract="True" type.


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%gt; 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 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 guns that don't entirely follow the previously described structure.