Hello everyone
I am trying to implement a simple slap functionality using a timer. It happens that when I try to run the SetTimer function, the delegate that I passed to the argument isn’t executing what is inside it.
Here is my .h file
UCLASS()
class PRACTICE_API ABP_FirstPersonCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
ABP_FirstPersonCharacter();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Enhanced Input")
UInputMappingContext* InputMappingContext;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Enhanced Input")
UInputAction* SlapAction;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
void TriggerSlap(const FInputActionValue& Value);
UFUNCTION(BlueprintCallable)
void StartHittingTimer();
UFUNCTION(BlueprintCallable)
void StopHittingTimer();
UFUNCTION()
void HitPlayer();
FTimerHandle TimerHandle;
UPROPERTY(EditAnywhere, Category="Slap")
float SlapDelay;
UPROPERTY(EditAnywhere, Category="Slap")
UAnimMontage* SlapAnimMontage;
};
And here is my .cpp file
ABP_FirstPersonCharacter::ABP_FirstPersonCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ABP_FirstPersonCharacter::BeginPlay()
{
Super::BeginPlay();
if (APlayerController* PC = Cast<APlayerController>(GetController()))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
{
Subsystem->AddMappingContext(InputMappingContext, 0);
}
}
}
// Called every frame
void ABP_FirstPersonCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ABP_FirstPersonCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
EnhancedInputComponent->BindAction(SlapAction, ETriggerEvent::Triggered, this, &ABP_FirstPersonCharacter::TriggerSlap);
}
}
void ABP_FirstPersonCharacter::TriggerSlap(const FInputActionValue& Value)
{
PlayAnimMontage(SlapAnimMontage, 0.8f);
}
void ABP_FirstPersonCharacter::HitPlayer()
{
FHitResult Hit;
FVector start = GetMesh()->GetSocketLocation("clavicle_r");
FVector end = GetMesh()->GetSocketLocation("RightHandMiddle4");
UE_LOG(LogTemp, Display, TEXT("Loop"));
FCollisionQueryParams TraceParams;
GetWorld()->LineTraceSingleByChannel(Hit, start, end, ECC_Visibility, TraceParams);
DrawDebugLine(GetWorld(), start, end, FColor::Red, false, 2.0f);
}
void ABP_FirstPersonCharacter::StartHittingTimer()
{
GetWorldTimerManager().SetTimer(TimerHandle, this, &ABP_FirstPersonCharacter::HitPlayer, 1.0f, true);
UE_LOG(LogTemp, Display, TEXT("Test"))
}
void ABP_FirstPersonCharacter::StopHittingTimer()
{
GetWorldTimerManager().ClearTimer(TimerHandle);
}
I tried adding some logs to see exactly what is executing, and the log within the HitPlayer function is not executing while the log after calling set timer is executing properly.
I’m kinda new to cpp programming in unreal engine so I am kind of lost and have literally no idea what should be wrong.
Thank you all guys in advance, I’m hoping that you could give me some help here