I have a current setup where I have a base C++ class for my enemies (deriving from ACharacter), which doesn't have anything to do with AI, but is only for properties enemies have (like health, for example). Deriving from that I have a C++ class for every type of AI the enemy has, like one for patrolling through waypoints. Then I have a blueprint for each type of Enemy, which I can just instantiate in my scene.
But of course I also need AI controllers: So there is no base class for an enemy AI controller. Every type on Enemy AI will have an AI type, so the patrol enemy will have a c++ AI controller that controls it. That's it, it does not end with a blueprint. So I don't want to have components that have values I need to tweak a lot on it. The AIPerception component has some value I want to tweak, that's why I don't want to put it on the AI controller. AI controllers should be purely functionality, no values.
Those are my scripts (only the parts that matter, of course): My character header file
/** Called whenever actors have been sensed */
void OnPerceptionUpdated(TArray<AActor*> SensedActors);
/** Behavior tree asset of this enmey */
UPROPERTY(EditDefaultsOnly, Category = "AI")
class UBehaviorTree* BehaviorTree;
// AI Perception sense configs:
class UAISenseConfig_Sight* SightSenseConfig;
class UAISenseConfig_Hearing* HearingSenseConfig;
/** AI sensing component of this enemy */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "AI", meta = (AllowPrivateAccess = "true"))
class UAIPerceptionComponent* AIPerceptionComp;
The source file
// Configure sight sense.
SightSenseConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("SightSense"));
SightSenseConfig->SightRadius = 2500.f;
SightSenseConfig->LoseSightRadius = 3000.f;
SightSenseConfig->PeripheralVisionAngleDegrees = 85.f;
// Configure hearing sense.
HearingSenseConfig = CreateDefaultSubobject<UAISenseConfig_Hearing>(TEXT("HearningSense"));
HearingSenseConfig->HearingRange = 2000.f;
// Configure AI perception component.
AIPerceptionComp = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("AIPerception"));
// Setup the perception component on the AI controller.
APatrolEnemyAIController* AIController = Cast<APatrolEnemyAIController>(Controller);
UE_LOG(LogTemp, Warning, TEXT("There is an AI controller!"));
void APatrolEnemy::OnPerceptionUpdated(TArray<AActor*> SensedActors)
UE_LOG(LogTemp, Warning, TEXT("Sensed stuff!"));
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Yellow, "Sensed stuff!");
I found this post. So as you can see in the code, I tried to use SetPerceptionComponent, but whenever I get into the sight range, I don't get the output of "Sensed stuff!". I think I might be missing something, because they were talking about PostRegisterAllComponents, which I don't know how is related at all, so I would love if someone explained it. And the also said something about AIPerceptionStimuliSourceComponent, which I can't find ANYTHING about online, so it also confuses me.
Dec 28 '16 at 09:54 PM
in C++ Programming