I’m trying to set an AI sight perception in a custom AI controller based on the sight config parameters defined in the ai pawn owner class. In my character class, I am assigning the sight config parameters (e.g. max age, sight radius, etc) in PreInitializeComponents() and spawning the default controller in BeginPlay(). In my controller’s OnPossess(), I define sight configs and call ConfigureSense.
However, once I play in editor, I get a warning “LogAIPerception: Warning: Listener must have a valid id to update its sense config”. When I debug the perception, I can see the green and pink sight circle but cannot see the green sphere when other actor is detected. Does the ConfigureSense() need to be called in the constructor? When I move the configure sense code to the constructor the perception works, but I must set the properties of the sight perception after the pawn is possessed.
Example code below:
Controller .h
UCLASS()
class TEST_API ATestAIController : public AAIController
{
GENERATED_BODY()
public:
ATestAIController();
virtual void OnPossess(APawn* const pawn) override;
TObjectPtr< class UAISenseConfig_Sight> sight_;
}
Controller .cpp
ATestAIController::ATestAIController()
{
PrimaryActorTick.bCanEverTick = true;
sight_ = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
SetPerceptionComponent(*CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component")));
}
ATestAIController::OnPossess(APawn* const pawn)
{
Super::OnPossess(pawn);
// the values are taken from the owner pawn class, but for now I added random values for testing
sight_->SightRadius = 2000;
sight_->LoseSightRadius = 2050;
sight_->PeripheralVisionAngleDegrees = 90;
sight_->SetMaxAge(5.0f);
sight_->AutoSuccessRangeFromLastSeenLocation = 2100;
sight_->DetectionByAffiliation.bDetectEnemies = true;
sight_->DetectionByAffiliation.bDetectFriendlies = true;
sight_->DetectionByAffiliation.bDetectNeutrals = true;
GetPerceptionComponent()->SetDominantSense(*sight_->GetSenseImplementation());
GetPerceptionComponent()->ConfigureSense(*sight_);
}