Search Unity

Can you change an Agent's public field at runtime through Python API?

Discussion in 'ML-Agents' started by Chrispresso, Sep 24, 2020.

  1. Chrispresso

    Chrispresso

    Joined:
    Nov 2, 2019
    Posts:
    5
    Let's say I have an Agent like so:
    Code (CSharp):
    1. public class MyAgent : Agent {
    2.     public int numEnemies;
    3.     public int numGoals;
    4.     ...
    5. }
    6.  
    Right now what I can do is change the value of numEnemies and numGoals through the Unity interface, build the project and re-run with my Python code. This works, but seems terribly inefficient.

    Is there a way to control those two variable through the Python API at runtime so I can just do something like:
    env = UnityEnvironment(filename="myApp.exe")
    env.numEnemies = 10
    env.numGoals = 5
    env.reset()

    Taking a look at custom side channels, it seems like that might be what I want, but I'm not entirely sure.
     
  2. ruoping_unity

    ruoping_unity

    Unity Technologies

    Joined:
    Jul 10, 2020
    Posts:
    134
    There is way to configure environment parameters in Python during training, please see this doc.
    Does that work for your case?
     
    Chrispresso likes this.
  3. Chrispresso

    Chrispresso

    Joined:
    Nov 2, 2019
    Posts:
    5
    I thought things like
    Academy.Instance.EnvironmentParameters.GetWithDefault("my_environment_parameter", 0.0f);

    only worked if I set the filename=None and ran it within the Unity environment. I would like to run this with a built binary and not through the Unity interface if possible.
     
  4. Chrispresso

    Chrispresso

    Joined:
    Nov 2, 2019
    Posts:
    5
    Okay, so I misunderstood the documentation the first time. You CAN in fact set and reference through prebuilt binaries using GetWithDefault.