Skip to content

Commit d8db633

Browse files
author
Chris Elion
authored
Use original behavior name on subsequent reset (#5356)
1 parent 8230987 commit d8db633

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class ModelOverrider : MonoBehaviour
4040

4141
string m_BehaviorNameOverrideDirectory;
4242

43+
private string m_OriginalBehaviorName;
44+
4345
private List<string> m_OverrideExtensions = new List<string>();
4446

4547
// Cached loaded NNModels, with the behavior name as the key.
@@ -85,6 +87,23 @@ public bool HasOverrides
8587
}
8688
}
8789

90+
/// <summary>
91+
/// The original behavior name of the agent. The actual behavior name will change when it is overridden.
92+
/// </summary>
93+
public string OriginalBehaviorName
94+
{
95+
get
96+
{
97+
if (string.IsNullOrEmpty(m_OriginalBehaviorName))
98+
{
99+
var bp = m_Agent.GetComponent<BehaviorParameters>();
100+
m_OriginalBehaviorName = bp.BehaviorName;
101+
}
102+
103+
return m_OriginalBehaviorName;
104+
}
105+
}
106+
88107
public static string GetOverrideBehaviorName(string originalBehaviorName)
89108
{
90109
return $"Override_{originalBehaviorName}";
@@ -306,13 +325,11 @@ void OverrideModel()
306325
string overrideError = null;
307326

308327
m_Agent.LazyInitialize();
309-
var bp = m_Agent.GetComponent<BehaviorParameters>();
310-
var behaviorName = bp.BehaviorName;
311328

312329
NNModel nnModel = null;
313330
try
314331
{
315-
nnModel = GetModelForBehaviorName(behaviorName);
332+
nnModel = GetModelForBehaviorName(OriginalBehaviorName);
316333
}
317334
catch (Exception e)
318335
{
@@ -324,18 +341,18 @@ void OverrideModel()
324341
if (string.IsNullOrEmpty(overrideError))
325342
{
326343
overrideError =
327-
$"Didn't find a model for behaviorName {behaviorName}. Make " +
344+
$"Didn't find a model for behaviorName {OriginalBehaviorName}. Make " +
328345
"sure the behaviorName is set correctly in the commandline " +
329346
"and that the model file exists";
330347
}
331348
}
332349
else
333350
{
334351
var modelName = nnModel != null ? nnModel.name : "<null>";
335-
Debug.Log($"Overriding behavior {behaviorName} for agent with model {modelName}");
352+
Debug.Log($"Overriding behavior {OriginalBehaviorName} for agent with model {modelName}");
336353
try
337354
{
338-
m_Agent.SetModel(GetOverrideBehaviorName(behaviorName), nnModel);
355+
m_Agent.SetModel(GetOverrideBehaviorName(OriginalBehaviorName), nnModel);
339356
overrideOk = true;
340357
}
341358
catch (Exception e)

0 commit comments

Comments
 (0)