Looking for AI & Behaviour Tree C++ Documentation or Sample Code!!! Lack of NON Blueprint Docs!!!

Guys, I’ve been scrounging for days now trying to find Information on programming AI into C++. I heard over and over @MieszkoZ and others saying “If you desire performance, do your AI in C++”. But you don’t teach us how, or have up to date Documentation on it!!!

Epic are you serious?

For Tasks i finally found this. https://wiki.unrealengine.com/Survival_Sample_Game:_Section_3 Okay i can handle it for now.

But there is NOTHING regarding services in C++!!! I found Exi post asking around. https://answers.unrealengine.com/questions/200312/behavior-tree-service-in-c.html

However, the method that he overrides doens’t EXIST!!! “virtual void ReceiveTick(AActor* OwnerActor, float DeltaSeconds) override;”

Excuse my anger and frustration but, i agree that we receive to little atention. And i think it is a little to much force us to read the entire engine code when we need something quickly made.


I wrote this a long time ago, so some of it may be outdated, but most of if should be the same.


your post was one of the first that i read before trying to figure it all out. But you just used tasks. And it changed a bit. The part that ****** me off is that they have time to spend making tutorial after tutorial of Blueprint ready gameplay, but almost nothing is made to C++.

I’m coding here in the dark, reading superficial commented code.


You don’t have to use the Behavior Tree. The Strategy Game example has C++ AI that is not using the Behavior Tree. Or it used to, I haven’t checked it in more than a year.

My cat game no longer uses Behavior Tree’s, I made my own system in C++. But, in another project that has many types of different NPC’s I do use Behavior Tree’s.

My advice to you is, if you are going to use Behavior Tree’s, do them in Blueprints. You can make your individual tasks or services in C++ if you need to optimize it, but I’d prototype it first in Blueprints only, then when something proved to be a performance issue, then optimize it. From the Editor when making a new C++ class you can choose the Service or Task as it’s parent ( forgetting their exact names right now ).

I have some blog posts on UE4 gameplay C++ here. http://www.-.com but, I’m so busy with my job and 2 Indie games that I make that I have not been updating it much with new posts. There is one on there about a Utility System in C++, but it’s not complete.

Time and time again, i see it on forums and videos that clearly state that blueprint are extremely slow when handling ticks. A Service Task in a BT, depending on its frequency, runs almost as a tick. So i wanted to create it as a C++ code from the get-go.

I got something working here, i will try to create a post talking about it to help developers out. It grinds my gears that this could have been done a long time ago with little effort but the main focus is blueprints.

Thanks for the link i’ll take a look. I enjoy Blueprint as much as the other guy, but i’m trying to get as much performance as i can get, can you blame me?

To clarify, if the node only calls C++ code and all you’re doing is chaining up C++ nodes in Blueprint, then it’s not much slower than pure C++ at all, at least not enough that it’s worth the workarounds of having to hardcode everything in C++.

There’s an experimental feature in engine at the moment that compiles blueprints down to C++ when it exports, I believe eliminating the VM layer. You could try that but to be honest I wouldn’t expect much if the nodes are already mostly C++.

So why in every AI Stream they keep reminding that fact to everyone that is watching? Pure terrorism? If so, could please tell them to stop? Mieszko and Ian Shadden keep making pun and remarks on that note.

Come again?

What I’m saying is that Blueprints that just call a bunch of C++ nodes should perform as well as /only ever-so-slightly slower than full-on C++. The advantage of having superb flexibility with the Behavior Tree editor etc. is more than worth the minute amount of cycles you’ll save, and if you’re getting down to that level of optimization then the blueprint VM costs are frankly the least of your concerns.

I haven’t seen all the streams, but the point is all of the nodes they write are done in Blueprint AFAIK, which can be a crux if you want to do lots of things every frame for many objects. Doing heavy math operations often in Blueprint is ill-advised, C++ general does that stuff a lot faster.

If i were making game for pc or consoles i would be less worried like you said. But the games that i develop is for Mobile, primarily IOS. So i’m trying to make the best out of it. Regarding the streams, i’ve never benchmarked Blueprints and C++ so i take their word for it. But maybe i should do something like that.

Basically it’s the code in tick that you want either move out of there and not do it every tick, or put it in C++.

There are a lot of posts on the forums right now from people thinking they need to abandon Blueprints and do everything in C++. Think of Blueprints like Java, it compiles to a byte code that runs on a virtual machine in the game engine, instead of being directly compiled to the users machine. But, if I understand it right that isn’t going to be the case completely as there’s an experimental feature to compile Blueprints to C++.

Actually run performance tests and see what is slowing your game down. For mine, textures size seem to be the biggest issue which has nothing to do with Blueprints. They have tons of tools built in to do that, looks through the docs on it. Start with View-Stat-Engine-Unit and View-FPS from the Editor window if you don’t know where to begin. Tick is in the game stat. Also, just unplug the wire coming out of the tick in the Blueprint and see if it makes a difference, it likely won’t and so anytime spent “optimizing” that Blueprint tick code into C++ will not give a return in performance, though you may be happier about doing it for other reasons such as maintainability. It’s a lot easier to read C++ than Blueprints, conversely it seems it’s faster to write Blueprints so they are great for prototyping.

For Behavior Trees, use the Blueprint editor for them. If there are Tasks or Services that have computation in them past a simple lookup and compare then you can make that specific task or service in C++, but still assign in in the Blueprint Editor.

Aren’t they going to convert blueprints to native C++ code anyway ? I saw something for 4.11/4.12 … so when it gets converted anyway to C++, why bother ? I use C++ mainly when doing things in the engine that i cannot access through blueprints.

Thats exactly what TheJamsh is saying

“There’s an experimental feature in engine at the moment that compiles blueprints down to C++ when it exports, I believe eliminating the VM layer. You could try that but to be honest I wouldn’t expect much if the nodes are already mostly C++.”

So if you created everything in Blueprints this experimental feature for 4.11 compiles Blueprints down to Native C++ code. It’s meant to give similar performance to coding in C++

So maybe true the Blueprint route and use the feature and check performance, it would hardly be a waste of time since you will be getting some experience with the Behavior Tree anyways.

Might be a old post but since it’s the first result popping on google for running a behaviour tree in C++ I will share what I did. In a class file that inherit ACharacter ( could also be APawn I guess), in the BeginPlay() overridden function you can do something like this.

void AAIMinionActor::BeginPlay()

    AAIController * AIC = UAIBlueprintHelperLibrary::GetAIController(this);

    AIC->RunBehaviorTree(this->BehaviorTree); // Obviously this will crash if BehaviorTree is nullptr


“this->BehaviorTree” is a UPROPERTY you have to set in your AI Blueprint. You have to set “Auto Possess AI” to “Spawned”.

Useful doc :

Cheers :slight_smile: