From c5d490b8b2b2fff747eab90afc8d1a8299507a0a Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Fri, 25 Nov 2022 11:44:58 +0800 Subject: [PATCH] Added null checks for AI blackboard data - Null checks added to prevent AI causing the engine to crash --- .../AIBehaviour/Implemented/LeafNodes/LeafAttack.cs | 10 +++++++++- .../AIBehaviour/Implemented/LeafNodes/LeafChase.cs | 2 +- .../AIBehaviour/Implemented/LeafNodes/LeafPatrol.cs | 4 ++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafAttack.cs b/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafAttack.cs index e64b63ad..56b5e1ef 100644 --- a/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafAttack.cs +++ b/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafAttack.cs @@ -41,7 +41,15 @@ public partial class LeafAttack : BehaviourTreeNode onEnter(BehaviourTreeNodeStatus.RUNNING); //Succeed when stand in hurt box for long enough - float captureTime = (float)GetNodeData("captureTimeLeft"); + //Debug.Log("Attempting to get blackboard data"); + float? captureTime = (float?)GetNodeData("captureTimeLeft"); + //Debug.Log("Got blackboard data"); + if (captureTime == null) + { + status = BehaviourTreeNodeStatus.FAILURE; + onExit(BehaviourTreeNodeStatus.FAILURE); + return status; + } captureTime -= Time.DeltaTimeF; SetNodeData("captureTimeLeft", captureTime); //Debug.Log(captureTime.ToString()); diff --git a/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafChase.cs b/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafChase.cs index 10562fc9..cab81420 100644 --- a/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafChase.cs +++ b/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafChase.cs @@ -119,7 +119,7 @@ public partial class LeafChase : BehaviourTreeNode //Play SFX if (GetNodeData("isCapturing") != null && (bool)GetNodeData("isCapturing") == false) { - Debug.Log("AI Play capturing SFX"); + //Debug.Log("AI Play capturing SFX"); } SetNodeData("isCapturing", true); diff --git a/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafPatrol.cs b/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafPatrol.cs index 248fa8b9..3f66bf94 100644 --- a/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafPatrol.cs +++ b/Assets/Scripts/AIBehaviour/Implemented/LeafNodes/LeafPatrol.cs @@ -84,6 +84,10 @@ public partial class LeafPatrol : BehaviourTreeNode } waypoints = (List)GetNodeData("waypoints"); + if (waypoints == null) + { + return; + } Vector3 targetPosition = waypoints[currentWaypointIndex].GetComponent().GlobalPosition; //Reach waypoint by X and Z being near enough