Skip to content

Commit 0e6ac12

Browse files
author
Chris Elion
authored
check for missing AbstractBoard, display warning (#5276) (#5278)
1 parent da50376 commit 0e6ac12

File tree

7 files changed

+48
-0
lines changed

7 files changed

+48
-0
lines changed

Project/Assets/ML-Agents/Examples/Match3/Scripts/Match3Drawer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ void OnDrawGizmos()
105105
if (!m_Board)
106106
{
107107
m_Board = GetComponent<Match3Board>();
108+
if (m_Board == null)
109+
{
110+
return;
111+
}
108112
}
109113

110114
var currentSize = m_Board.GetCurrentBoardSize();

com.unity.ml-agents/Editor/Match3ActuatorComponentEditor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ public override void OnInspectorGUI()
1111
var so = serializedObject;
1212
so.Update();
1313

14+
var component = (Match3ActuatorComponent)target;
15+
var board = component.GetComponent<AbstractBoard>();
16+
if (board == null)
17+
{
18+
EditorGUILayout.HelpBox("You must provide an implementation of an AbstractBoard.", MessageType.Warning);
19+
return;
20+
}
21+
1422
// Drawing the RenderTextureComponent
1523
EditorGUI.BeginChangeCheck();
1624

com.unity.ml-agents/Editor/Match3SensorComponentEditor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ public override void OnInspectorGUI()
1111
var so = serializedObject;
1212
so.Update();
1313

14+
var component = (Match3SensorComponent)target;
15+
var board = component.GetComponent<AbstractBoard>();
16+
if (board == null)
17+
{
18+
EditorGUILayout.HelpBox("You must provide an implementation of an AbstractBoard.", MessageType.Warning);
19+
return;
20+
}
21+
1422
// Drawing the RenderTextureComponent
1523
EditorGUI.BeginChangeCheck();
1624

com.unity.ml-agents/Runtime/Integrations/Match3/Match3ActuatorComponent.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Unity.MLAgents.Actuators;
23
using UnityEngine;
34
using UnityEngine.Serialization;
@@ -52,6 +53,11 @@ public bool ForceHeuristic
5253
public override IActuator[] CreateActuators()
5354
{
5455
var board = GetComponent<AbstractBoard>();
56+
if (!board)
57+
{
58+
return Array.Empty<IActuator>();
59+
}
60+
5561
var seed = m_RandomSeed == -1 ? gameObject.GetInstanceID() : m_RandomSeed + 1;
5662
return new IActuator[] { new Match3Actuator(board, m_ForceHeuristic, seed, m_ActuatorName) };
5763
}

com.unity.ml-agents/Runtime/Integrations/Match3/Match3SensorComponent.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public override ISensor[] CreateSensors()
4545
Dispose();
4646

4747
var board = GetComponent<AbstractBoard>();
48+
if (!board)
49+
{
50+
return Array.Empty<ISensor>();
51+
}
4852
var cellSensor = Match3Sensor.CellTypeSensor(board, m_ObservationType, m_SensorName + " (cells)");
4953
// This can be null if numSpecialTypes is 0
5054
var specialSensor = Match3Sensor.SpecialTypeSensor(board, m_ObservationType, m_SensorName + " (special)");

com.unity.ml-agents/Tests/Editor/Integrations/Match3/Match3ActuatorTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,14 @@ public void TestMasking(bool fullBoard)
194194
// And they should add up to all the potential moves
195195
Assert.AreEqual(validIndices.Count + masks.HashSets[0].Count, board.NumMoves());
196196
}
197+
198+
[Test]
199+
public void TestNoBoardReturnsEmptyActuators()
200+
{
201+
var gameObj = new GameObject("board");
202+
var actuatorComponent = gameObj.AddComponent<Match3ActuatorComponent>();
203+
var actuators = actuatorComponent.CreateActuators();
204+
Assert.AreEqual(0, actuators.Length);
205+
}
197206
}
198207
}

com.unity.ml-agents/Tests/Editor/Integrations/Match3/Match3SensorTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,5 +395,14 @@ byte[] LoadPNGs(string pathPrefix, int numExpected)
395395

396396
return bytesOut.ToArray();
397397
}
398+
399+
[Test]
400+
public void TestNoBoardReturnsEmptySensors()
401+
{
402+
var gameObj = new GameObject("board");
403+
var sensorComponent = gameObj.AddComponent<Match3SensorComponent>();
404+
var sensors = sensorComponent.CreateSensors();
405+
Assert.AreEqual(0, sensors.Length);
406+
}
398407
}
399408
}

0 commit comments

Comments
 (0)