Skip to content

Commit 04bb02c

Browse files
committed
fix "InRange", should calculate position relative to Character not Controller, improved debug InRange/InAngle
1 parent beab65a commit 04bb02c

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

Source/UnrealHelperLibrary/Private/AI/Decorators/BTD_InAngle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ float UBTD_InAngle::GetCurrentAngle(const UBehaviorTreeComponent& OwnerComp, uin
141141
DrawDebugSphere(OwnerComp.GetWorld(), LineStart, 4.0f, 16, FColor::Blue, false, -1, -1, 2.0f);
142142
DrawDebugSphere(OwnerComp.GetWorld(), LineEnd, 4.0f, 16, FColor::Blue, false, -1, -1, 2.0f);
143143
DrawDebugString(OwnerComp.GetWorld(), TextLocation, FString::Printf(TEXT("Angle: %f"), CurrentAngle), nullptr, bInAngle ? FColor::Green : FColor::White, 0, true);
144-
DrawDebugString(OwnerComp.GetWorld(), SelfActor->GetActorLocation(), FString::Printf(TEXT("ParentNode:\n%s"), *GetParentNode()->NodeName), nullptr, FColor::White, 0, true);
144+
DrawDebugString(OwnerComp.GetWorld(), SelfActor->GetActorLocation(), FString::Printf(TEXT("ParentNode:\n%s \n\nNodeName:\n%s"), *GetParentNode()->NodeName, *GetMyNode()->NodeName), nullptr, FColor::White, 0, true);
145145
}
146146

147147
return CurrentAngle;

Source/UnrealHelperLibrary/Private/AI/Decorators/BTD_InRange.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,34 @@ FName UBTD_InRange::GetNodeIconName() const
3232
}
3333
#endif
3434

35-
float UBTD_InRange::GetCurrentDistance(const UBehaviorTreeComponent& OwnerComp, bool bDrawDebug_In) const
35+
float UBTD_InRange::GetCurrentDistance(const UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, bool bDrawDebug_In) const
3636
{
3737
float CurrentDistance = 0.0f;
3838

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

42+
FBTInRangeMemory* Memory = CastInstanceNodeMemory<FBTInRangeMemory>(NodeMemory);
43+
if (Memory == nullptr) return CurrentDistance;
44+
4245
AActor* SelfActor = OwnerComp.GetOwner();
4346
AActor* TargetActor = Cast<AActor>(BlackboardComponent->GetValueAsObject(Target.SelectedKeyName));
4447
if (!IsValid(SelfActor) || !IsValid(TargetActor)) return CurrentDistance;
4548

46-
CurrentDistance = SelfActor->GetDistanceTo(TargetActor);
49+
ACharacter* OwnerCharacter = Memory->OwnerCharacter.Get();
50+
if (!IsValid(OwnerCharacter))
51+
{
52+
AController* OwnerCharacterController = Cast<AController>(SelfActor);
53+
OwnerCharacter = IsValid(OwnerCharacterController) ? OwnerCharacterController->GetCharacter() : nullptr;
54+
Memory->OwnerCharacter = OwnerCharacter;
55+
}
56+
CurrentDistance = OwnerCharacter->GetDistanceTo(TargetActor);
4757

48-
ACharacter* OwnerCharacter = nullptr;
49-
ACharacter* TargetCharacter = nullptr;
58+
ACharacter* TargetCharacter = nullptr;
5059

51-
if (bIncludeSelfCapsuleRadius)
60+
if (bIncludeSelfCapsuleRadius && IsValid(OwnerCharacter))
5261
{
53-
// TODO cache OwnerCharacter on start
54-
AController* OwnerCharacterController = Cast<AController>(SelfActor);
55-
OwnerCharacter = IsValid(OwnerCharacterController) ? OwnerCharacterController->GetCharacter() : nullptr;
56-
if(IsValid(OwnerCharacter))
57-
{
58-
CurrentDistance -= OwnerCharacter->GetCapsuleComponent()->GetScaledCapsuleRadius();
59-
}
62+
CurrentDistance -= OwnerCharacter->GetCapsuleComponent()->GetScaledCapsuleRadius();
6063
}
6164

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

7275
if (bDrawDebug_In)
7376
{
74-
FVector LineStart = SelfActor->GetActorLocation();
77+
FVector LineStart = OwnerCharacter->GetActorLocation();
7578
FVector LineEnd = TargetActor->GetActorLocation();
7679
FVector TextLocation = (LineEnd - LineStart) / 2 + LineStart;
7780
bool bInRange = UKismetMathLibrary::InRange_FloatFloat(CurrentDistance, Min, Max);
@@ -83,8 +86,8 @@ float UBTD_InRange::GetCurrentDistance(const UBehaviorTreeComponent& OwnerComp,
8386
OwnerCharacterCapsule->GetScaledCapsuleHalfHeight(),
8487
OwnerCharacterCapsule->GetScaledCapsuleRadius(),
8588
OwnerCharacterCapsule->GetComponentRotation().Quaternion(), FColor::Blue,
86-
false,-1, 0, 2.0f);
87-
LineStart = (LineEnd - LineStart).GetSafeNormal() * OwnerCharacterCapsule->GetScaledCapsuleRadius() + SelfActor->GetActorLocation();
89+
false,DebugLifetime, 0, 2.0f);
90+
LineStart = (LineEnd - LineStart).GetSafeNormal() * OwnerCharacterCapsule->GetScaledCapsuleRadius() + OwnerCharacter->GetActorLocation();
8891
}
8992

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

