diff --git a/com.unity.ml-agents/Runtime/Actuators/IActionReceiver.cs b/com.unity.ml-agents/Runtime/Actuators/IActionReceiver.cs index d020dad2e9..4736e34c2b 100644 --- a/com.unity.ml-agents/Runtime/Actuators/IActionReceiver.cs +++ b/com.unity.ml-agents/Runtime/Actuators/IActionReceiver.cs @@ -156,7 +156,7 @@ public override int GetHashCode() /// A float array to pack actions into whose length is greater than or /// equal to the addition of the Lengths of this objects and /// segments. - /// [Obsolete("PackActions has been deprecated.")] + [Obsolete("PackActions has been deprecated.")] public void PackActions(in float[] destination) { Debug.Assert(destination.Length >= ContinuousActions.Length + DiscreteActions.Length, diff --git a/com.unity.ml-agents/Runtime/Agent.cs b/com.unity.ml-agents/Runtime/Agent.cs index 195566d3e2..f28c88a5d9 100644 --- a/com.unity.ml-agents/Runtime/Agent.cs +++ b/com.unity.ml-agents/Runtime/Agent.cs @@ -886,7 +886,11 @@ public virtual void Initialize() { } /// public virtual void Heuristic(in ActionBuffers actionsOut) { - // For backward compatibility + // Disable deprecation warnings so we can call the legacy overload. +#pragma warning disable CS0618 + + // The default implementation of Heuristic calls the + // obsolete version for backward compatibility switch (m_PolicyFactory.BrainParameters.VectorActionSpaceType) { case SpaceType.Continuous: @@ -904,6 +908,8 @@ public virtual void Heuristic(in ActionBuffers actionsOut) actionsOut.ContinuousActions.Clear(); break; } +#pragma warning restore CS0618 + } /// @@ -1151,7 +1157,10 @@ public virtual void WriteDiscreteActionMask(IDiscreteActionMask actionMask) { m_ActionMasker = new DiscreteActionMasker(actionMask); } + // Disable deprecation warnings so we can call the legacy overload. +#pragma warning disable CS0618 CollectDiscreteActionMasks(m_ActionMasker); +#pragma warning restore CS0618 } /// @@ -1232,7 +1241,10 @@ public virtual void OnActionReceived(ActionBuffers actions) { m_LegacyActionCache = Array.ConvertAll(actions.DiscreteActions.Array, x => (float)x); } + // Disable deprecation warnings so we can call the legacy overload. +#pragma warning disable CS0618 OnActionReceived(m_LegacyActionCache); +#pragma warning restore CS0618 } /// diff --git a/com.unity.ml-agents/Runtime/Agent.deprecated.cs b/com.unity.ml-agents/Runtime/Agent.deprecated.cs index 98015eff63..582882637f 100644 --- a/com.unity.ml-agents/Runtime/Agent.deprecated.cs +++ b/com.unity.ml-agents/Runtime/Agent.deprecated.cs @@ -9,6 +9,7 @@ public partial class Agent /// Deprecated, use instead. /// /// + [Obsolete("CollectDiscreteActionMasks has been deprecated, please use WriteDiscreteActionMask.")] public virtual void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker) { } @@ -17,6 +18,7 @@ public virtual void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker /// Deprecated, use instead. /// /// + [Obsolete("The float[] version of Heuristic has been deprecated, please use the ActionBuffers version instead.")] public virtual void Heuristic(float[] actionsOut) { Debug.LogWarning("Heuristic method called but not implemented. Returning placeholder actions."); @@ -27,6 +29,7 @@ public virtual void Heuristic(float[] actionsOut) /// Deprecated, use instead. /// /// + [Obsolete("The float[] version of OnActionReceived has been deprecated, please use the ActionBuffers version instead.")] public virtual void OnActionReceived(float[] vectorAction) { } /// @@ -36,7 +39,7 @@ public virtual void OnActionReceived(float[] vectorAction) { } /// The last action that was decided by the Agent (or null if no decision has been made). /// /// - // [Obsolete("GetAction has been deprecated, please use GetStoredActionBuffers, Or GetStoredDiscreteActions.")] + [Obsolete("GetAction has been deprecated, please use GetStoredActionBuffers instead.")] public float[] GetAction() { var storedAction = m_Info.storedVectorActions; diff --git a/com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs b/com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs index 5768b2989c..28a79977f9 100644 --- a/com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs +++ b/com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs @@ -101,6 +101,8 @@ public static List GetObservationSummaries(this AgentInfoAct /// Whether or not the Brain is training. public static BrainParametersProto ToProto(this BrainParameters bp, string name, bool isTraining) { + // Disable deprecation warnings so we can set legacy fields +#pragma warning disable CS0618 var brainParametersProto = new BrainParametersProto { VectorActionSpaceTypeDeprecated = (SpaceTypeProto)bp.VectorActionSpaceType, @@ -116,6 +118,7 @@ public static BrainParametersProto ToProto(this BrainParameters bp, string name, { brainParametersProto.VectorActionDescriptionsDeprecated.AddRange(bp.VectorActionDescriptions); } +#pragma warning restore CS0618 return brainParametersProto; } diff --git a/com.unity.ml-agents/Runtime/Policies/BrainParameters.cs b/com.unity.ml-agents/Runtime/Policies/BrainParameters.cs index 1ae30a95e8..d0375d6d90 100644 --- a/com.unity.ml-agents/Runtime/Policies/BrainParameters.cs +++ b/com.unity.ml-agents/Runtime/Policies/BrainParameters.cs @@ -78,7 +78,7 @@ public ActionSpec ActionSpec /// the action. /// For the discrete action space: the number of branches in the action space. /// - /// [Obsolete("VectorActionSize has been deprecated, please use ActionSpec instead.")] + [Obsolete("VectorActionSize has been deprecated, please use ActionSpec instead.")] [FormerlySerializedAs("vectorActionSize")] public int[] VectorActionSize = new[] { 1 }; @@ -91,7 +91,7 @@ public ActionSpec ActionSpec /// /// (Deprecated) Defines if the action is discrete or continuous. /// - /// [Obsolete("VectorActionSpaceType has been deprecated, please use ActionSpec instead.")] + [Obsolete("VectorActionSpaceType has been deprecated, please use ActionSpec instead.")] [FormerlySerializedAs("vectorActionSpaceType")] public SpaceType VectorActionSpaceType = SpaceType.Discrete; @@ -102,7 +102,7 @@ public ActionSpec ActionSpec /// /// (Deprecated) The number of actions specified by this Brain. /// - /// [Obsolete("NumActions has been deprecated, please use ActionSpec instead.")] + [Obsolete("NumActions has been deprecated, please use ActionSpec instead.")] public int NumActions { get @@ -117,6 +117,8 @@ public int NumActions /// A new BrainParameter object with the same values as the original. public BrainParameters Clone() { + // Disable deprecation warnings so we can read/write the old fields. +#pragma warning disable CS0618 return new BrainParameters { VectorObservationSize = VectorObservationSize, @@ -126,6 +128,7 @@ public BrainParameters Clone() VectorActionSize = (int[])VectorActionSize.Clone(), VectorActionSpaceType = VectorActionSpaceType, }; +#pragma warning restore CS0618 } /// @@ -133,6 +136,8 @@ public BrainParameters Clone() /// private void UpdateToActionSpec() { + // Disable deprecation warnings so we can read the old fields. +#pragma warning disable CS0618 if (!hasUpgradedBrainParametersWithActionSpec && m_ActionSpec.NumContinuousActions == 0 && m_ActionSpec.BranchSizes == null) @@ -149,6 +154,7 @@ private void UpdateToActionSpec() } } hasUpgradedBrainParametersWithActionSpec = true; +#pragma warning restore CS0618 } /// @@ -156,6 +162,9 @@ private void UpdateToActionSpec() /// private void SyncDeprecatedActionFields() { + // Disable deprecation warnings so we can read the old fields. +#pragma warning disable CS0618 + if (m_ActionSpec.NumContinuousActions == 0) { VectorActionSize = (int[])ActionSpec.BranchSizes.Clone(); @@ -170,6 +179,7 @@ private void SyncDeprecatedActionFields() { VectorActionSize = null; } +#pragma warning restore CS0618 } /// diff --git a/com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs b/com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs index b7b70f1c81..e8be0c36bb 100644 --- a/com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs +++ b/com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs @@ -711,8 +711,16 @@ public void TestHeuristicPolicyStepsSensors() Assert.AreEqual(numSteps, agent1.sensor1.numWriteCalls); Assert.AreEqual(numSteps, agent1.sensor2.numCompressedCalls); + // Disable deprecation warnings so we can read/write the old fields. +#pragma warning disable CS0618 + // Make sure the Heuristic method read the observation and set the action Assert.AreEqual(agent1.collectObservationsCallsForEpisode, agent1.GetAction()[0]); + Assert.AreEqual( + agent1.collectObservationsCallsForEpisode, + agent1.GetStoredActionBuffers().ContinuousActions[0] + ); +#pragma warning restore CS0618 } }