Add Sight and Hearing to Perception Component in C++

G’Day I am trying to add sight and hearing to a custom C++ AIPerception component, so I can add some extra function to perception to change range etc. at runtime…

.h looks like this;

#include "CoreMinimal.h"
#include "UObject/ObjectMacros.h"
#include "Templates/SubclassOf.h"
#include "Components/ActorComponent.h"
#include "EngineDefines.h"
#include "GenericTeamAgentInterface.h"
#include "Perception/AISense.h"
#include "Perception/AIPerceptionSystem.h"
#include "Perception/AIPerceptionComponent.h"
#include <UObject/ObjectMacros.h>
#include <Perception/AISenseConfig_Sight.h>
#include <Perception/AISenseConfig_Hearing.h>
#include "SRAIPerceptionComponent.generated.h"

/**
 * 
 */
UCLASS(ClassGroup = AI, HideCategories = (Activation, Collision), meta = (BlueprintSpawnableComponent), config = Game)
class SAVAGE_API USRAIPerceptionComponent : public UAIPerceptionComponent
{
	GENERATED_BODY()
public:
	// Sets defaults
	USRAIPerceptionComponent();
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "SRPerception")
	UAISenseConfig_Sight* sightConfig;
	UAISenseConfig_Hearing* hearingConfig;

	UFUNCTION(BlueprintCallable, Category = "SRPerception|Range")
		void SetSightSenseRadius(float newRange);


};

.cpp looks like this;

#include "SRAIPerceptionComponent.h"

USRAIPerceptionComponent::USRAIPerceptionComponent()
{
	sightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("SR Sight Config"));
	USRAIPerceptionComponent::ConfigureSense(*sightConfig);

	hearingConfig = CreateDefaultSubobject<UAISenseConfig_Hearing>(TEXT("SR Hearing Config"));
	USRAIPerceptionComponent::ConfigureSense(*hearingConfig);

	

	sightConfig->SightRadius = 6400.0f;
	sightConfig->LoseSightRadius = 7500.0f;
	sightConfig->PeripheralVisionAngleDegrees = 142.0f;
	sightConfig->DetectionByAffiliation.bDetectEnemies = true;
	sightConfig->DetectionByAffiliation.bDetectNeutrals = true;
	sightConfig->DetectionByAffiliation.bDetectFriendlies = true;
	sightConfig->SetMaxAge(45.0f);
	USRAIPerceptionComponent::ConfigureSense(*sightConfig);

	hearingConfig->HearingRange = 6000.0f;
	hearingConfig->DetectionByAffiliation.bDetectEnemies = true;
	hearingConfig->DetectionByAffiliation.bDetectNeutrals = true;
	hearingConfig->DetectionByAffiliation.bDetectFriendlies = true;
	hearingConfig->SetMaxAge(35.0f);
	USRAIPerceptionComponent::ConfigureSense(*hearingConfig);

	USRAIPerceptionComponent::SetDominantSense(sightConfig->GetSenseImplementation());

}

void USRAIPerceptionComponent::SetSightSenseRadius(float newRange)
{
	sightConfig->SightRadius = newRange;
	sightConfig->LoseSightRadius = newRange + 1000.0f;
	USRAIPerceptionComponent::ConfigureSense(*sightConfig);
}

It seems to be working but only the sight is showing up in the blueprint under senses, am guessing I need to add the hearing sense to the

UPROPERTY(EditDefaultsOnly, Instanced, Category = "AI Perception")
	TArray<UAISenseConfig*> SensesConfig;

SensesConfig in AIPerception, but cannot find a function to add a sense, any help with this would be greatly appreciated :slight_smile:

The USRAIPerceptionComponent::ConfigureSense(*sightConfig); seems to add the sense but only sight is showing up, something weird going on :slight_smile:

Finally figured this out as being a blueprint issue, the Playercontroller blueprint wasn’t showing both senses configured until I hit the reset default button

Once I did that both senses showed up and the SetSightSenseRadius function works a treat

1 Like