# 4. C Sharp Bindings and Event Channels Part of [[Unity Behavior]]. Prev: [[3. Authoring Graphs and Custom Nodes]]. Next: [[5. Worked Example Guard Graph]]. Official 1.0.17 manual: [Interact with behavior graphs via C# scripts](https://docs.unity3d.com/Packages/[email protected]/manual/bind-c.html). Two supported techniques: bind to a particular `BehaviorGraphAgent`, or bind through an **Event Channel** asset. Scripts below follow those official samples and were **not compiled**. ## 4.1 Bind to one agent Official: use `GetVariable` to obtain a `BlackboardVariable<T>`, then subscribe to `OnValueChanged`, or, for an event channel variable, to `BlackboardVariable.Value.Event`. Unsubscribe in `OnDisable`. The official sample uses types `StateExample` and `StateEventChannel` as stand-ins for *your* blackboard types. Replace them with the types your graph actually declares. ```csharp using Unity.Behavior; using UnityEngine; /// <summary> /// Official bind-to-agent pattern (1.0.17 bind-c manual). /// NOT compiled in Unity. Replace StateExample / StateEventChannel with your types. /// </summary> public sealed class GuardBrainBinding : MonoBehaviour { [SerializeField] BehaviorGraphAgent agent; BlackboardVariable<StateEventChannel> stateEventChannelBBV; BlackboardVariable<StateExample> stateBBV; void OnEnable() { if (agent.GetVariable("StateEventChannel", out stateEventChannelBBV)) stateEventChannelBBV.Value.Event += OnStateEvent; if (agent.GetVariable("StateToReact", out stateBBV)) stateBBV.OnValueChanged += OnStateValueChanged; } void OnDisable() { if (stateEventChannelBBV != null) stateEventChannelBBV.Value.Event -= OnStateEvent; if (stateBBV != null) stateBBV.OnValueChanged -= OnStateValueChanged; } public void AlertThisAgent() { // Official: only this agent receives it, unless the BlackboardVariable is Shared. stateEventChannelBBV.Value.SendEventMessage(StateExample.Alert); } void OnStateEvent(StateExample value) { } void OnStateValueChanged() { } } ``` `StateExample` / `StateEventChannel` are **placeholders from the official sample**, not types this course invented as a package API. Create the event channel and enum (or whatever types you used on the blackboard) in your project, then match the variable **names** (`"StateEventChannel"`, `"StateToReact"`) to the graph. Pushing facts *into* the graph from sensing code is `SetVariableValue` (part 2): ```csharp agent.SetVariableValue("CanSeePlayer", canSee); agent.SetVariableValue("Target", targetTransform); ``` ## 4.2 Event Channel assets (official) Official steps: 1. Project window: **Create > Behavior > Event Channel > [Channel Name]** (the menu lists channel types you have). This creates an **instance** you can assign to graphs and to C# fields. 2. In C#, listen or send on that instance. ```csharp using UnityEngine; /// <summary> /// Official event-channel asset pattern (1.0.17 bind-c manual). /// NOT compiled in Unity. /// </summary> public sealed class GlobalAlertRelay : MonoBehaviour { [SerializeField] StateEventChannel eventChannel; void OnEnable() => eventChannel.Event += OnStateEvent; void OnDisable() => eventChannel.Event -= OnStateEvent; public void AlertEveryoneOnThisChannel() { // Official: all graphs and C# systems on this same instance receive it. eventChannel.SendEventMessage(StateExample.Alert); } void OnStateEvent(StateExample value) { } } ``` Use an **asset** channel when several agents (or a manager and a graph) must hear the same message. Use the **agent-bound** channel variable when only that instance should hear it (official distinction, including the Shared blackboard caveat). ## 4.3 Exercise Create an event channel from the official menu, assign it to a graph and to a C# relay, fire `SendEventMessage` from a key press, and confirm the graph reacts in Play mode (the package's Play-mode visualization helps). Then fire the agent-bound path on a second agent and confirm the first does *not* react. ## Sources - https://docs.unity3d.com/Packages/[email protected]/manual/bind-c.html