AI Sens Implementation : How to override one ?

Hello,

Simple question, i would like to modify for AISens_Hearing (in c++).
I’ve inherited a new class from it, but when you change “implementation” :

Then no stimuli is registered.
I’ve digged into source code, but doesn’t found what i miss, any idea ?

Hi,

I think you will also need to create your own NoiseEvent, where you tell the engine to send that event to your UAISens_AdvancedHearing.

So you could also create a new NoiseEvent inheriting from the regular one like this:


USTRUCT(BlueprintType)
struct AISENSETEST_API FMyAINoiseEvent : public FAINoiseEvent
{
	GENERATED_USTRUCT_BODY()

	typedef class UMyAISense_Hearing FSenseClass;

	FMyAINoiseEvent();
	FMyAINoiseEvent(AActor* InInstigator, const FVector& InNoiseLocation, float InLoudness = 1.f, float InMaxRange = 0.f, FName Tag = NAME_None);
	
};

where the important part is:

typedef class UMyAISense_Hearing FSenseClass;

For your code there you would put your new hearing sense class there, so

typedef class UAISens_AdvancedHearing FSenseClass;

That should make the AIPerceptionSystem send that event to your UAISens_AdvancedHearing sense.


The whole code (minimalistic example):

MyAISenseHearing.h:


#pragma once

#include "CoreMinimal.h"
#include "Perception/AISense_Hearing.h"
#include "MyAISense_Hearing.generated.h"



USTRUCT(BlueprintType)
struct AISENSETEST_API FMyAINoiseEvent : public FAINoiseEvent
{
	GENERATED_USTRUCT_BODY()

	typedef class UMyAISense_Hearing FSenseClass;

	FMyAINoiseEvent();
	FMyAINoiseEvent(AActor* InInstigator, const FVector& InNoiseLocation, float InLoudness = 1.f, float InMaxRange = 0.f, FName Tag = NAME_None);
	
};


/**
 * 
 */
UCLASS()
class AISENSETEST_API UMyAISense_Hearing : public UAISense_Hearing
{
	GENERATED_BODY()
	

public:
	UFUNCTION(BlueprintCallable, Category = "AI|Perception", meta = (WorldContext = "WorldContextObject"))
	static void MyReportNoiseEvent(UObject* WorldContextObject, FVector NoiseLocation, float Loudness = 1.f, AActor* Instigator = nullptr, float MaxRange = 0.f, FName Tag = NAME_None);
};

and .cpp:

#include "MyAISense_Hearing.h"
#include "Perception/AIPerceptionSystem.h"

//----------------------------------------------------------------------//
// FMyAINoiseEvent
//----------------------------------------------------------------------//
FMyAINoiseEvent::FMyAINoiseEvent()
{
}


FMyAINoiseEvent::FMyAINoiseEvent(AActor* InInstigator, const FVector& InNoiseLocation, float InLoudness, float InMaxRange, FName InTag)
{
	Age = 0.0f;
	NoiseLocation = InNoiseLocation;
	Loudness = InLoudness;
	MaxRange = InMaxRange;
	Instigator = InInstigator;
	Tag = InTag;
	TeamIdentifier = FGenericTeamId::NoTeam;
	Compile();
}



void UMyAISense_Hearing::MyReportNoiseEvent(UObject* WorldContextObject, FVector NoiseLocation, float Loudness, AActor* Instigator, float MaxRange, FName Tag)
{
	UAIPerceptionSystem* PerceptionSystem = UAIPerceptionSystem::GetCurrent(WorldContextObject);
	if (PerceptionSystem)
	{
		FMyAINoiseEvent Event = FMyAINoiseEvent(Instigator, NoiseLocation, Loudness, MaxRange, Tag);
		PerceptionSystem->OnEvent(Event);
	}

}

If you would then report an event for your new hearing sense, you would use the MyReportNoiseEvent function.


I’m not sure though whether there is an easier/better way to do this.

1 Like

Thank you so much for your detailed reply, will try that !