Transferring over from BP to C++ (AI)

Hi,

I have a relatively general question about how to transfer over AI in Blueprints to AI in C++. I have been messing around with these awesome tutorials found HERE and I have gotten to the part where I have the four main parts of AI set up and working. But I was wondering, if I wanted to turn this into C++ code, how would I go about doing that?

I right now have AI that can: use the Behavior Tree, the Black Board Component, can move to a TargetPoint, can sense the player, can attack the player, pretty much the whole nine. But that is in Blueprints, and while I know it’s powerful, I would like to to things in C++ instead.

How would I go about doing this? How could I set up the AI? How could I have it use the Behavior Tree/Blackboard? How can I use Services to make the AI properly follow along the Nodes in the Behavior Tree? How can I get it to move to a Target Point, or Sense the player, or Attack the player? Is this even possible through C++, or is this even recommended?

Basically, I would like some basics on getting this same kind of AI, that I created in Blueprints, for C++. But I don’t really know where to begin (the Engine looks like a beast, and to be honest, I’m a little intimidated by it). Mind you I am using 4.6, I’ve heard there were some changes between pre 4.6 and after in the C++ realm. I’ve Googled and found A tutorial on this…The only problem is that none of the code seems to work for me (could it have been created on an older version of UE4?). Either way, I’m pretty much lost, I don’t see much documentation for anyone interested in creating AI in the C++ side, and without knowing any basics of how everything works together on the C++ side, you can imagine how discouraging it could get.

Could anyone help me figure this out?

Thanks.

Can’t really help you too much,but you might try this tutorial Unreal Engine 4 C++ Tutorial Version 4.0.2: Basic Artificial Intelligence - YouTube if you haven’t already . I’ve just been making my own “AI” via C++ but it’s pretty basic.

lol. That was the tutorial that I mentioned (the one where the code didn’t work and left me in this confused state).

On a good note, I decided to snoop around, and I have to thank the devs at Epic. I made/opened up the C++ Flying project, and it helps a TON when they included their explanations on what’s going on when I built the project. I’m right now in a bit of a quandary of what to do.

I am looking at documentation now for the UBrainComponent, I see it has the UBlackboardComponent. What’s the difference between the two? Could I use UBrainComponent instead of UBlackboardComponent to store the “memory” of the AI?

I want to create (basically Drone Flying AI atm, but it could help in Enemy AI as well). AI that basically goes from TargetPoint to TargetPoint. I’m thinking UPathFollowingComponent for this…But I don’t EXACTLY know how it works (does it allow Beizier movements from TP to TP? How could one implement that once the AI reaches a TP?)…

Still a ton of questions, but at least I THINK I’m getting somewhere with this (…I think)…

My personal opinion is that if you should not try to do your AI fully in C++, especially if it’s based on blueprints. Instead, you should try to know exactly what needs to be computed in C++, implement it in C++, and then expose it to the blueprint system.

For example, let’s suppose you want to have a very complex function computeNextGoal that takes in input an array (for example, all the features of your AI character : its location, health, ammo, etc…) does some heavy computation that needs C++ (for example, you need to compute a matrix inversion, it would be stupidly hard and inefficient to do it in blueprint. In C++, you just use a library). So, you create a static function like described here, and inside your decorator/service/task, you call this function.

Now, why did I recommend NOT to do behavior trees in C++? Because you are going to lose the single greatest tool that UE4 Editor offers you : the visualization of your BT. Not only will your BT be much clearer to visualize, you will also be able to debug it live (with the yellow arrows)

If you see a UBrainComponent, it’s in fact a UBehaviorTreeComponent. UBrainComponent is the parent of all the “AI components”, but in practice, it’s an abstract class with only one child : UBehaviorTreeComponent.

Hmmm…But what if you want to try to base your AI in C++? Would the better option still be to use C++ to set up BP? I’m asking this because I am a programmer (obviously) and I am coming from Unity (where if you want anything done in it, you’d have to program…A lot) and I’m trying to figure out where to draw the line on when/how C++ programming is even needed for the AI.

Makes sense. I guess I was a little bit confused on it. Thanks for the info!

You can always add a new C++ class extending the appropriated BehaviorTree component that you need later if you need a function to be done in C++. Then rebase the Blueprint to extend from your new C++ class which only adds the functions you need. You don’t need to start with it.

Having the visual editor for BehaviorTrees is an swesome tool. In UE3 I did BehaviorTree’s and imported an xml file which decribed the tree. It’s much easier in a visual editor, especially one that gives you the debugging info where you can see the execution flow in real time instead of just stepping through it in a text editor.

Agreed with this comment. Because that’s what I actually do. I have a planner (Hierarchichal Task Network) done in C++. But I still use Behavior Trees. I just call my C++ functions at the relevant places.

I finished it, if you ever want to try that tutorial again, I can help you with it. I was stumped like 3 times on that thing because of the new engine build, but figure everything out :slight_smile:

Hmmm…I guess I could try to give it another shot. Would it be okay to PM you with any questions? (don’t know how long this question would be up now that i’ve selected an answer)

Hey, if you figure out how to make it work and don’t mind giving a short explanation, don’t hesitate to put it somewhere , like the unreal forums. Just explain where you got stumped, that would be super useful!

The tutorial thing.

sure! Though how do you do that?

Are you talking about that tutorial or the op question?

Hey, I have another question. Let’s say I wanted to have a base C++ script for AI (not to use whole-heartedly, but something I could use for future implementation of any specialized AI code). It’s based off the AIController class, but for some reason, when I try to create a BP from the script, it doesn’t show up.

Why am I not seeing this code? Am I doing something wrong here?

Scratch that, figured it out.

Ok will do

I would love to see something like “set your AI complete in C++” too but the documentation is not enough here.