RimWorld-CCL-Reborn / PlagueGun

The completed mod from the tutorial
MIT License
10 stars 4 forks source link

Type coercion in Impact() causes ticking error #1

Open kyleoneill opened 4 years ago

kyleoneill commented 4 years ago

The code hitThing is Pawn hitPawn in the if statement of the Impact override causes the following exception in Rimworld. I used Visual Studio Community 2019 as my compiler.

Exception ticking Bullet_PlagueGun46757: System.NullReferenceException: Object reference not set to an instance of an object at Plague.Projectile_PlagueBullet.Impact (Verse.Thing hitThing) [0x00000] in <filename unknown>:0 at Verse.Projectile.ImpactSomething () [0x00000] in <filename unknown>:0 at Verse.Projectile.Tick () [0x00000] in <filename unknown>:0 at Verse.TickList.Tick () [0x00000] in <filename unknown>:0 Verse.Log:Error(String, Boolean) Verse.TickList:Tick() Verse.TickManager:DoSingleTick() Verse.TickManager:TickManagerUpdate() Verse.Game:UpdatePlay() Verse.Root_Play:Update()

The error can be fixed by adding a new if statement to check if the hitThing is a Pawn, like if (hitThing is Pawn) and putting the rest of the code for Impact inside the block.

Mehni commented 4 years ago

That's not Type coercion; it's pattern matching. It's nullsafe.

Make sure to use hitPawn in the rest of your code, not hitThing.