Skip to content

Commit

Permalink
fix "InRange", should calculate position relative to Character not Co…
Browse files Browse the repository at this point in the history
…ntroller, improved debug InRange/InAngle
  • Loading branch information
Ciberusps committed Jun 25, 2024
1 parent beab65a commit 04bb02c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ float UBTD_InAngle::GetCurrentAngle(const UBehaviorTreeComponent& OwnerComp, uin
DrawDebugSphere(OwnerComp.GetWorld(), LineStart, 4.0f, 16, FColor::Blue, false, -1, -1, 2.0f);
DrawDebugSphere(OwnerComp.GetWorld(), LineEnd, 4.0f, 16, FColor::Blue, false, -1, -1, 2.0f);
DrawDebugString(OwnerComp.GetWorld(), TextLocation, FString::Printf(TEXT("Angle: %f"), CurrentAngle), nullptr, bInAngle ? FColor::Green : FColor::White, 0, true);
DrawDebugString(OwnerComp.GetWorld(), SelfActor->GetActorLocation(), FString::Printf(TEXT("ParentNode:\n%s"), *GetParentNode()->NodeName), nullptr, FColor::White, 0, true);
DrawDebugString(OwnerComp.GetWorld(), SelfActor->GetActorLocation(), FString::Printf(TEXT("ParentNode:\n%s \n\nNodeName:\n%s"), *GetParentNode()->NodeName, *GetMyNode()->NodeName), nullptr, FColor::White, 0, true);
}

return CurrentAngle;
Expand Down
51 changes: 28 additions & 23 deletions Source/UnrealHelperLibrary/Private/AI/Decorators/BTD_InRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,34 @@ FName UBTD_InRange::GetNodeIconName() const
}
#endif

float UBTD_InRange::GetCurrentDistance(const UBehaviorTreeComponent& OwnerComp, bool bDrawDebug_In) const
float UBTD_InRange::GetCurrentDistance(const UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, bool bDrawDebug_In) const
{
float CurrentDistance = 0.0f;

const UBlackboardComponent* BlackboardComponent = OwnerComp.GetBlackboardComponent();
if (!BlackboardComponent) return CurrentDistance;

FBTInRangeMemory* Memory = CastInstanceNodeMemory<FBTInRangeMemory>(NodeMemory);
if (Memory == nullptr) return CurrentDistance;

AActor* SelfActor = OwnerComp.GetOwner();
AActor* TargetActor = Cast<AActor>(BlackboardComponent->GetValueAsObject(Target.SelectedKeyName));
if (!IsValid(SelfActor) || !IsValid(TargetActor)) return CurrentDistance;

CurrentDistance = SelfActor->GetDistanceTo(TargetActor);
ACharacter* OwnerCharacter = Memory->OwnerCharacter.Get();
if (!IsValid(OwnerCharacter))
{
AController* OwnerCharacterController = Cast<AController>(SelfActor);
OwnerCharacter = IsValid(OwnerCharacterController) ? OwnerCharacterController->GetCharacter() : nullptr;
Memory->OwnerCharacter = OwnerCharacter;
}
CurrentDistance = OwnerCharacter->GetDistanceTo(TargetActor);

ACharacter* OwnerCharacter = nullptr;
ACharacter* TargetCharacter = nullptr;
ACharacter* TargetCharacter = nullptr;

if (bIncludeSelfCapsuleRadius)
if (bIncludeSelfCapsuleRadius && IsValid(OwnerCharacter))
{
// TODO cache OwnerCharacter on start
AController* OwnerCharacterController = Cast<AController>(SelfActor);
OwnerCharacter = IsValid(OwnerCharacterController) ? OwnerCharacterController->GetCharacter() : nullptr;
if(IsValid(OwnerCharacter))
{
CurrentDistance -= OwnerCharacter->GetCapsuleComponent()->GetScaledCapsuleRadius();
}
CurrentDistance -= OwnerCharacter->GetCapsuleComponent()->GetScaledCapsuleRadius();
}

if (bIncludeTargetCapsuleRadius)
Expand All @@ -71,7 +74,7 @@ float UBTD_InRange::GetCurrentDistance(const UBehaviorTreeComponent& OwnerComp,

if (bDrawDebug_In)
{
FVector LineStart = SelfActor->GetActorLocation();
FVector LineStart = OwnerCharacter->GetActorLocation();
FVector LineEnd = TargetActor->GetActorLocation();
FVector TextLocation = (LineEnd - LineStart) / 2 + LineStart;
bool bInRange = UKismetMathLibrary::InRange_FloatFloat(CurrentDistance, Min, Max);
Expand All @@ -83,8 +86,8 @@ float UBTD_InRange::GetCurrentDistance(const UBehaviorTreeComponent& OwnerComp,
OwnerCharacterCapsule->GetScaledCapsuleHalfHeight(),
OwnerCharacterCapsule->GetScaledCapsuleRadius(),
OwnerCharacterCapsule->GetComponentRotation().Quaternion(), FColor::Blue,
false,-1, 0, 2.0f);
LineStart = (LineEnd - LineStart).GetSafeNormal() * OwnerCharacterCapsule->GetScaledCapsuleRadius() + SelfActor->GetActorLocation();
false,DebugLifetime, 0, 2.0f);
LineStart = (LineEnd - LineStart).GetSafeNormal() * OwnerCharacterCapsule->GetScaledCapsuleRadius() + OwnerCharacter->GetActorLocation();
}

if (bIncludeTargetCapsuleRadius && IsValid(TargetCharacter))
Expand All @@ -94,15 +97,15 @@ float UBTD_InRange::GetCurrentDistance(const UBehaviorTreeComponent& OwnerComp,
TargetCharacterCapsule->GetScaledCapsuleHalfHeight(),
TargetCharacterCapsule->GetScaledCapsuleRadius(),
TargetCharacterCapsule->GetComponentRotation().Quaternion(), FColor::Blue,
false,-1, 0, 2.0f);
false,DebugLifetime, 0, 2.0f);
LineEnd = (LineStart - LineEnd).GetSafeNormal() * TargetCharacterCapsule->GetScaledCapsuleRadius() + TargetActor->GetActorLocation();
}

DrawDebugLine(OwnerComp.GetWorld(), LineStart, LineEnd, bInRange ? FColor::Green : FColor::Red, false, -1, -1, 2.0f);
DrawDebugSphere(OwnerComp.GetWorld(), LineStart, 5.0f, 16, FColor::Blue, false, -1, -1, 2.0f);
DrawDebugSphere(OwnerComp.GetWorld(), LineEnd, 5.0f, 16, FColor::Blue, false, -1, -1, 2.0f);
DrawDebugString(OwnerComp.GetWorld(), TextLocation, FString::Printf(TEXT("Distance: %f"), CurrentDistance), nullptr, FColor::White, 0, true);
DrawDebugString(OwnerComp.GetWorld(), SelfActor->GetActorLocation(), FString::Printf(TEXT("ParentNode:\n%s"), *GetParentNode()->NodeName), nullptr, FColor::White, 0, true);
DrawDebugLine(OwnerComp.GetWorld(), LineStart, LineEnd, bInRange ? FColor::Green : FColor::Red, false, DebugLifetime, -1, 2.0f);
DrawDebugSphere(OwnerComp.GetWorld(), LineStart, 5.0f, 16, FColor::Blue, false, DebugLifetime, DebugLifetime, 2.0f);
DrawDebugSphere(OwnerComp.GetWorld(), LineEnd, 5.0f, 16, FColor::Blue, false, DebugLifetime, DebugLifetime, 2.0f);
DrawDebugString(OwnerComp.GetWorld(), TextLocation, FString::Printf(TEXT("Distance: %f"), CurrentDistance), nullptr, FColor::White, DebugLifetime < 0 ? 0 : DebugLifetime, true);
DrawDebugString(OwnerComp.GetWorld(), OwnerCharacter->GetActorLocation(), FString::Printf(TEXT("ParentNode:\n%s \n\nNodeName:\n%s"), *GetParentNode()->NodeName, *GetMyNode()->NodeName), nullptr, FColor::White, DebugLifetime < 0 ? 0 : DebugLifetime, true);
}

return CurrentDistance;
Expand All @@ -117,7 +120,7 @@ void UBTD_InRange::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory

bool UBTD_InRange::CalculateRawConditionValue(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const
{
bool bIsInRange = UKismetMathLibrary::InRange_FloatFloat(GetCurrentDistance(OwnerComp, bDrawDebug), Min, Max);
bool bIsInRange = UKismetMathLibrary::InRange_FloatFloat(GetCurrentDistance(OwnerComp, NodeMemory, bDrawDebug), Min, Max);

return bIsInRange;
}
Expand All @@ -127,6 +130,8 @@ void UBTD_InRange::InitializeMemory(UBehaviorTreeComponent& OwnerComp, uint8* No
{
FBTInRangeMemory* DecoratorMemory = CastInstanceNodeMemory<FBTInRangeMemory>(NodeMemory);
DecoratorMemory->CurrentDistance = 0.0f;
DecoratorMemory->OwnerCharacter = nullptr;
DecoratorMemory->TargetCharacter = nullptr;
}

void UBTD_InRange::CleanupMemory(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory,
Expand Down Expand Up @@ -154,5 +159,5 @@ void UBTD_InRange::DescribeRuntimeValues(const UBehaviorTreeComponent& OwnerComp
{
Super::DescribeRuntimeValues(OwnerComp, NodeMemory, Verbosity, Values);
bool bInRange = false;
Values.Add(FString::Printf(TEXT("CurrentDistance: %f"), GetCurrentDistance(OwnerComp)));
Values.Add(FString::Printf(TEXT("CurrentDistance: %f"), GetCurrentDistance(OwnerComp, NodeMemory)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
struct FBTInRangeMemory
{
float CurrentDistance = 0.0f;
TWeakObjectPtr<ACharacter> OwnerCharacter;
TWeakObjectPtr<ACharacter> TargetCharacter;
};

/**
Expand Down Expand Up @@ -42,6 +44,9 @@ class UNREALHELPERLIBRARY_API UBTD_InRange : public UBTD_Base
bool bIncludeTargetCapsuleRadius = true;
UPROPERTY(Category="Decorator", EditAnywhere)
bool bDrawDebug = false;
// TODO: if -1 or 0, when should be overriden by UHLGlobalSettings.DebugLifeTime, by default should be -1 or 0?
UPROPERTY(Category="Decorator", EditAnywhere, meta=(EditCondition="bDrawDebug", EditConditionHides))
float DebugLifetime = -1.0f;

virtual bool CalculateRawConditionValue(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const override;

Expand All @@ -55,7 +60,7 @@ class UNREALHELPERLIBRARY_API UBTD_InRange : public UBTD_Base
virtual FName GetNodeIconName() const override;
#endif

float GetCurrentDistance(const UBehaviorTreeComponent& OwnerComp, bool bDrawDebug_In = false) const;
float GetCurrentDistance(const UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, bool bDrawDebug_In = false) const;

virtual void TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds) override;
};

0 comments on commit 04bb02c

Please sign in to comment.