Editing Modding Tutorials/Def classes

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
 
{{BackToTutorials}}
 
{{BackToTutorials}}
  
This tutorial provides you with an introduction to Def Classes in C#. It shows what effects each part of the C# code has on the XML format. First a vanilla example, then it will guide you through making your own.<br/><br/>
+
This tutorial provides you with an introduction to Def Classes in C#. It shows what effects each part of the C# code has on the XML format.<br/><br/>
  
 
=Requirements=
 
=Requirements=
Line 8: Line 8:
 
* [[Modding Tutorials/Decompiling source code|Decompiling source code]] is required.<br/><br/>
 
* [[Modding Tutorials/Decompiling source code|Decompiling source code]] is required.<br/><br/>
  
=Vanilla defClasses=
+
=Modifying defs=
  
This chapter helps you develop a broad understanding of the def classes. First we take a look at tags inside <ThingDef>, then we take a look at tags inside those tags and finally we take a look at &lt;li&gt; items.<br/><br/>
+
There's a few ways to modify existing def formats. We'll go over writing a ''custom defClass'', ''custom comp'' and other ways of integrating XML tags into C# for example through the use of ''weaponTags'' or ''apparelTags''.<br/><br/>
  
==Tags==
+
==Vanilla defClasses==
 +
 
 +
This chapter helps you develop a broad understanding of the def classes. First we take a look at tags inside <thingDef>, then we take a look at tags inside those tags and finally we take a look at &lt;li&gt; items.<br/><br/>
 +
 
 +
===Tags===
  
 
The structure of a base game defClass might look like this:<br/>
 
The structure of a base game defClass might look like this:<br/>
Line 92: Line 96:
 
*Note: these are snippets of Verse.ThingDef. To find the full source please [[Modding Tutorials/Decompiling source code|decompile the source code]] yourself.<br/><br/>
 
*Note: these are snippets of Verse.ThingDef. To find the full source please [[Modding Tutorials/Decompiling source code|decompile the source code]] yourself.<br/><br/>
  
==Subtags==
+
===Subtags===
  
 
To explain how subtags work we'll take a look at (parts of) StuffProperties. The contents of the StuffProperties class, formally called Verse.StuffProperties, are very similar to the ThingDef class. The structure is mostly the same.<br/>
 
To explain how subtags work we'll take a look at (parts of) StuffProperties. The contents of the StuffProperties class, formally called Verse.StuffProperties, are very similar to the ThingDef class. The structure is mostly the same.<br/>
Line 122: Line 126:
 
