How to use Interface with Behaviour Tree

Hi All!

I’m currently making a behavior tree for a coop game I’ve started. I want my AI to interact with Items in the level. I currently have an Interact Interface for items and an Interact Task for the tree.

I want the Items to control the action and when they’re done have my Interact Task succeed. The part I’m getting hung up on is how do I make the task wait until it’s told by the item it’s done. Currently, I have a simple delay in the Interact task. Which does solve the issue. But it will scale terribly and I’m looking for a more precise way.

Here’s my Interact AI Task

If anyone has any resources or know how to solve this issue. Please throw them my way.

A few ideas, notes:

. If you’re using blueprint interfaces, you don’t need to cast, anywhere

. If you get that yellow pin with BPIs, it probably means you have the wrong version of the interface call:

image

either that, or you’re calling the interface from an actor on itself. Which is pointless.

The whole point of interfaces, is that actor A, can make the call on actor B, without knowing what kind of actor B is. If A knows, in any way, what class of actor B is, you might as well use custom events.

Having said all that… What about this scenario:

. You can have events in your interface definition, ‘interact’, and ‘done’.

. Your AI walks around looking for actors to call ‘interact’ on, using ‘does implement interface’

. When it finds one ( call it B ), it calls ‘interact’ and waits, using a task in the behavior tree.

. After some time, B ( which has an ACTOR reference to the AI because of the interaction ), calls ‘done’ on the AI, and then the behavior tree takes over again.

Does it make sense?

2 Likes

Thanks heaps for the advice.

you might as well use custom events.

Yeah true… I can fix up that one. And you’ve answered my question on the high level. But…

After some time, B ( which has an ACTOR reference to the AI because of the interaction ), calls ‘done’ on the AI, and then the behavior tree takes over again.

How could you get access/finish the task from the Item (the one that implements the interface)? I’ve tried looking but haven’t found anything that works.

Cheers

The both implement the interface. The items and the AI.

So, the item can easily call ‘done’ on any actor.

The AI’s response to the ‘done’ call, is to do something that lets the behavior tree continue. Which is probably something as easy as setting a bool.

Ahh okay this clears the connecting part up.

But this is the part I’m getting stuck on. I could use a bool to handle this. Just seems a lill dirty.

Not at all. The tree needs to know when to switch, when to come out of waiting. A bool is fine for that.

1 Like

I can’t tell for sure, without trying to implement all this, but I think you can just have the custom event that handles the call to ‘done’ in a BTTask. Then you don’t need a bool.

But this is the part I’m getting stuck on. I could use a bool to handle this. Just seems a lill dirty.

I can show you how i do it, maybe that gives you some ideas. I have a similar setup for a sort of management sim. In it, an item broadcasts that it is interact-able, with all necessary information (Animation, potential held items etc.).
A worker can grab that assignment, walk to it and start interacting. When the worker starts working, it notifies the item itself via events that he started working. On a building spot for example, that triggers a timer for the duration of a build time; on a resource spot for collecting resources we wait(aka play an animation) until a full cycle to gather a single resource is done (like chopping wood). All of that “stopping the interrupt” is done on the interact-able item itself.
The assignment is stored on an Actor Component on the worker itself.
This assignment object is also where start and stop the interact and trigger aforementioned events. As i said, stop is mostly implementation detail on wherever the actor interacts, but as starting and interacting is fully data-driven (like i mentioned, animations and held item come from a DataAsset) my Interact BTTask looks like this. Keep in mind that “StartInteracting” will also tell the item about it.

As you see, utilizing the interact assignment i register to the “stop event”, when that is triggered i can also finish the BTTask.

And here is that in action. If you make your interact logic data driven, you can abstract it enough to handle everything via it, as you see below in the video: Building, Sleeping, Eating all is just Interacting.

Nice I like the idea of using delegates.

How do you implement them? Like in your “OnBind” Event.

The event is created and fired in C++, i’m not sure how to do define an event in Blueprint. But it should be easy to find that in the documentation. The stuff you see in my BP screenshot is just basic registering to the event, waiting for the call and then unregistering it once it was fired, to cleanup the memory.