Setup AI touch sense in c++

Hi so does anyone know how I can setup an AI touch sense in my AI controller in c++. Any help is much appreciated, thank you :slight_smile:

1 Like

Hi, you can/need to create the config for the touch sense in the constructor of your AI controller and then add it to your AIPerceptionComponent. So the same you would do for any other sense.



UAIConfig_Touch* MySenseConfigTouch = CreateDefaultSubobject<UAISenseConfig_Touch>(TEXT("Touch Config"));
MyAIPerceptionComponent->ConfigureSense(*MySenseConfigTouch);

That was all you need to do inside the AI controller (btw you can also do the step above in blueprints, no need to use C++ till here). Now you also need touch events. So you could/would make a static function that you can call from anywhere (and best make it also blueprint callable so that you can send touch events from blueprints as well) that registers a touch event to the AI system.

So you will need to manually call this static function anytime you want to register a touch event (e. g. when you register a collision inside your logic, but you can ofc also register a touch event with an actor that is 100 meters away…), it won’t happen automagically under the hood (only for sight it will).

Never used the touch sense, but for the team sense the static function I implemented to register a team event looked like



void UMyGameplayStaticsLibrary::ReportTeamEvent(AActor* InBroadcaster, AActor* InEnemy, const FVector& InLastKnowLocation, float EventRange, float PassedInfoAge, float InStrength)
{
    if (InBroadcaster)
    {
        UAIPerceptionSystem* PerceptionSystem = UAIPerceptionSystem::GetCurrent(InBroadcaster);
        if (PerceptionSystem)
        {
            FAITeamStimulusEvent Event = FAITeamStimulusEvent(InBroadcaster, InEnemy, InLastKnowLocation, EventRange, PassedInfoAge, InStrength);
            PerceptionSystem->OnEvent(Event);
        }
    }
}
}

So I guess you would only need to replace FAITeamStimulusEvent with FAITouchEvent and ofc the input into the constructor of the event (take a look at


UAISense_Touch.h

)

EDIT: And the TouchReceiver also needs to implement the IAIPerceptionListenerInterface (located in "Perception/AIPerceptionListenerInterface.h").

Thanks so much for this, will try this out now!