x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

AIPerception component on character

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

 private:
     /** 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

 APatrolEnemy::APatrolEnemy()
 {
     // Configure sight sense.
     SightSenseConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("SightSense"));
     SightSenseConfig->SightRadius = 2500.f;
     SightSenseConfig->LoseSightRadius = 3000.f;
     SightSenseConfig->PeripheralVisionAngleDegrees = 85.f;
     SightSenseConfig->DetectionByAffiliation.DetectAllFlags();
 
     // Configure hearing sense.
     HearingSenseConfig = CreateDefaultSubobject<UAISenseConfig_Hearing>(TEXT("HearningSense"));
     HearingSenseConfig->HearingRange = 2000.f;
     HearingSenseConfig->DetectionByAffiliation.DetectAllFlags();
 
     // Configure AI perception component.
     AIPerceptionComp = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("AIPerception"));
     AIPerceptionComp->ConfigureSense(*SightSenseConfig);
     AIPerceptionComp->ConfigureSense(*HearingSenseConfig);
     AIPerceptionComp->SetDominantSense(SightSenseConfig->GetSenseImplementation());
 }
 
 void APatrolEnemy::BeginPlay()
 {
     Super::BeginPlay();
     
     // Setup the perception component on the AI controller.
     APatrolEnemyAIController* AIController = Cast<APatrolEnemyAIController>(Controller);
     if (AIController)
     {
         UE_LOG(LogTemp, Warning, TEXT("There is an AI controller!"));
         AIController->SetPerceptionComponent(*AIPerceptionComp);
     }
 }
 
 void APatrolEnemy::PostInitializeComponents()
 {
     Super::PostInitializeComponents();
 
     AIPerceptionComp->OnPerceptionUpdated.AddDynamic(this, &APatrolEnemy::OnPerceptionUpdated);
 }
 
 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.

Product Version: UE 4.14
Tags:
more ▼

asked Dec 28 '16 at 09:54 PM in C++ Programming

avatar image

Or Aviram
1 2 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question