Populate followers in Leader Follow dynamically?

thretch

Member
Why is the agent list static? I need to populate it with a List of GameObjects at run-time. How is this accomplished?

1570828185047.png
 

Justin

Administrator
Staff member
The agents field is a SharedGameObject[] so you can assign the value at runtime using the shared variable system.
 

thretch

Member
I'v gotten this far:

behaviorTree = GameObject.Find("Agent " + squadNumber + "-" + i).GetComponent<BehaviorTree>();
var followerListVar = (SharedGameObjectList) behaviorTree.GetVariable("followers");
followerListVar.Value = followers;


But that only populates a shared variable. In the Leader Follow task, there's no button allowing you to assign a shared variable to the Agent List, like there is on every other parameter.
 
Last edited:

thretch

Member
Went through literally every page of the BD docs without answers. Also spent nearly an hour googling dozens of variations trying to find the solution. Nada.
 

Justin

Administrator
Staff member
You can find a specific task with the BehaviorTree.FindTask method. The agents array is a SharedGameObject[] so you can swap out the individual elements within the array using the shared variable system, but you're right in that this doesn't help much with switching out the entire array.

If you change SharedGameObject[] to SharedGameObjectList that should make it easier to switch out the entire array. I am hesitant to make this change within the Asset Store version because it'll clear out the agents for everyone. I will make it when we release version 2 of BD since that will be a major upgrade anyway.

When you make the change agents.Length becomes agents.Count, and agents.Value becomes agents.Value.
 

thretch

Member
For those of you who, like me, love to see some actual code:

// declarations
private SharedGameObject[] followers;
private BehaviorTree leaderBehaviorTree;

// before the loop
leaderBehaviorTree = thisAgent.GetComponent<BehaviorTree>(); // do this on the leader only

// in the loop
followers = thisAgent;

// after the loop
LeaderFollow leaderFollowTask = (LeaderFollow)leaderBehaviorTree.FindTaskWithName("Leader Follow");
leaderFollowTask.agents = followers;


Thanks for the assistance, Justin!!
 

Cyberfox

New member
Greetings,
I did not find this as useful as it might seem, as modifying the agents field does nothing for the transforms and other related fields, which causes problems throughout the code if updating the list dynamically.

My solution was to subclass and extend the DoUpdate method to look for GameObjects tagged with a given tag. My Behavior Tree that spawns enemies tags them appropriately, and they automatically are recognized and added to the Leader Follow list, and get movement behavior appropriately. You add the Task as Tagged Leader Follow, instead of just Leader Follow.

You can find my code here:


That's for the A*Pathfinding version. I believe a similar version could be made for the stock Unity NavMeshAgents.

-- Morgan
 
Last edited:
Top