Skip to content

Commit 729b4cf

Browse files
Add CreateActuators API, obsolete old method. (#4899)
* Add CreateActuators method to the ActuatorComponent class which wraps the original method. The original method will be removed in the future. Co-authored-by: Vincent-Pierre BERGES <vincentpierre@unity3d.com>
1 parent d7652b8 commit 729b4cf

File tree

8 files changed

+45
-2
lines changed

8 files changed

+45
-2
lines changed

Project/Assets/ML-Agents/Examples/Basic/Scripts/BasicActuatorComponent.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public class BasicActuatorComponent : ActuatorComponent
1717
/// Creates a BasicActuator.
1818
/// </summary>
1919
/// <returns></returns>
20+
#pragma warning disable 672
2021
public override IActuator CreateActuator()
22+
#pragma warning restore 672
2123
{
2224
return new BasicActuator(basicController);
2325
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ namespace Unity.MLAgentsExamples
77
public class Match3ExampleActuatorComponent : Match3ActuatorComponent
88
{
99
/// <inheritdoc/>
10+
#pragma warning disable 672
1011
public override IActuator CreateActuator()
12+
#pragma warning restore 672
1113
{
1214
var board = GetComponent<Match3Board>();
1315
var agent = GetComponentInParent<Agent>();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public class Match3ActuatorComponent : ActuatorComponent
2828
public bool ForceHeuristic;
2929

3030
/// <inheritdoc/>
31+
#pragma warning disable 672
3132
public override IActuator CreateActuator()
33+
#pragma warning restore 672
3234
{
3335
var board = GetComponent<AbstractBoard>();
3436
var agent = GetComponentInParent<Agent>();

com.unity.ml-agents/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ removed when training with a player. The Editor still requires it to be clamped
3030
additional memory allocations. (#4887)
3131
- Added `ObservationWriter.AddList()` and deprecated `ObservationWriter.AddRange()`.
3232
`AddList()` is recommended, as it does not generate any additional memory allocations. (#4887)
33+
- Added `ActuatorComponent.CreateActuators`, and deprecate `ActuatorComponent.CreateActuator`. The
34+
default implementation will wrap `ActuatorComponent.CreateActuator` in an array and return that. (#4899)
3335

3436
#### ml-agents / ml-agents-envs / gym-unity (Python)
3537
- Added a `--torch-device` commandline option to `mlagents-learn`, which sets the default

com.unity.ml-agents/Runtime/Actuators/ActuatorComponent.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using UnityEngine;
23

34
namespace Unity.MLAgents.Actuators
@@ -12,8 +13,21 @@ public abstract class ActuatorComponent : MonoBehaviour
1213
/// Create the IActuator. This is called by the Agent when it is initialized.
1314
/// </summary>
1415
/// <returns>Created IActuator object.</returns>
16+
[Obsolete("Use CreateActuators instead.")]
1517
public abstract IActuator CreateActuator();
1618

19+
/// <summary>
20+
/// Create a collection of <see cref="IActuator"/>s. This is called by the <see cref="Agent"/> during
21+
/// initialization.
22+
/// </summary>
23+
/// <returns>A collection of <see cref="IActuator"/>s</returns>
24+
public virtual IActuator[] CreateActuators()
25+
{
26+
#pragma warning disable 618
27+
return new[] { CreateActuator() };
28+
#pragma warning restore 618
29+
}
30+
1731
/// <summary>
1832
/// The specification of the possible actions for this ActuatorComponent.
1933
/// This must produce the same results as the corresponding IActuator's ActionSpec.

com.unity.ml-agents/Runtime/Actuators/ActuatorManager.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,18 @@ void ClearBufferSizes()
369369
NumContinuousActions = NumDiscreteActions = SumOfDiscreteBranchSizes = 0;
370370
}
371371

372+
/// <summary>
373+
/// Add an array of <see cref="IActuator"/>s at once.
374+
/// </summary>
375+
/// <param name="actuators">The array of <see cref="IActuator"/>s to add.</param>
376+
public void AddActuators(IActuator[] actuators)
377+
{
378+
for (var i = 0; i < actuators.Length; i++)
379+
{
380+
Add(actuators[i]);
381+
}
382+
}
383+
372384
/*********************************************************************************
373385
* IList implementation that delegates to m_Actuators List. *
374386
*********************************************************************************/
@@ -432,7 +444,7 @@ public bool Remove(IActuator item)
432444
public int Count => m_Actuators.Count;
433445

434446
/// <inheritdoc/>
435-
public bool IsReadOnly => m_Actuators.IsReadOnly;
447+
public bool IsReadOnly => false;
436448

437449
/// <inheritdoc/>
438450
public int IndexOf(IActuator item)

com.unity.ml-agents/Runtime/Agent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ void InitializeActuators()
10061006

10071007
foreach (var actuatorComponent in attachedActuators)
10081008
{
1009-
m_ActuatorManager.Add(actuatorComponent.CreateActuator());
1009+
m_ActuatorManager.AddActuators(actuatorComponent.CreateActuators());
10101010
}
10111011
}
10121012

docs/Migrating.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ double-check that the versions are in the same. The versions can be found in
2121
- `VectorSensor.AddObservation(IEnumerable<float>)` is deprecated. Use `VectorSensor.AddObservation(IList<float>)`
2222
instead.
2323
- `ObservationWriter.AddRange()` is deprecated. Use `ObservationWriter.AddList()` instead.
24+
- `ActuatorComponent.CreateAcuator()` is deprecated. Please use override `ActuatorComponent.CreateActuators`
25+
instead. Since `ActuatorComponent.CreateActuator()` is abstract, you will still need to override it in your
26+
class until it is removed. It is only ever called if you don't override `ActuatorComponent.CreateActuators`.
27+
You can suppress the warnings by surrounding the method with the following pragma:
28+
```c#
29+
#pragma warning disable 672
30+
public IActuator CreateActuator() { ... }
31+
#pragma warning restore 672
32+
```
2433

2534

2635
# Migrating

0 commit comments

Comments
 (0)