Stanford UE4 + C++ Course Learning Record 24: AI Behavior Tree - Getting Started Pathfinding

Table of contents

1. Create hostile minions

2. Pathfinding behavior tree


        At the beginning of the course, the author first briefly explained some of the basic concepts about behavior trees. Since I have also been exposed to learning behavior trees in the process of researching reinforcement learning, I will not repeat the relevant concepts. Friends who are not familiar with behavior trees can search for some popular science articles by themselves, and have a preliminary understanding of the operation process of behavior trees with examples.

        Before entering the course, you can refer to the author's settings and turn off "Automatically compile new C++ classes" in UE's "Editor Preferences". If so, the following interface will pop up when we add a new class to prevent UE from automatically hot reloading every time.

Figure 24-1 Turn off automatic compilation

1. Create hostile minions

        Before implementing the enemy AI, first create a character for the enemy creeps. The detailed creation operation is the same as the creation of characters in the first section 3, and you can also directly refer to the beginning of the course P40 . The brief process is to create two classes, SurAICharacter and SurAIController (the former implements the character, the latter implements the behavior tree), then creates the blueprint class in UE, and sets the Minion_Lane_Ranged_Dawn mesh and MinionRanged_AnimBP animation for the SurAICharacter blueprint class, and finally drags it into in the world scene.

        There are two points to pay attention to: one is to facilitate project management, you can add these two classes to the newly created AI folder, and pay attention to the include path in the code; the other is that the soldier model downloaded from the Epic store does not contain animations Blueprint, so you can click here to download the file from the author's git, and refer to the original path to put it in the content/ParagonMinions/Characters/Minions/Down_Minions/Meshes directory of the local path. This blueprint has been modified and does not need to be debugged as in the course.

Then, add a NavMeshBoundsVolume         to the world from Place Actors and scale it up to roughly the size of the floor. In this section, its role is to calculate the collision volume and prevent specific Actors from entering the volume, that is, to calculate which places the Actor can go and which places the Actor cannot go. If the navigation display is turned on as shown in Figure 24-2, the passable areas in the world will be displayed in green, as shown in Figure 24-3.

Figure 24-2 Turn on the navigation display
Figure 24-3 Green indicates reachable

2. Pathfinding behavior tree

        Create "blackboard" and "behavior tree" under "artificial intelligence" in UE, and "blackboard" can be understood as the memory of AI. Create a vector-type MoveToLocation key in the blackboard to record the destination of the minion's movement. Add simple logic to the behavior tree, as shown in Figure 24-4, to realize that the soldier moves towards the position recorded by MoveToLocation, and waits for 5 seconds after arriving:

Figure 24-4 Pathfinding Behavior Tree

         After completion, create a parallel behavior tree object in the relevant code of SurAIController and expose it to UE, rewrite BeginPlay, implement the running behavior tree, and assign the player's position to MoveToLocation. The core code is as follows, and the core code can also be found in my github page:

RunBehaviorTree(BehaviorTree);

APawn* MyPawn = UGameplayStatics::GetPlayerPawn(this, 0);
if (MyPawn) {
    GetBlackboardComponent()->SetValueAsVector("MoveToLocation", MyPawn->GetActorLocation());
	}

        Finally, go back to UE to create the blueprint class of AIController, name it MinionControllerBP, and set the exposed BehaviorTree as the previously created pathfinding behavior tree after opening; "AI Controller Class" is set to MinionControllerBP.

Figure 24-5 Setting up the minion controller

        Compile and run after completion, and the creeps will automatically move towards the player's birth point, as shown in Figure 24-6. In addition, if there is an obstacle in the line, the minion can also bypass the obstacle to reach the destination:

Figure 24-6 Automatic pathfinding

 

Guess you like

Origin blog.csdn.net/surkea/article/details/128052893