Networking
Run decisions on the authority
The recommended approach for Behavior Designer and networking is to have Behavior Designer run on the server. This will ensure the server makes all of the decisions and then sends the clients the results. For example, if the Seek task is running the client should only receive the current position of the agent. With this setup the behavior tree isn’t even aware of the network. This is also true for a client authoritative solution. Only the resulting transform/animation properties should be synced to all other clients. The actual behavior tree does not need to be synchronized across the network. For a practical example your script would look something like this:
using Opsive.BehaviorDesigner.Runtime;
using UnityEngine;
public class BehaviorTreeAuthority : MonoBehaviour
{
[SerializeField] private BehaviorTree m_BehaviorTree;
public void SetAuthority(bool hasAuthority)
{
if (m_BehaviorTree == null) { return; }
if (hasAuthority) {
m_BehaviorTree.StartBehavior();
} else {
m_BehaviorTree.StopBehavior();
}
}
}
Start deferred Entity trees on the server
Turn off Start When Enabled on the graph component. Call SetAuthority from the networking layer after it establishes or changes ownership, passing whether this instance is the server or authoritative client. Only that owner starts the graph; losing ownership stops it. The networking layer remains responsible for synchronizing the resulting movement and animation.
For Entity clients, leave Start When Enabled off. Only the server or authoritative client should request a deferred start. Baked initialization runs after instantiation and adds DeferredBakedBehaviorTreeStart; a start call made before that pass returns false.
In your spawner’s OnUpdate(ref SystemState state), tag each authoritative entity after its per-agent setup is complete. Here spawner.Prefab, spawner.SpawnCount, and spawner.IsServer come from your project’s spawn data:
using (var entities = state.EntityManager.Instantiate(
spawner.Prefab, spawner.SpawnCount, Unity.Collections.Allocator.Temp)) {
for (int i = 0; i < entities.Length; ++i) {
var entity = entities[i];
// Set this entity's position, team, and other per-agent data here.
if (spawner.IsServer) {
state.EntityManager.AddComponent<PendingBehaviorTreeStart>(entity);
}
}
}
Add this marker and system to your project. It waits for baked initialization and retains unsuccessful requests for a later update. Client entities receive no pending marker, so this system never starts them:
using Opsive.BehaviorDesigner.Runtime;
using Opsive.BehaviorDesigner.Runtime.Components;
using Unity.Collections;
using Unity.Entities;
public struct PendingBehaviorTreeStart : IComponentData { }
public partial class StartAuthoritativeBehaviorTrees : SystemBase
{
private EntityQuery m_ReadyTrees;
protected override void OnCreate()
{
m_ReadyTrees = GetEntityQuery(
ComponentType.ReadOnly<PendingBehaviorTreeStart>(),
ComponentType.ReadOnly<DeferredBakedBehaviorTreeStart>());
}
protected override void OnUpdate()
{
using (var entities = m_ReadyTrees.ToEntityArray(Allocator.Temp)) {
for (int i = 0; i < entities.Length; ++i) {
var entity = entities[i];
if (BehaviorTree.StartBakedBehaviorTree(World, entity)) {
EntityManager.RemoveComponent<PendingBehaviorTreeStart>(entity);
}
}
}
}
}
Keep the startup system enabled while requests remain. Remove a pending marker if gameplay cancels the request or the agent loses authority before starting. A request that remains pending after initialization indicates an invalid start branch and should be diagnosed, not treated as a successful start.