jeudi 15 juillet 2021

Nested string to class deserialization using YamlDotNet

I need to be able to dynamically deserialize strings (as properties) into classes during runtime. I also need to be able to do this in a way that supports nesting.

Example - Turret which shoots exploding bullets

Turret.yaml

Type: Turret
Name: MiniTurret
DisplayName: Mini Turret
MaxHP: 100
Damage: 50
TurretTurnSpeed: 20
Sprite: turret_1
ProjectileName: ExplosiveBullet

ExplosiveBullet.yaml

Type: Bullet
Name: ExplosiveBullet
DisplayName: Explosive Bullet
Damage: 50
TurretTurnSpeed: 20
Sprite: explosivebullet1
Components:
   - ExplosionComp:
      - ExplosionType: Incendiary
      - Damage: 10
      - Radius: 5

We're trying to create a turret (with the Turret.cs class, as stated in the "Type" field) that shoots projectiles with a Def name "ExplosiveBullet". At runtime, we deserialize the string "ExplosiveBullet" into it's appropriate class. The bullet now does the same with the list of Components - it needs to deserialize the string "ExplosionComp" into ExplosionComp.cs, and pass in the Damage and Radius values.

This way, I'm trying to created a nested component system which can reference other components (such as a turret referencing bullets, and a bullet referencing an explosion effect it causes when it's destroyed).

Does reflection have to be used to solve this? Is there anything built into YamlDotNet specifically to handle this?

I've seen dated or convoluted solutions; is there a good approach to handle this problem with YamlDotNet?





Aucun commentaire:

Enregistrer un commentaire