ml-agents icon indicating copy to clipboard operation
ml-agents copied to clipboard

[development-0.3] Strange behaviour while instantiating agents

Open ferpica opened this issue 8 years ago • 3 comments

EDIT: I discarded my original question, it was fixed in a new commit. Of course, i'm playing with the dev branch, sorry.

Hi! I'm testing the development-0.3 branch because I need to instantiate and destroy agents from script, and I couldn't do it from master.

I noticed a strange behaviour while instantiating agents following the docs, it says we should Instantiate new agents like this:

agentPrefab.GetComponentInChildren<Agent>().GiveBrain(brain);
GameObject newAgent = Instantiate(agentPrefab);
newAgent.GetComponentInChildren<Agent>().AgentReset();

It calls GiveBrain on the Agent component from the prefab (the one in assets resources), and it seems that agent gets registered in the brain, as well as the agent from the instantiated gameobject. I instantiated 4 agents and the brain had 5 agents in its list. It also called CollectState on that agent, which in my case threw an error because my agent needs an external reference after being instantiated. I tried to use GiveBrain on the instantiated agent, but it didn't work. I think it has something to do with the usage of the brain property on the OnEnabled of Agent. I got it working by doing this, but I don't think its very nice:

var agent = agentPrefab.GetComponentInChildren<Agent>();
agent.GiveBrain(Brain);
GameObject newAgent = Instantiate(agentPrefab);
agent.RemoveBrain();
var myAgent = newAgent.GetComponentInChildren<SenteAgent>();
myAgent.AgentReset();

Also, correct me if I'm wrong, but if I destroy the agent's GameObject in OnAgentDone it removes it from the brain, causing it not to send the result and the reward to the Python API, right?

Thank you for your time!

ferpica avatar Jan 26 '18 04:01 ferpica

Hi, thank you for testing this out. This feature is experimental and I am glad you could provide feedback on it. Removing the brain from the prefab is the right way to fix it. We hope to provide a simpler way to instantiate agents. Unfortunately as of now, destroying the gameObject will prevent it from sending its reward and state. Another quick fix would be to use Destroy and passing a float argument to delay the destruction.

vincentpierre avatar Jan 30 '18 19:01 vincentpierre

Hi @vincentpierre. I didn't know Destroy had a delay argument, nice! I wrote a little ugly workaround and it works, but the delay is a much better solution. Instantiating and destroying agents works perfectly. Thank you very much! Your project is awesome

ferpica avatar Jan 30 '18 19:01 ferpica

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 03 '20 17:01 lock[bot]