Skip to content

Commit

Permalink
Fix Demonstration recorder null reference bug (#1994)
Browse files Browse the repository at this point in the history
* Fix for recording mid-play

* Change docstring

* Add additional null check
  • Loading branch information
awjuliani committed May 13, 2019
1 parent d121527 commit 4e00306
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions UnitySDK/Assets/ML-Agents/Scripts/DemonstrationRecorder.cs
@@ -1,4 +1,4 @@
using UnityEngine;
using UnityEngine;
using System.Text.RegularExpressions;

namespace MLAgents
Expand All @@ -16,24 +16,37 @@ public class DemonstrationRecorder : MonoBehaviour
private DemonstrationStore demoStore;
public const int MAX_NAME_LENGTH = 16;

/// <summary>
/// Initializes Demonstration store.
/// </summary>
private void Start()
{
if (Application.isEditor && record)
{
recordingAgent = GetComponent<Agent>();
demoStore = new DemonstrationStore();
demonstrationName = SanitizeName(demonstrationName, MAX_NAME_LENGTH);
demoStore.Initialize(
demonstrationName,
recordingAgent.brain.brainParameters,
recordingAgent.brain.name);
Monitor.Log("Recording Demonstration of Agent: ", recordingAgent.name);
InitializeDemoStore();
}
}

private void Update()
{
if (Application.isEditor && record && demoStore == null)
{
InitializeDemoStore();
}
}

/// <summary>
/// Creates demonstration store for use in recording.
/// </summary>
private void InitializeDemoStore()
{
recordingAgent = GetComponent<Agent>();
demoStore = new DemonstrationStore();
demonstrationName = SanitizeName(demonstrationName);
demoStore.Initialize(
demonstrationName,
recordingAgent.brain.brainParameters,
recordingAgent.brain.name);
Monitor.Log("Recording Demonstration of Agent: ", recordingAgent.name);
}

/// <summary>
/// Removes all characters except alphanumerics from demonstration name.
/// Shorten name if it is longer than the maxNameLength.
Expand Down Expand Up @@ -63,7 +76,7 @@ public void WriteExperience(AgentInfo info)
/// </summary>
private void OnApplicationQuit()
{
if (Application.isEditor && record)
if (Application.isEditor && record && demoStore != null)
{
demoStore.Close();
}
Expand Down

0 comments on commit 4e00306

Please sign in to comment.