Skip to content

Convert Academy to a singleton #3210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jan 14, 2020
Merged

Convert Academy to a singleton #3210

merged 21 commits into from
Jan 14, 2020

Conversation

chriselion
Copy link
Contributor

This is mostly ready, except for unit test fixes and documentation.

  • Changes Academy from a MonoBehavior to a singleton (using System.Lazy)
  • Adds methods to optionally connect/disconnect from the player loop (need to test in 2019.x, I think the namespace changed)
  • Changed scenes to not have an Academy instance. Renamed the corresponding GameObject to {SceneName}Settings instead.

{
Application.quitting += OnDestroy;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we evert want to deregister from Application.quitting?

@@ -203,9 +284,9 @@ void InitializeEnvironment()
new CommunicatorInitParameters
{
version = k_ApiVersion,
name = gameObject.name,
name = "AcademySingleton",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a name field? Or default this to the current scene?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name used to be given to Python at some point. we removed this (the Python environment is now nameless) so we can delete that field I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was coming from the Unity GameObject (e.g. "3DBallAcademy"). I'll remove it from the protos in a subsequent PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -203,9 +284,9 @@ void InitializeEnvironment()
new CommunicatorInitParameters
{
version = k_ApiVersion,
name = gameObject.name,
name = "AcademySingleton",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name used to be given to Python at some point. we removed this (the Python environment is now nameless) so we can delete that field I think.

var acaGo = new GameObject("TestAcademy");
acaGo.AddComponent<Academy>();
var aca = acaGo.GetComponent<Academy>();
Assert.AreEqual(0, aca.GetStepCount());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these don't make sense anymore, because we never have an uninitialized Academy

@@ -21,10 +22,16 @@

namespace MLAgents
{
public class AcademyStepper : MonoBehaviour
{
void FixedUpdate()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO hide?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why hide ?
Also, maybe rename AcademyDefaultStepper ? Or AcademyFixedUpdateStepper for clarity ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to AcademyFixedUpdateStepper.

I added m_StepperObject.hideFlags = HideFlags.HideInHierarchy in the setup code below, because I think it would be surprising to the user to see a GameObject that they didn't create there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe only hide if the user is not in using a DEBUG flag ? Like what you did for sensor validators?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's really no reason for the user to interact with this object in the editor, or ever. I'm in favor of hiding it over showing it conditionally (which would only ever be in the editor in debug mode).

}

m_Stepper = null;
UnityEngine.Object.Destroy(m_StepperObject);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@surfnerd Is this right, or do I need to Destroy the MonoBehaviour too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the AcademyStepper object destroy itself when the Academy disappears?
You should explicitly call using UnityEngine; on top of file so we know we rely on it in this script.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the AcademyStepper object destroy itself when the Academy disappears?

Yeah, I was missing that. Changing so that the Academy deletes it in Dispose()

You should explicitly call using UnityEngine; on top of file so we know we rely on it in this script

using UnityEngine is already at the top. I think it's necessary here to disambiguate between https://docs.microsoft.com/en-us/dotnet/api/system.object?view=netframework-4.8

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests were complaining about using Destroy instead of DestroyImmediate in Editor mode. I changed things around a bit to use DestroyImmediate from Dispose (but will still use Destroy when disabling normally)

@@ -21,10 +22,16 @@

namespace MLAgents
{
public class AcademyStepper : MonoBehaviour
{
void FixedUpdate()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe only hide if the user is not in using a DEBUG flag ? Like what you did for sensor validators?

}

// Clear out the actions so we're not keeping references to any old objects
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Co-Authored-By: Chris Goy <christopherg@unity3d.com>
@chriselion chriselion merged commit 48e21ad into master Jan 14, 2020
@delete-merged-branch delete-merged-branch bot deleted the develop-academy-singleton branch January 14, 2020 22:42
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants