Skip to content

Instantly share code, notes, and snippets.

@rocifier
Created December 12, 2025 11:09
Show Gist options
  • Select an option

  • Save rocifier/328258526feb05d74f9431ccf2381f61 to your computer and use it in GitHub Desktop.

Select an option

Save rocifier/328258526feb05d74f9431ccf2381f61 to your computer and use it in GitHub Desktop.
#include "STT_SetIsPlayerInSight.h"
EStateTreeRunStatus FSTT_SetIsPlayerInSight::Tick(FStateTreeExecutionContext& Context, const float DeltaTime) const
{
FInstanceDataType& InstanceData = Context.GetInstanceData(*this);
if (!IsValid(InstanceData.AIController))
{
return EStateTreeRunStatus::Failed;
}
InstanceData.bIsPlayerInSight = false;
const UAIPerceptionComponent* PerceptionComponent = InstanceData.AIController->GetAIPerceptionComponent();
if (!IsValid(PerceptionComponent) || !IsValid(InstanceData.PlayerActor))
{
return EStateTreeRunStatus::Failed;
}
TArray<AActor*> CurrentlyPerceivedActors;
PerceptionComponent->GetCurrentlyPerceivedActors(UAISense_Sight::StaticClass(), CurrentlyPerceivedActors);
if (CurrentlyPerceivedActors.Contains(InstanceData.PlayerActor))
{
InstanceData.bIsPlayerInSight = true;
}
float LastStimulusAge = PerceptionComponent->GetYoungestStimulusAge(*InstanceData.PlayerActor);
if (LastStimulusAge >= 0.f && LastStimulusAge < 0.05f)
{
InstanceData.bIsPlayerInSight = true;
}
return EStateTreeRunStatus::Running;
}
// STT_CheckPlayerSightTask.h
#pragma once
#include "CoreMinimal.h"
#include "StealthAI.h"
#include "StateTreeExecutionContext.h"
#include "Tasks/StateTreeAITask.h"
#include "AIController.h"
#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISense_Sight.h"
#include "STT_SetIsPlayerInSight.generated.h"
USTRUCT()
struct FSTT_SetIsPlayerInSightInstanceData
{
GENERATED_BODY()
// Input: AI Controller of the pawn
UPROPERTY(EditAnywhere, Category = "Context")
TObjectPtr<AAIController> AIController = nullptr;
// Input: The player actor to check sight of
UPROPERTY(EditAnywhere, Category = "Input")
TObjectPtr<AActor> PlayerActor = nullptr;
// Output: Whether the player is currently or recently in sight
UPROPERTY(EditAnywhere, Category = "Output")
bool bIsPlayerInSight = false;
};
USTRUCT(meta = (DisplayName = "STTask Set Is Player In Sight"))
struct FSTT_SetIsPlayerInSight : public FStateTreeAITaskBase
{
GENERATED_BODY()
using FInstanceDataType = FSTT_SetIsPlayerInSightInstanceData;
virtual const UStruct* GetInstanceDataType() const override { return FInstanceDataType::StaticStruct(); }
STEALTHAI_API virtual EStateTreeRunStatus Tick(FStateTreeExecutionContext& Context, const float DeltaTime) const override;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment