Skip to content

Commit

Permalink
check for missing AbstractBoard, display warning (#5276)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Elion authored Apr 16, 2021
1 parent da50376 commit 2fae4c0
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ void OnDrawGizmos()
if (!m_Board)
{
m_Board = GetComponent<Match3Board>();
if (m_Board == null)
{
return;
}
}

var currentSize = m_Board.GetCurrentBoardSize();
Expand Down
8 changes: 8 additions & 0 deletions com.unity.ml-agents/Editor/Match3ActuatorComponentEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ public override void OnInspectorGUI()
var so = serializedObject;
so.Update();

var component = (Match3ActuatorComponent)target;
var board = component.GetComponent<AbstractBoard>();
if (board == null)
{
EditorGUILayout.HelpBox("You must provide an implementation of an AbstractBoard.", MessageType.Warning);
return;
}

// Drawing the RenderTextureComponent
EditorGUI.BeginChangeCheck();

Expand Down
8 changes: 8 additions & 0 deletions com.unity.ml-agents/Editor/Match3SensorComponentEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ public override void OnInspectorGUI()
var so = serializedObject;
so.Update();

var component = (Match3SensorComponent)target;
var board = component.GetComponent<AbstractBoard>();
if (board == null)
{
EditorGUILayout.HelpBox("You must provide an implementation of an AbstractBoard.", MessageType.Warning);
return;
}

// Drawing the RenderTextureComponent
EditorGUI.BeginChangeCheck();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Unity.MLAgents.Actuators;
using UnityEngine;
using UnityEngine.Serialization;
Expand Down Expand Up @@ -52,6 +53,11 @@ public bool ForceHeuristic
public override IActuator[] CreateActuators()
{
var board = GetComponent<AbstractBoard>();
if (!board)
{
return Array.Empty<IActuator>();
}

var seed = m_RandomSeed == -1 ? gameObject.GetInstanceID() : m_RandomSeed + 1;
return new IActuator[] { new Match3Actuator(board, m_ForceHeuristic, seed, m_ActuatorName) };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public override ISensor[] CreateSensors()
Dispose();

var board = GetComponent<AbstractBoard>();
if (!board)
{
return Array.Empty<ISensor>();
}
var cellSensor = Match3Sensor.CellTypeSensor(board, m_ObservationType, m_SensorName + " (cells)");
// This can be null if numSpecialTypes is 0
var specialSensor = Match3Sensor.SpecialTypeSensor(board, m_ObservationType, m_SensorName + " (special)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,14 @@ public void TestMasking(bool fullBoard)
// And they should add up to all the potential moves
Assert.AreEqual(validIndices.Count + masks.HashSets[0].Count, board.NumMoves());
}

[Test]
public void TestNoBoardReturnsEmptyActuators()
{
var gameObj = new GameObject("board");
var actuatorComponent = gameObj.AddComponent<Match3ActuatorComponent>();
var actuators = actuatorComponent.CreateActuators();
Assert.AreEqual(0, actuators.Length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,5 +395,14 @@ byte[] LoadPNGs(string pathPrefix, int numExpected)

return bytesOut.ToArray();
}

[Test]
public void TestNoBoardReturnsEmptySensors()
{
var gameObj = new GameObject("board");
var sensorComponent = gameObj.AddComponent<Match3SensorComponent>();
var sensors = sensorComponent.CreateSensors();
Assert.AreEqual(0, sensors.Length);
}
}
}

0 comments on commit 2fae4c0

Please sign in to comment.