Difference between revisions of "Modding Tutorials/DefModExtension"

From RimWorld Wiki
Jump to navigation Jump to search
(Created page with "{{stub}} {{BackToTutorials}} [https://ludeon.com/forums/index.php?topic=46351.0 Mod Extensions]")
 
(first iteration - more code to follow)
Line 2: Line 2:
 
{{BackToTutorials}}
 
{{BackToTutorials}}
  
[https://ludeon.com/forums/index.php?topic=46351.0 Mod Extensions]
+
A DefModExtension is a way to add fields to Defs. DefModExtensions are a simple way to extend functionality to a Def.
 +
 
 +
'''Benefits''':<br/>
 +
<nowiki>+</nowiki> Is really simple and light-weight to use.<br/>
 +
<nowiki>+</nowiki> Works on any Def.<br/>
 +
<nowiki>+</nowiki> Exposes its functionality to XML.<br/>
 +
<nowiki>+</nowiki> Compatible with savefiles, other mods, you name it.</br>
 +
<nowiki>+</nowiki> Does not come with the compatibility issues and pitfalls of creating a custom Def class.<br/>
 +
<nowiki>+</nowiki> Does not have the overhead of a [[Modding Tutorials/ThingComp|ThingComp]].<br/>
 +
<nowiki>+</nowiki> More broadly applicable than a [[Modding Tutorials/ThingComp|ThingComp]] (not every Def is a ThingWithComps!).<br/>
 +
 
 +
'''Downsides''':<br>
 +
<nowiki>-</nowiki> Static and global data.<br/>
 +
<nowiki>-</nowiki> Can't save data.<br/>
 +
<nowiki>-</nowiki> Only works on Defs.<br/>
 +
 
 +
=Requirements=
 +
You know the deal by now.
 +
* Solution
 +
* Custom code
 +
* C#
 +
* XML
 +
 
 +
=The Code=
 +
<source lang="csharp">
 +
using Verse;
 +
using RimWorld;
 +
 
 +
namespace ExampleNameSpace
 +
{
 +
    public class RimQuest_ModExtension : DefModExtension
 +
    {
 +
        public bool canBeARimQuest = true;
 +
    }
 +
}
 +
</source>
 +
That's it. DefModExtensions can of course contain any field you wish: a C# Type, a Thingfilter, a list -- but for the sake of a simple example, we'll use a bool.
 +
 
 +
=See also=
 +
[https://ludeon.com/forums/index.php?topic=46351.0 Ludeon thread on Mod Extensions]<br>
 +
[https://github.com/jecrell/RimQuest/commit/54413b821233316c4057db87031aa6e528ab2db6 The pull request the code this article is based on]
 +
 
 +
[[Category: Modding]]
 +
[[Category: Modding tutorials]]

Revision as of 16:35, 30 January 2019

Modding Tutorials

A DefModExtension is a way to add fields to Defs. DefModExtensions are a simple way to extend functionality to a Def.

Benefits:
+ Is really simple and light-weight to use.
+ Works on any Def.
+ Exposes its functionality to XML.
+ Compatible with savefiles, other mods, you name it.
+ Does not come with the compatibility issues and pitfalls of creating a custom Def class.
+ Does not have the overhead of a ThingComp.
+ More broadly applicable than a ThingComp (not every Def is a ThingWithComps!).

Downsides:
- Static and global data.
- Can't save data.
- Only works on Defs.

Requirements

You know the deal by now.

  • Solution
  • Custom code
  • C#
  • XML

The Code

using Verse;
using RimWorld;

namespace ExampleNameSpace
{
    public class RimQuest_ModExtension : DefModExtension
    {
        public bool canBeARimQuest = true;
    }
}

That's it. DefModExtensions can of course contain any field you wish: a C# Type, a Thingfilter, a list -- but for the sake of a simple example, we'll use a bool.

See also

Ludeon thread on Mod Extensions
The pull request the code this article is based on