How do I get the owner actor of a BTService in C++

I’m trying to follow along with the AI behaviour tutorial except create it all in C++.

https://docs.unrealengine.com/latest/INT/Engine/AI/BehaviorTrees/QuickStart/11/index.html

I’m trying to create the AggroCheck BTService, but I’m having a really difficult time finding any documentation on how to work with this in C++.

From what I can tell, instead of the ReceiveTick event, I need to override the TickNode function. The problem is that TickNode does not have a parameter for the OwnerActor and I’m at a loss as to how to retrieve it.

I’ve dug around the BTService Blueprint code a bit and it seems that the owner is set by a call to SetOwner, and then that value is pass to ReceiveTick when calling TickNode. Does this mean I need to override the SetOwner function and store a reference in my own class too and then use that?

By default C++ BT nodes are non-instanced (meaning they’re shared across all AI agents using this one given BT asset) and as such don’t have an “owner”. To get your hands on the AIController given TickNode call (and others) is regarding you need to extract it from the BT component, like so:

AAIController* MyController = OwnerComp.GetAIOwner();

Simple as that. If you want to work with BT nodes I strongly suggest studying the way other native C++ nodes are implemented. The BP BT nodes are more of an exception that a rule, and do some unorthodox things to make BT nodes creation in BP possible/easier.

Cheers,

–mieszko

Thanks for the response. That looks like what I’ll need so I’ll give it a try.

I would love to check out some other C++ nodes (digging through existing code is my preferred method of learning new systems), but as far as I can tell, the only classes that inherit from BTService is UBTService_BlueprintBase and UBTService_BlackboardBase.

Do you have some suggestions on existing code that I could look at?

There’s UBTService_DefaultFocus and UBTService_RunEQS (not sure when I’ve added those, just take latest :wink: ), but in general I suggest looking at all the BT nodes in AIModule.

Perfect. Thanks. :slight_smile: