So I have an Enemy c++ class which I inherit as an Enemy blueprint. To add AI functionality I created a Behaviour tree for my Enemy Blueprint. The issue is that the “Move to” node is not working and keeps failing.
I created a standard blueprint class in the editor and the move to node works on it, but not on my c++ one.
Any ideas about the difference between the two blueprints if both are characters?
Do you you have a navMeshBoundsVolume in your scene that covers where the ai moves?
inside the ai character c++ move to code
//in your build file make sure you have AIModule
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "EnhancedInput", "AIModule" });
.h
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "InputActionValue.h"
#include "aicppCharacter_AI.generated.h"
UCLASS(config=Game)
class AaicppCharacter_AI : public ACharacter
{
GENERATED_BODY()
/** Jump Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* JumpAction;
public:
AaicppCharacter_AI();
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = AIConfig, meta = (AllowPrivateAccess = "true"))
class UBlackboardComponent* Blackboard;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = AIConfig, meta = (AllowPrivateAccess = "true"))
class UBehaviorTreeComponent* BehaviorTree;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = AIConfig, meta = (AllowPrivateAccess = "true"))
FVector Destination;
protected:
/** Called for movement input */
void Move(const FInputActionValue& Value);
/** Called for looking input */
void Look(const FInputActionValue& Value);
protected:
// To add mapping context
virtual void BeginPlay();
};
cpp
// Copyright Epic Games, Inc. All Rights Reserved.
#include "aicppCharacter_AI.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "BehaviorTree/BehaviorTreeComponent.h"
#include "Blueprint/AIBlueprintHelperLibrary.h"
#include "AIController.h"
//////////////////////////////////////////////////////////////////////////
// AaicppCharacter
AaicppCharacter_AI::AaicppCharacter_AI()
{
// Set size for collision capsule
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
// Don't rotate when the controller rotates. Let that just affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// Configure character movement
GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...at this rotation rate
// Note: For faster iteration times these variables, and many more, can be tweaked in the Character Blueprint
// instead of recompiling to adjust them
GetCharacterMovement()->JumpZVelocity = 700.f;
GetCharacterMovement()->AirControl = 0.35f;
GetCharacterMovement()->MaxWalkSpeed = 500.f;
GetCharacterMovement()->MinAnalogWalkSpeed = 20.f;
GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;
// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
// are set in the derived blueprint asset named ThirdPersonCharacter (to avoid direct content references in C++)
Blackboard = CreateDefaultSubobject<UBlackboardComponent>(TEXT("BlackBoard"));
BehaviorTree = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("BehaviorTree"));
}
void AaicppCharacter_AI::BeginPlay()
{
// Call the base class
Super::BeginPlay();
AAIController* aic = UAIBlueprintHelperLibrary::GetAIController(this);
aic->MoveToLocation(Destination);
}
You also need to set the behavior & blackboard in the actor components.
As I said a non c++ character with the same setting works. NavMesh and all working, it’s the MoveTo node that fails. (used print strings to check )
So it won’t work if I just add those components in the blueprint…And will this still allow me to visually make the decision tree as I really don’t want to code the decision tree.
If possible can you list just the additions to the c++ files that still allow me to add AI functionality using Blueprints?
Thanks in Advance.
To get basic move to working all you need is the ai controller.
If you want behavior trees then you need the blackboard BehaviorTreeComponent either in c++ or bp.
I prefer to have the blackboard component as well to have faster access to changes on it if needed.
If you want it in c++ then the “AIModule” module is a must in the build file.
Thanks, I’ll try it tonight.
Ok so I tried adding the module but it did not make a difference.
In you scene in the viewport press p and see if the navmeshvolume works.
Also are you placing the character in the scene or dynamically spawning it?
If you are spawning it then make sure ai takes control when placed in world or spawned.
Nav mesh works, possess it set to on spawn and placed.
And I have another question…can my " Enemy" take a blueprint AiController or it needs a c++ one…I saw a video and he made a c++ Aicontroller.
I already showed you how to get the aicontroller connected to your character. It’s this part
AAIController* aic = UAIBlueprintHelperLibrary::GetAIController(this);
That is if your character extends the default ACharacter and is not a pawn.
But shouldn’t adding it in the blueprint also work… anyways thanks for the help…I’ll probably stick with blueprints for Ai.
You can access it either way. The c++ method is if you want to do changes in code. They both call the same underlying code (this code & Get AI Controller do the same).
As my task is running in the behaviour tree the controller must be working right…So I guess the issue is not with the controller but something else.
I’ll list my steps …see if I missed something
- Created an “Enemy” c++ class
- Added properties in the .h file
- Gave default values in the .cpp file
- Created a Blueprint based on the c++ class
- Created an AIcontroller blueprint and added “Run Behaviour tree” in it
- Created a Blackboard and Decision Tree
- Created a task that moves to a random location in the navmesh
Now with this, the moveTo node kept failing
- Added the AIModule in the build.cs file
Recompiled but the same issue.
Try debugging you AI with the ’ key in game.
Analyze if it sees the navmesh, what behavior tree is it running, what task it has set etc.
From what I can tell the controller and behaviour tree are working perfectly, the perception data is also being updated correctly. The AI clearly see the nav mesh…What is the issue…I don’t understand.
Your character movement mode is wrong. It should be walking.
Does you character have any movement component?
Is it based on a character or are you using a pawn as the base?
This is the blueprint only “enemy”…it also has running but works fine.
- The base for my c++ class is the character class
Project with c++ ai with random roam
I’ll try this tonight.
I have UE 5.1 and the project is not opening, it says to try recompiling manually, how do I do that ?
You need to generate the visual studio project (right click “generate visual studio project”) open it, compile & run it