This is one of the reasons you might be better off [[Modding Tutorials/Modifying defs#Custom comp|writing a custom comp]].<br/><br/>
 
This is one of the reasons you might be better off [[Modding Tutorials/Modifying defs#Custom comp|writing a custom comp]].<br/><br/>
  
==Lists==
+
===Lists===
  
 
Some tags include a list (it contains &lt;li&gt; subtags). If you look at the subtags of StuffProperties you can see two List<SomeType> elements. If you look at the XML you can see a clear distinction between the two:<br/>
 
Some tags include a list (it contains &lt;li&gt; subtags). If you look at the subtags of StuffProperties you can see two List<SomeType> elements. If you look at the XML you can see a clear distinction between the two:<br/>
Line 164: Line 168:
  
 
Because of this code the two act completely differently. One is a list with integer keys and StuffCategoryDef values, the other is a list with StatDef keys and float values.<br/><br/>
 
Because of this code the two act completely differently. One is a list with integer keys and StuffCategoryDef values, the other is a list with StatDef keys and float values.<br/><br/>
 
+
<!--
=Making your own=
+
Would be nice to expand on this.
 +
-->
  
 
This is where you create custom defs for your mod and C# code to go along with them.
 
This is where you create custom defs for your mod and C# code to go along with them.
Line 171: Line 176:
 
So if you have your own objects that you've created, and have C# code to go with them, it's fairly straightforward to add a custom def.  On the other hand, if you want to add a custom def to a core def that gets used everywhere in the code, it will be extremely difficult and you might be better off with a different approach.
 
So if you have your own objects that you've created, and have C# code to go with them, it's fairly straightforward to add a custom def.  On the other hand, if you want to add a custom def to a core def that gets used everywhere in the code, it will be extremely difficult and you might be better off with a different approach.
  
 
+
===XML/Code Requirements===
==XML/Code Requirements==
 
 
To make a custom def, you need:
 
To make a custom def, you need:
 
# The XML must have <code><ThingDef Class="MyNamespace.MyCustomDefName"></code>
 
# The XML must have <code><ThingDef Class="MyNamespace.MyCustomDefName"></code>
Line 178: Line 182:
 
# Code needs to access the new fields - probably using casting, such as <code>var def = thing.def as MyCustomDefName;</code>
 
# Code needs to access the new fields - probably using casting, such as <code>var def = thing.def as MyCustomDefName;</code>
  
==Simple Example==
+
===Simple Example===
 
Suppose you add a new gun to RimWorld (assuming you're violent), that has a special feature.
 
Suppose you add a new gun to RimWorld (assuming you're violent), that has a special feature.
  
Line 248: Line 252:
  
  
==Further Example==
+
===Further Example===
 
You might want to add a whole class of guns with this new feature:
 
You might want to add a whole class of guns with this new feature:
  
Line 280: Line 284:
 
Each will inherit the parent's def class and link in with your code, and you can have a whole class of things using your cool new weapon mod.
 
Each will inherit the parent's def class and link in with your code, and you can have a whole class of things using your cool new weapon mod.
  
==When Not To Use It==
+
===When Not To Use It===
  
 
There are two big catches:
 
There are two big catches:
Line 287: Line 291:
 
#  Your defs only work for your own code.  An example:
 
#  Your defs only work for your own code.  An example:
  
===Problem Example===
+
====Problem Example====
 
''The following method shows you '''why not to use''' custom defs. In some cases you could attempt to use this method but it's generally considered the least favourable way to deal with things.''
 
''The following method shows you '''why not to use''' custom defs. In some cases you could attempt to use this method but it's generally considered the least favourable way to deal with things.''
  
Line 362: Line 366:
  
  
==Inheritance==
+
===Older Versions===
Class = "MyNamespace.myCustomClass" does not inherit in XML. That means Def classes require a pretty hefty overhaul of XML code to work. First of all you will have to change anything in the inheritance to refer to the same class:<br/>
+
***NOTE: This seems to work fine in 0.18.1722, so you can ignore this section***
 +
 
 +
Def classes require a pretty hefty overhaul of XML code to work. First of all you will have to change anything in the inheritance to refer to the same class:<br/>
  
 
<source lang="xml"><?xml version="1.0" encoding="utf-8"?>
 
<source lang="xml"><?xml version="1.0" encoding="utf-8"?>
 
<Defs>
 
<Defs>
<ThingDef Name="BaseThing" Class="MyNamespace.MyCustomClass">
+
<ThingDef Name="BaseThing" Class="MyNamespace.myCustomClass">
 
</ThingDef>
 
</ThingDef>
  
<ThingDef Name="BaseSpecificThing" ParentName="BaseThing" Class="MyNamespace.MyCustomClass">
+
<ThingDef Name="BaseSpecificThing" ParentName="BaseThing" Class="MyNamespace.myCustomClass">
 
</ThingDef>
 
</ThingDef>
  
<ThingDef ParentName="BaseSpecificThing" Class="MyNamespace.MyCustomClass">
+
<ThingDef ParentName="BaseSpecificThing" Class="MyNamespace.myCustomClass">
 
</ThingDef>
 
</ThingDef>
 
</Defs></source><br/>
 
</Defs></source><br/>
  
And in case you have more ThingDefs which require the changes that come with MyNamespace.MyCustomClass you'll have to set all of their classes for it to work.<br/>
+
And in case you have more ThingDefs which require the changes that come with MyNamespace.myCustomClass you'll have to set all of their classes for it to work.<br/>
 
Applied to core defs this way of doing things introduces incompatibilities with other mods that modify the same def. Creating compatibility patches is inevitable.<br/><br/>
 
Applied to core defs this way of doing things introduces incompatibilities with other mods that modify the same def. Creating compatibility patches is inevitable.<br/><br/>
 +
  
 
=See also=
 
=See also=

Please note that all contributions to RimWorld Wiki are considered to be released under the CC BY-SA 3.0 (see RimWorld Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)

Template used on this page:

This page is a member of 1 hidden category: