Hi @AntLewis
Thank you for your interest!
This is an area we are aware is missing documentation at the moment. We currently working with the documentation team to fill that gap.
In the meantime here are 2 different approaches you could use to achieve communication between Behavior Graphs and C# systems/components.
C# binding to a particular instance of BehaviorGraphAgent
You can get a reference to a BlackboardVariable from a BehaviorGraphAgent
using the BehaviorGraphReference
API.
You can then subscribed to the BlackboardVariable.OnValueChanged
event or in the case of an EventChannel, subscribe to BlackboardVariable.Value.Event
event.
Sample code
[SerializeField] private BehaviorGraphAgent m_Agent;
// Agent state has changed to [value] (where value is of type StateExample)
private BlackboardVariable<StateEventChannel> m_stateEventChannelBBV;
private BlackboardVariable<StateExample> m_stateBBV;
private void OnEnable()
{
if (m_Agent.BlackboardReference.GetVariable("StateEventChannel", out m_stateEventChannelBBV))
m_stateEventChannelBBV.Value.Event += OnStateEvent;
if (m_Agent.BlackboardReference.GetVariable("StateToReact", out m_stateBBV))
m_stateBBV.OnValueChanged += OnStateValueChanged;
}
private void OnDisable()
{
if (m_stateEventChannelBBV != null)
m_stateEventChannelBBV.OnValueChanged -= OnStateChanged;
if (m_stateBBV != null)
m_stateBBV.OnValueChanged -= OnStateValueChanged;
}
private void Update()
{
// your custom logic
// Send event to the event channel of the referenced agent.
// Only this instance of agent will receive it (except if the BlackboardVariable is 'Shared').
m_stateEventChannelBBV.Value.SendEventMessage(StateExample.Alert);
}
private void OnStateEvent(StateExample value)
{
// React to event
}
private void OnStateValueChanged()
{
// React to state change
}
C# binding using Event Channel asset
It is also possible to generate an instance of an Event Channel that can be use to received or emit message from. This is useful in case you want a global event channel that should be the same accross several graphs. You can then reference this asset in C# systems or component to react or send event message.
To do so, once you have created a new Event Channel from a Blackboard wizard:
- Go to the project view
- Right click > Create > Behavior > Event Channel >
%Name_of_Your_Event_Channel%
- This will create an instance of that Event Channel that can be assigned to Graph but also C# components.
- In C#, use the Event Channel API to listen or send new message.
Sample code
[SerializeField] private StateEventChannel m_EventChannel;
private void OnEnable()
{
m_EventChannel.Event += OnStateEvent;
}
private void OnDisable()
{
m_EventChannel.Event -= OnStateEvent;
}
private void Update()
{
// your custom logic
// Send event to the event channel instance.
// All graphs and C# systems on that same event channel instance will receive it.
m_EventChannel.SendEventMessage(StateExample.Alert);
}
private void OnStateEvent(StateExample value)
{
// React to event
}
I hope this will answer your question, and don’t hesitate to ask if you have any others