From c7a357d84c62df5bdec9de475ae0c49a3bcb8b5b Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Sun, 13 Aug 2017 20:00:52 +0300 Subject: [PATCH] Fix for exceptions when a human is targeting a removed entity while debugdraw is enabled, accessing AITarget position properties after the target has been destroyed doesn't throw an exception (but does log an error in debug builds). --- .../Source/Characters/AI/HumanAIController.cs | 2 +- .../Source/Characters/AI/AITarget.cs | 26 +++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Characters/AI/HumanAIController.cs b/Barotrauma/BarotraumaClient/Source/Characters/AI/HumanAIController.cs index 0cd62a1ad..07db87e3a 100644 --- a/Barotrauma/BarotraumaClient/Source/Characters/AI/HumanAIController.cs +++ b/Barotrauma/BarotraumaClient/Source/Characters/AI/HumanAIController.cs @@ -21,7 +21,7 @@ namespace Barotrauma public override void DebugDraw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch) { - if (selectedAiTarget != null) + if (selectedAiTarget?.Entity != null) { GUI.DrawLine(spriteBatch, new Vector2(Character.DrawPosition.X, -Character.DrawPosition.Y), diff --git a/Barotrauma/BarotraumaShared/Source/Characters/AI/AITarget.cs b/Barotrauma/BarotraumaShared/Source/Characters/AI/AITarget.cs index 11c42b50d..12491c615 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/AI/AITarget.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/AI/AITarget.cs @@ -31,12 +31,34 @@ namespace Barotrauma public Vector2 WorldPosition { - get { return Entity.WorldPosition; } + get + { + if (Entity == null) + { +#if DEBUG + DebugConsole.ThrowError("Attempted to access a removed AITarget\n" + Environment.StackTrace); +#endif + return Vector2.Zero; + } + + return Entity.WorldPosition; + } } public Vector2 SimPosition { - get { return Entity.SimPosition; } + get + { + if (Entity == null) + { +#if DEBUG + DebugConsole.ThrowError("Attempted to access a removed AITarget\n" + Environment.StackTrace); +#endif + return Vector2.Zero; + } + + return Entity.SimPosition; + } } public AITarget(Entity e)