@@ -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\n NodeName: \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
118121bool 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
132137void 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}
0 commit comments