UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
AIPerceptionComponent.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
7#include "UObject/ObjectKey.h"
10#include "EngineDefines.h"
13#include "Perception/AISense.h"
15#include "AIPerceptionComponent.generated.h"
16
17class AAIController;
19class UAISenseConfig;
20struct FVisualLogEntry;
21
25
26USTRUCT(BlueprintType, meta = (DisplayName = "Sensed Actor's Update Data"))
28{
30
31
32 UPROPERTY(BlueprintReadWrite, Category = "AI|Perception")
33 int32 TargetId = -1;
34
36 UPROPERTY(BlueprintReadWrite, Category = "AI|Perception")
38
40 UPROPERTY(BlueprintReadWrite, Category = "AI|Perception")
41 FAIStimulus Stimulus;
42
44 FActorPerceptionUpdateInfo(const int32 TargetId, const TWeakObjectPtr<AActor>& Target, const FAIStimulus& Stimulus);
45};
46
48
50{
52
54
58
61
64
66 : Target(InTarget), DominantSense(FAISenseID::InvalidID()), bIsHostile(false), bIsFriendly(false)
67 {
68 LastSensedStimuli.AddDefaulted(FAISenseID::GetSize());
69 }
70
74 {
76 float BestAge = FLT_MAX;
77 bool bBestWasSuccessfullySensed = false;
78 for (int32 Sense = 0; Sense < LastSensedStimuli.Num(); ++Sense)
79 {
80 const float Age = LastSensedStimuli[Sense].GetAge();
81 const bool bWasSuccessfullySensed = LastSensedStimuli[Sense].WasSuccessfullySensed();
82
83 if (Age >= 0 && (Age < BestAge
85 || (Sense == DominantSense && bWasSuccessfullySensed)))
86 {
87 BestAge = Age;
88 Location = LastSensedStimuli[Sense].StimulusLocation;
90
91 if (Sense == DominantSense && bWasSuccessfullySensed)
92 {
93 // if dominant sense is active we don't want to look any further
94 break;
95 }
96 }
97 }
98
99 if (OptionalAge)
100 {
102 }
103
104 return Location;
105 }
106
109 {
110 for (const FAIStimulus& Stimulus : LastSensedStimuli)
111 {
112 // not that WasSuccessfullySensed will return 'false' for expired stimuli
113 if (Stimulus.IsValid() && (Stimulus.WasSuccessfullySensed() == true || Stimulus.IsExpired() == false))
114 {
115 return true;
116 }
117 }
118
119 return false;
120 }
121
124 {
125 for (const FAIStimulus& Stimulus : LastSensedStimuli)
126 {
127 // not that WasSuccessfullySensed will return 'false' for expired stimuli
128 if (Stimulus.IsValid() && Stimulus.WasSuccessfullySensed() == true && Stimulus.IsExpired() == false)
129 {
130 return true;
131 }
132 }
133
134 return false;
135 }
136
143 {
144 return LastSensedStimuli.IsValidIndex(Sense) && (LastSensedStimuli[Sense].IsValid() && (LastSensedStimuli[Sense].IsExpired() == false)) ? LastSensedStimuli[Sense].StimulusLocation : FAISystem::InvalidLocation;
145 }
146
153 {
154 return LastSensedStimuli.IsValidIndex(Sense) && (LastSensedStimuli[Sense].IsValid() && (LastSensedStimuli[Sense].IsExpired() == false)) ? LastSensedStimuli[Sense].ReceiverLocation : FAISystem::InvalidLocation;
155 }
156
162 inline bool HasKnownStimulusOfSense(const FAISenseID Sense) const
163 {
164 return LastSensedStimuli.IsValidIndex(Sense) && (LastSensedStimuli[Sense].IsValid() && (LastSensedStimuli[Sense].IsExpired() == false));
165 }
166
172 inline bool IsSenseActive(const FAISenseID Sense) const
173 {
174 return LastSensedStimuli.IsValidIndex(Sense) && LastSensedStimuli[Sense].IsActive();
175 }
176
179};
180
181USTRUCT(BlueprintType, meta = (DisplayName = "Sensed Actor's Data"))
183{
185
186 UPROPERTY(BlueprintReadWrite, Category = "AI|Perception")
188
189 UPROPERTY(BlueprintReadWrite, Category = "AI|Perception")
190 TArray<FAIStimulus> LastSensedStimuli;
191
192 UPROPERTY(BlueprintReadWrite, Category = "AI|Perception")
193 uint32 bIsHostile : 1;
194
195 UPROPERTY(BlueprintReadWrite, Category = "AI|Perception")
196 uint32 bIsFriendly : 1;
197
198 FActorPerceptionBlueprintInfo() : Target(nullptr), bIsHostile(false), bIsFriendly(false)
199 {}
201};
202
207UCLASS(ClassGroup=AI, HideCategories=(Activation, Collision), meta=(BlueprintSpawnableComponent), config=Game, MinimalAPI)
209{
211
213
216
218 UPROPERTY(EditDefaultsOnly, Instanced, Category = "AI Perception")
220
223 UPROPERTY(EditDefaultsOnly, Category = "AI Perception")
224 TSubclassOf<UAISense> DominantSense;
225
227
229 TObjectPtr<AAIController> AIOwner;
230
233
234private:
237
238protected:
240 {
242 FAIStimulus Stimulus;
243
245 : Source(InSource), Stimulus(InStimulus)
246 {
247
248 }
249 };
250
252
255
256private:
257
260 uint32 bForgetStaleActors : 1;
261
262 uint32 bCleanedUp : 1;
263
264public:
265
266 AIMODULE_API virtual void PostInitProperties() override;
267 AIMODULE_API virtual void BeginDestroy() override;
268 AIMODULE_API virtual void OnRegister() override;
269 AIMODULE_API virtual void OnUnregister() override;
270
271 UFUNCTION()
272 AIMODULE_API void OnOwnerEndPlay(AActor* Actor, EEndPlayReason::Type EndPlayReason);
273
274 AIMODULE_API void GetLocationAndDirection(FVector& Location, FVector& Direction) const;
275 AIMODULE_API const AActor* GetBodyActor() const;
277
279
280 AIMODULE_API FGenericTeamId GetTeamIdentifier() const;
282
283 AIMODULE_API FVector GetActorLocation(const AActor& Actor) const;
284 inline const FActorPerceptionInfo* GetActorInfo(const AActor& Actor) const { return PerceptualData.Find(&Actor); }
285 inline FActorPerceptionContainer::TIterator GetPerceptualDataIterator() { return FActorPerceptionContainer::TIterator(PerceptualData); }
286 inline FActorPerceptionContainer::TConstIterator GetPerceptualDataConstIterator() const { return FActorPerceptionContainer::TConstIterator(PerceptualData); }
287
289
291
296
297 // @note Will stop on first age 0 stimulus
299
301 inline FAISenseID GetDominantSenseID() const { return DominantSenseID; }
302 inline TSubclassOf<UAISense> GetDominantSense() const { return DominantSense; }
304 AIMODULE_API const UAISenseConfig* GetSenseConfig(const FAISenseID& SenseID) const;
305
306 template<typename T, typename = std::enable_if_t<std::is_base_of_v<UAISenseConfig, T>>>
307 T* GetSenseConfig() const
308 {
310 {
312 {
313 return SenseConfigType;
314 }
315 }
316 return nullptr;
317 }
318
320
323
325 UFUNCTION(BlueprintCallable, Category="AI|Perception")
327
329 AIMODULE_API void UpdatePerceptionAllowList(const FAISenseID Channel, const bool bNewValue);
330
331 UE_DEPRECATED(5.0, "Use UpdatePerceptionAllowList instead")
332 void UpdatePerceptionWhitelist(const FAISenseID Channel, const bool bNewValue)
333 {
335 }
336
337
338 AIMODULE_API void RegisterStimulus(AActor* Source, const FAIStimulus& Stimulus);
339 AIMODULE_API void ProcessStimuli();
341 AIMODULE_API bool AgeStimuli(const float ConstPerceptionAgingRate);
343
345 UFUNCTION(BlueprintCallable, Category = "AI|Perception")
346 AIMODULE_API void ForgetAll();
347
348 AIMODULE_API float GetYoungestStimulusAge(const AActor& Source) const;
349 AIMODULE_API bool HasAnyActiveStimulus(const AActor& Source) const;
350 AIMODULE_API bool HasAnyCurrentStimulus(const AActor& Source) const;
351 AIMODULE_API bool HasActiveStimulus(const AActor& Source, const FAISenseID Sense) const;
352
353#if WITH_GAMEPLAY_DEBUGGER_MENU
355#endif // WITH_GAMEPLAY_DEBUGGER_MENU
356
357#if ENABLE_VISUAL_LOG
359#endif // ENABLE_VISUAL_LOG
360
361 //----------------------------------------------------------------------//
362 // blueprint interface
363 //----------------------------------------------------------------------//
364 UFUNCTION(BlueprintCallable, Category = "AI|Perception")
366
367 UFUNCTION(BlueprintCallable, Category = "AI|Perception")
369
371 UFUNCTION(BlueprintCallable, Category = "AI|Perception")
373
375 UFUNCTION(BlueprintCallable, Category = "AI|Perception")
377
379 UFUNCTION(BlueprintCallable, Category = "AI|Perception")
381
384 UFUNCTION(BlueprintCallable, Category = "AI|Perception")
386
389 UFUNCTION(BlueprintCallable, Category = "AI|Perception")
391
393 // Might want to move these to special "BP_AIPerceptionComponent"
395 UPROPERTY(BlueprintAssignable)
397
407 UPROPERTY(BlueprintAssignable)
409
422 UPROPERTY(BlueprintAssignable)
424
435 UPROPERTY(BlueprintAssignable)
437
441
443 AIMODULE_API virtual void CleanUp();
444
446
448 UE_DEPRECATED(5.6, "RefreshStimulus is deprecated. Use ConditionallyStoreSuccessfulStimulus() instead.")
450
457
460
461private:
463
466 AIMODULE_API void SetMaxStimulusAge(const FAISenseID SenseId, float MaxAge);
467};
468
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(DelegateName, Param1Type, Param1Name, Param2Type, Param2Name)
Definition DelegateCombinations.h:62
#define DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(DelegateName, Param1Type, Param1Name)
Definition DelegateCombinations.h:53
#define UPROPERTY(...)
UObject definition macros.
Definition ObjectMacros.h:744
#define UFUNCTION(...)
Definition ObjectMacros.h:745
#define GENERATED_UCLASS_BODY(...)
Definition ObjectMacros.h:768
#define UCLASS(...)
Definition ObjectMacros.h:776
#define USTRUCT(...)
Definition ObjectMacros.h:746
#define GENERATED_USTRUCT_BODY(...)
Definition ObjectMacros.h:767
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition AIController.h:91
Definition Actor.h:257
Definition GameplayDebuggerCategory.h:49
Definition UnrealType.h:3087
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
SizeType AddDefaulted()
Definition Array.h:2795
UE_NODEBUG UE_FORCEINLINE_HINT bool IsValidIndex(SizeType Index) const
Definition Array.h:1122
Definition AssetRegistryState.h:50
Definition Array.h:64
Definition UnrealString.h.inl:34
Definition ObjectKey.h:228
Definition SubclassOf.h:30
Definition AIPerceptionSystem.h:31
Definition AISenseConfig.h:19
Definition AISense.h:21
Definition ActorComponent.h:152
virtual ENGINE_API void OnRegister()
Definition ActorComponent.cpp:1542
virtual ENGINE_API void OnUnregister()
Definition ActorComponent.cpp:1577
virtual ENGINE_API void PostInitProperties() override
Definition ActorComponent.cpp:592
virtual ENGINE_API void BeginDestroy() override
Definition ActorComponent.cpp:1144
Definition EngineTypes.h:3429
constexpr FVector InvalidLocation
Definition AITypes.h:30
@ false
Definition radaudio_common.h:23
static uint32 GetSize()
Definition AITypes.h:212
Definition AIPerceptionTypes.h:130
bool IsValid() const
Definition AIPerceptionTypes.h:198
bool IsExpired() const
Definition AIPerceptionTypes.h:193
bool WasSuccessfullySensed() const
Definition AIPerceptionTypes.h:192
Definition AIPerceptionComponent.h:183
Definition AIPerceptionComponent.h:50
TWeakObjectPtr< AActor > Target
Definition AIPerceptionComponent.h:51
FAISenseID DominantSense
Definition AIPerceptionComponent.h:57
FVector GetReceiverLocation(const FAISenseID Sense) const
Definition AIPerceptionComponent.h:152
FVector GetLastStimulusLocation(float *OptionalAge=nullptr) const
Definition AIPerceptionComponent.h:73
bool HasAnyCurrentStimulus() const
Definition AIPerceptionComponent.h:123
TArray< FAIStimulus > LastSensedStimuli
Definition AIPerceptionComponent.h:53
uint32 bIsHostile
Definition AIPerceptionComponent.h:60
bool HasKnownStimulusOfSense(const FAISenseID Sense) const
Definition AIPerceptionComponent.h:162
bool HasAnyKnownStimulus() const
Definition AIPerceptionComponent.h:108
FVector GetStimulusLocation(const FAISenseID Sense) const
Definition AIPerceptionComponent.h:142
uint32 bIsFriendly
Definition AIPerceptionComponent.h:63
bool IsSenseActive(const FAISenseID Sense) const
Definition AIPerceptionComponent.h:172
FActorPerceptionInfo(AActor *InTarget=nullptr)
Definition AIPerceptionComponent.h:65
Definition AIPerceptionComponent.h:28
Definition GenericTeamAgentInterface.h:24
Definition AIPerceptionTypes.h:36
Definition VisualLoggerTypes.h:205
Definition ObjectPtr.h:488
Definition WeakObjectPtrTemplates.h:25