Event System Examples

pako

New member
Hi,

I've been quite frustrated about not finding enough documentation and examples on the event system. I am an experienced programmer and I can code system events with no problem. However, to be quite honest, the relative documentation on the Behavior Designer event system is quite inadequate. I've been reading through your code, re-reading the documentation over and over, searching the examples for hours, and cannot yet send an event in code that gets received by the HasReceivedEvent task in a BehaviorTree. I mean, if you had implemented some send/receive events, both through code and tasks there would be no problem, as I would have found them and solved the problem.

I'm using the Patrol task that is included in the Movement pack, on a very simple behavior tree, which has an enemy patrol between 2 waypoints. All I want to achieve is for the BT on the enemy to stop, when the Player gets killed so that the enemy stops moving.

Inside an "Enemy" MonoBehaviour I have a Player_Killed(object sender, EventArgs eventArgs) event handler, inside which, I want to signal the BT to stop.

Per your documentation, the way to send an event through code is:

var behaviorTree = GetComponent<BehaviorTree>();
behaviorTree.SendEvent<object>("MyEvent", Vector3.zero);

However, there's no explanation about what "object" or "MyEvent" represent, and if only a single parameter is necessary. It doesn't also describe how the corresponding HasReceivedEvent task in the BT should be configured to receive the "MyEvent". Does "MyEvent" represent the name of a method somewhere in code, used similarly to a Unity message? Or is it just a string that must correspond to the same string set in the HasReceivedEvent task. In any case, I've tried both, but it doesn't work.

Please excuse my frustration, but I've spend hours on something that should be solved in minutes, and I do believe that your documentation and examples are quite inadequate and don't reflect the actual quality of this asset.
 

Justin

Administrator
Staff member
I can look at improving that aspect of the documentation. Thanks for the feedback.

However, there's no explanation about what "object" or "MyEvent" represent, and if only a single parameter is necessary.
object is the object type. Has Received Event subscribes to the object type of the event so it should always use object.

"MyEvent" is the name of the event. For that scenario your Has Received Event task would look like:

1605125303349.png

Make sure you are reevaluating the task with conditional aborts otherwise the event won't be received. Has Received Event takes up to three parameters, though it would be easy to create a similar task that can take more.
 

pako

New member
Thank you for the reply,

I figured it out and got it to work by trial and error, yesterday after posting.

What I find confusing in the documentation is that I was under the impression that Has Received Event is equivalent to behaviorTree.RegisterEvent<object>, which of course is not. Now I see that Has Received Event evaluates to either true or false (becomes true if the SendEvent triggers it), while RegisterEvent registers an event handler to run when the SendEvent triggers it. Quite different indeed!

Also, I wasn't sure if I had to replace "object" in behaviorTree.SendEvent<object> with a specific object type. Thanks for the clarification!

I believe that an example scene demonstrating the BT event system using tasks as well as code would be very helpful.
 

Tzirrit

New member
Just want to add that I was also a bit confused by how the event sending and recieving worked. Just take the default HasReceivedEvent Conditional provided by BD as rough example with very little applicability, apart from registering that an event has just happened.

The fun started once I created multiple custom Conditionals, each for a specific Event payload (object type). Now I have custom event handlers for all key event payloads; I read out all the relevant payload properties and write them to shared variables. Very powerful and very fun ;)

So the whole event handling went from frustrating to "omg this is awesome". Adding a short example to the documentation and suggesting to write custom event handlers would probably help reduce confustiuon and frustration of future users.
 
Top