101-
DrawDebugLine(OwnerComp.GetWorld(), LineStart, LineEnd, bInRange ? FColor::Green : FColor::Red, false, -1, -1, 2.0f);
102-
DrawDebugSphere(OwnerComp.GetWorld(), LineStart, 5.0f, 16, FColor::Blue, false, -1, -1, 2.0f);
103-
DrawDebugSphere(OwnerComp.GetWorld(), LineEnd, 5.0f, 16, FColor::Blue, false, -1, -1, 2.0f);
104-
DrawDebugString(OwnerComp.GetWorld(), TextLocation, FString::Printf(TEXT("Distance: %f"), CurrentDistance), nullptr, FColor::White, 0, true);
105-
DrawDebugString(OwnerComp.GetWorld(), SelfActor->GetActorLocation(), FString::Printf(TEXT("ParentNode:\n%s"), *GetParentNode()->NodeName), nullptr, FColor::White, 0, true);
104+
DrawDebugLine(OwnerComp.GetWorld(), LineStart, LineEnd, bInRange ? FColor::Green : FColor::Red, false, DebugLifetime, -1, 2.0f);
105+
DrawDebugSphere(OwnerComp.GetWorld(), LineStart, 5.0f, 16, FColor::Blue, false, DebugLifetime, DebugLifetime, 2.0f);
106+
DrawDebugSphere(OwnerComp.GetWorld(), LineEnd, 5.0f, 16, FColor::Blue, false, DebugLifetime, DebugLifetime, 2.0f);
107+
DrawDebugString(OwnerComp.GetWorld(), TextLocation, FString::Printf(TEXT("Distance: %f"), CurrentDistance), nullptr, FColor::White, DebugLifetime < 0 ? 0 : DebugLifetime, true);
108+
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);
106109
}
107110

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

118121
bool UBTD_InRange::CalculateRawConditionValue(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const
119122
{
120-
bool bIsInRange = UKismetMathLibrary::InRange_FloatFloat(GetCurrentDistance(OwnerComp, bDrawDebug), Min, Max);
123+
bool bIsInRange = UKismetMathLibrary::InRange_FloatFloat(GetCurrentDistance(OwnerComp, NodeMemory, bDrawDebug), Min, Max);
121124

122125
return bIsInRange;
123126
}
@@ -127,6 +130,8 @@ void UBTD_InRange::InitializeMemory(UBehaviorTreeComponent& OwnerComp, uint8* No
127130
{
128131
FBTInRangeMemory* DecoratorMemory = CastInstanceNodeMemory<FBTInRangeMemory>(NodeMemory);
129132
DecoratorMemory->CurrentDistance = 0.0f;
133+
DecoratorMemory->OwnerCharacter = nullptr;
134+
DecoratorMemory->TargetCharacter = nullptr;
130135
}
131136

132137
void UBTD_InRange::CleanupMemory(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory,
@@ -154,5 +159,5 @@ void UBTD_InRange::DescribeRuntimeValues(const UBehaviorTreeComponent& OwnerComp
154159
{
155160
Super::DescribeRuntimeValues(OwnerComp, NodeMemory, Verbosity, Values);
156161
bool bInRange = false;
157-
Values.Add(FString::Printf(TEXT("CurrentDistance: %f"), GetCurrentDistance(OwnerComp)));
162+
Values.Add(FString::Printf(TEXT("CurrentDistance: %f"), GetCurrentDistance(OwnerComp, NodeMemory)));
158163
}

Source/UnrealHelperLibrary/Public/AI/Decorators/BTD_InRange.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
struct FBTInRangeMemory
1212
{
1313
float CurrentDistance = 0.0f;
14+
TWeakObjectPtr<ACharacter> OwnerCharacter;
15+
TWeakObjectPtr<ACharacter> TargetCharacter;
1416
};
1517

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

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

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

58-
float GetCurrentDistance(const UBehaviorTreeComponent& OwnerComp, bool bDrawDebug_In = false) const;
63+
float GetCurrentDistance(const UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, bool bDrawDebug_In = false) const;
5964

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

0 commit comments

Comments
 (0)