I am trying to create my async node based on UBlueprintAsyncActionBase. When I run this code Engine crashes. It says problem is in Activate function. I think the reason is using timer. Can you help me about what I can do? Thanks for answering.
header
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintAsyncActionBase.h"
#include "TurnInPlace.generated.h"
/**
*
*/
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FResponsDeleget);
UCLASS()
class PROJECTSM_MAINCODE_API UTurnInPlace : public UBlueprintAsyncActionBase
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintAssignable)
FResponsDeleget OnFinished;
FTimerHandle rotatorHandle;
AActor* selfTemp;
AActor* targetTemp;
float turningRateTemp;
UFUNCTION(BlueprintCallable, meta = (BlueprintInternalUseOnly = "true"))
static UTurnInPlace* TurnInPlaceTask(AActor* Target, AActor* SelfTarget, float turningRate);
virtual void Activate() override;
void RotateCharacter();
};
cpp
#include "TurnInPlace.h"
#include "Engine/World.h"
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/GameplayStatics.h"
UTurnInPlace* UTurnInPlace::TurnInPlaceTask(AActor* Target, AActor* SelfTarget, float turningRate)
{
UTurnInPlace* TurnInPlaceTask = NewObject<UTurnInPlace>();
TurnInPlaceTask->targetTemp = Target;
TurnInPlaceTask->selfTemp = SelfTarget;
TurnInPlaceTask->turningRateTemp = turningRate;
return TurnInPlaceTask;
}
void UTurnInPlace::Activate()
{
selfTemp->GetWorld()->GetTimerManager().SetTimer(rotatorHandle, this, &UTurnInPlace::RotateCharacter, 0.1f, true, 0.1f);
}
void UTurnInPlace::RotateCharacter()
{
const FVector currentLocation = FVector(selfTemp->GetActorLocation().X,selfTemp->GetActorLocation().Y,0);
const FVector targetLocation = FVector(targetTemp->GetActorLocation().X,targetTemp->GetActorLocation().Y,0);
const FRotator currentRotation = selfTemp->GetActorRotation();
const FRotator targetRotation = UKismetMathLibrary::FindLookAtRotation(currentLocation, targetLocation);
FRotator returnRotation = FMath::RInterpTo(currentRotation, targetRotation, GetWorld()->GetDeltaSeconds(), turningRateTemp);
selfTemp->SetActorRotation(returnRotation);
if (FMath::IsNearlyEqual(currentRotation.Yaw, targetRotation.Yaw, 1.0f))
{
OnFinished.Broadcast();
selfTemp->GetWorld()->GetTimerManager().ClearTimer(rotatorHandle);
}
//RemoveFromRoot();
}