[Solved]Creating entirely new def

Started by Latta, May 04, 2015, 04:52:11 AM

Previous topic - Next topic

Latta

Yes, new def. What I want to make is an InfusionDef for my Infusions. I can try to make custom ThingDef and put everything there but...
Anyway, is there a mod that I can use as a reference? Or anyone?

mipen

This is actually deceptively easy to do :) All you need to do is create a new class and inherit from the Def class, so -> TestDef : Def  Make sure to include 'Def' on the end of your class name. So lets use your InfusionDef as an example. First, make the new class:


public class InfusionDef : Def
{
}


Now, add the fields you want to be able to use:


public class InfusionDef : Def
{
public string Name;
public int number;
public ThingDef thing;

public bool allowed = true;
//You can initialise these values with a default value that you wish to use
}


Once you have your Def class set out, the game will automatically search the mod folders for any xml files that use this class. To add some of these defs, first add a new folder inside your mod's Defs folder. Call this the same name used for your def class and add an 's' on the end. So using the InfusionDef as an example, the folder name will be InfusionDefs.
Inside this folder, make a new xml file as normal, and use the same encoding (<?xml version="1.0" encoding="utf-8" ?>). For the first tag, use the folder name (in this case, it will be InfusionDefs) and then you can start defs as normal, i.e:


<?xml version="1.0" encoding="utf-8" ?>
<InfusionDefs>
<InfusionDef>
<defName>InfusionDef1</defName>
<number>10040</number>
<Name>Infusion 1</Name>
<allowed>false</allowed>
<InfusionDef>
</InfusionDefs>


And so you can see, once you have set up the class, it is the same as doing a core def :)

To access the def, it is the same method:

InfusionDef infusionDef = DefDatabase<InfusionDef>.GetNamed("InfusionDef1");

Hopefully I managed to explain that properly, if you have any questions feel free to ask!

Latta

Thank you a lot! I'm now overhauling my mod source to use custom def. Huge credit to you if I success 8)