Move AI Character using C++ instead of blueprint

hi all. i have a blueprint which moves my a single character from one point to another

How would i implement this in the character class (C++) so i can spawn a number of characters and it will move each one of them??

You create a behavior tree which has a “move” behavior. Then, you can assign that behavior tree to multiple character types and they’ll all use instance of the behavior tree.

If only implement in c++ code, there is a way:
First you need an AIController or some class inherit from AIController. Then make sure that your AICharacter is using AIController as its default Controller class and it has a CharacterMovementComponent in it. And finally, use the MoveToLocation(or MoveToActor and so on) which is a function of AIController to move the AICharacter it own.
Oh,and don’t forget add a NavMeshVolume in you scene to generate navigation information since MoveTo functions are using navigation information to determine which way to go.

1 Like

I know this is old, but I stuck on same problem and can’t seem to figure it out by myself.

I have NPC_Blueprint inherited from c++ class AMyCharacter : ACharacter
Here are some BP settings:

I have already tried moving it with blueprints, and it looks like this:

It works almost perfectly (exept I don’t have walking/running animation lol, but that’s not the point)
Here I get NPC reference, which I store in game mode class and move desired character to specified location.

Now I want the same in c++. The following code is function I write in blueprint static library I called UDayFlow, that executes in game mode class of my game.
What I don’t understand is how do I retrieve** AIcontrolle**r from ACharacter
From variety of ACharacter methods there are no AIController, just simple Controller, some getters to Controller and AIControllerClass variable.
I was trying to create new AAIController variable, but this line just gives me compilation errors.
Was trying to Cast to AAIController, but yet again - no luck.

In this function I’m trying to move referenced NPC to location that defined by FVector inside FEventCharMoves structure.

Please help!

Make sure you include “AIController.h” at the top of your file, not sure about earlier version but in 4.15 that’s definitely needed to use AAIController class.

But once you’ve done that, the code you have should work

simtbi, thank you for you attention to my question!
I had all the needed includes and that’s why I was so frustrated. But! At last I found out what was missing - it was the dependency in MyGame.Build.cs file.


PublicDependencyModuleNames.AddRange(new string] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "AIModule" });

it was this line, where I had to add AIModule!
I guess I have to dig deeper into this, and mind dependencies next time something wouldn’t compile.

So now, my code looks like:


	
        // spawns default controller (if not, there is no controller for NPC to control)
	NPC->SpawnDefaultController();

	// cast controller recieved in NCP in Bar Class, derived from AAIController class
	ABarNPCAIController * moveController = Cast<ABarNPCAIController>(NPC->GetController());

	// moves character to location defined by TargetLocation component of moveEvent	
	moveController->MoveToLocation(*moveEvent.targetLocation);	

still need some tuning, but it works!
As you, or anybody who have same problem might notice, I had to create my own AIController class (I called it ABarNPCAIController), which striclty inherits from AAIController, because engine demans my main Game.h file to be included in all of .cpp’s and if I just left default AAIController, I would have to include it in AIController.cpp.

Ahh yes, that was going to be my next suggestion :smiley: Glad you got it sorted anyway

Hmm, that doesn’t sound right, you should be able to use AAIController directly just fine (assuming that does everything you need).

I have the following code in one my game classes and it works no problem. Are you getting a specific error if you use AAIController directly?



UNavigationPath* path = UNavigationSystem::FindPathToLocationSynchronously(this, a->GetActorLocation(), dest, a);

if (path && path->IsValid())
{
	FAIMoveRequest req;
	req.SetAcceptanceRadius(50);
	req.SetUsePathfinding(true);

	AAIController* ai = Cast<AAIController>(a->GetController());
	if (ai)
	{
		ai->RequestMove(req, path->GetPath());
	}
}



Oddly enough, I switched my custom AIController class to one from the box and It worked as intended. Before that I had some errors of Main header file not included in AIController.cpp and that’s why I decided to create custom AI Class.
Thank you for pointing that out!

Thank you!