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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Configure PerceptionComponent in C++

Hi,

So I'm trying to set some basic AI perception sense config in C++, but for some reason, it crashes. Here's what I have right now.

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #pragma once
 
 #include "AIController.h"
 #include "Perception/AIPerceptionComponent.h"
 #include "CitizenAIController.generated.h"
 
 
 /**
  * 
  */
 UCLASS()
 class FPS_MMO_API ACitizenAIController : public AAIController
 {
     GENERATED_BODY()
 
     ACitizenAIController();
 
     UPROPERTY(VisibleAnywhere, Category = "Perception")
     class UAISenseConfig_Sight* config;
 
     UFUNCTION()
     void OnPerception(AActor* Actor, FAIStimulus Stimulus);
     
 };
 

and my .cpp is quite simple:

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "FPS_MMO.h"
 #include "Perception/AISenseConfig_Sight.h"
 #include "Perception/AISense.h"
 #include "CitizenAIController.h"
 
 
 ACitizenAIController::ACitizenAIController() 
 {
 
     config = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight sense"));
     config->SightRadius = 300.f;
     config->LoseSightRadius = 400.f;
     config->PeripheralVisionAngleDegrees = 180.f;
     config->DetectionByAffiliation.DetectAllFlags();
     
     GetPerceptionComponent()->ConfigureSense(*config);
     GetPerceptionComponent()->OnTargetPerceptionUpdated.AddDynamic(this, &ACitizenAIController::OnPerception);
 }
 
 void ACitizenAIController::OnPerception(AActor* Actor, FAIStimulus Stimulus) 
 {
     if (Stimulus.IsValid()) {
         UE_LOG(LogTemp, Warning, TEXT("Perception valide"));
     }
     else {
         UE_LOG(LogTemp, Warning, TEXT("LOGGING"));
     }
 }
 
 

I can't find anything about it in recent versions. Has anybody achieved this?

Product Version: UE 4.14
Tags:
more ▼

asked Dec 18 '16 at 06:58 AM in C++ Programming

avatar image

Snappydue
13 3 7 8

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

1 answer: sort voted first

Hi,

I did something similar to that some time ago, but instead of put as property the SenseConfig, I put in my NPC Pawn only the properties that I want to set on the Perception and in the AIController I already have the perception component with the SightSense added. With that, the only thing that I do is after Possess the NPC, get the SightSense on the Controller/PerceptionComponent and set the config variables with the variables on my Pawn.

I have something like this in my AIController:

 //Update UAISenseConfig_Sight properties based con possessed pawn
     AMyCharacter* Character = Cast<AMyCharacter>(GetPawn());
     if (Character)
     {
         // Setup AIController's Perception system values
         UAIPerceptionComponent* PerceptionComp = GetAIPerceptionComponent();
         if (PerceptionComp)
         {
             FAISenseID SenseID = UAISense::GetSenseID<UAISense_Sight>();
             UAISenseConfig_Sight* SenseConfig = Cast<UAISenseConfig_Sight>(PerceptionComp->GetSenseConfig(SenseID));
             if (SenseConfig)
             {
                 // Noticie here, in AMyCharacter I have two properties AISenseSight_Radius, AISenseSight_LoseSightRadius that allow me to define
                 // the perception setup for this specific character using the same AIController class.
                 SenseConfig->SightRadius = Character->AISenseSight_Radius;
                 SenseConfig->LoseSightRadius = Character->AISenseSight_LoseSightRadius;
 
                 PerceptionComp->ConfigureSense(*SenseConfig);
             }
         }
     }

On the other hand, to bind the perceptionupdated function:

 // AIController .h
 
 UFUNCTION()
 void OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus Stimulus);
 
 // MyAIController .cpp
 
 void AMyAIController::BeginPlay()
 {
     Super::BeginPlay();
 
     UAIPerceptionComponent* PerceptionComp = GetAIPerceptionComponent();
     if (PerceptionComp)
     {
         PerceptionComp->OnTargetPerceptionUpdated.AddDynamic(this, &AMyAIController::OnTargetPerceptionUpdated);
     }
 }
 
 void AMyAIController::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus Stimulus)
 {
     // do something
 }

Hope this help you.

Best regards,

more ▼

answered Dec 18 '16 at 02:10 PM

avatar image

nan2cc
502 23 13 38

avatar image Snappydue Dec 21 '16 at 06:10 PM

Thanks a lot for your answer! I finally made it work. I've move the code in the begin play to have a better understanding of the problem and realize that the

 GetPerceptionComponent();

function was only a shortcut, but you actually had to set the perception component before yourself. Which is weird since if you create a local variable and give me a getter, I expect the variable to be already set. Anyway, big thanks for your time!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
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