I'm trying to achieve a functionality implemented in the game "RimWorld" with XML, using YamlDotNet.
I am aware of how to deserialize basic information, like so: instanceOfDeserializer.Deserialize<MyBaseClass>(someTextReaderInstance)
I am trying to deserialize a list of derived types into a list of their base types.
The goal is to be able to take a string as a class name, and instantiate it with parameters listed in the YAML. Here's how it's done in XML:
<ThingDef ParentName="ResourceBase">
<defName>Chemfuel</defName>
<label>chemfuel</label>
<description>A volatile liquid chemical. Used to fuel engines and rockets, or to transmute into propellant for projectiles, or as an incendiary weapon.</description>
<stackLimit>150</stackLimit>
<comps>
<li Class="CompProperties_Explosive">
<explosiveRadius>1.1</explosiveRadius>
<explosiveDamageType>Flame</explosiveDamageType>
<explosiveExpandPerStackcount>0.037</explosiveExpandPerStackcount>
<startWickOnDamageTaken>
<li>Flame</li>
</startWickOnDamageTaken>
<startWickHitPointsPercent>0.333</startWickHitPointsPercent>
<preExplosionSpawnThingDef>Filth_Fuel</preExplosionSpawnThingDef>
<preExplosionSpawnChance>1</preExplosionSpawnChance>
<wickTicks>70~150</wickTicks>
</li>
</comps>
</ThingDef>
The comps
list is a list of outside classes I'd like to reference. In this case, we want to add an explosive property to an item chemfuel
. It seems like in XML, the developer referenced the wanted class with the Class="CompProperties_Explosive"
tag. I'd need similar functionality in a YAML document.
Here's how I envisioned this in my YAML document:
Type: Item
Name: fuel_cansiter
DisplayName: Fuel Canister
Sprite: fuel_canister_1
MaxStackSize: 10
MaxHP: 15
Value: 30
Components:
- Explosive:
- Damage: 10
- Range: 25
- Burnable:
- FlameSize: 10
- HealthThreshold: 0.4
I'd like to instantiate this FuelCanister
item with the Explosive.cs
and Burnable.cs
classes as components, but pass the dynamic variables such as damage and range via YAML.
In this case, the derived types are Explosive
and Burnable
, which should all be able to be deserialized into their base type, e.g., Component
.
In the end, The FuelCanister.cs
class should end up with a list of Component
types, from which Explosive
and Burnable
are derived from.
Is there a built-in function in YamlDotNet to easily do this?
EDIT: Perhaps the XML way uses XML attributes to extract the name of the class, and then with that name, we can use Reflection to instantiate the class with passed parameters? Is there a way to do this with YAML?