When I call SetTimer, it finishes immediately, even if inRate is set to a very large value. I have tried debugging a bit, but my limited UE knowledge doesnt get me far.
My code (simplified):
//PickupBase.h
class ADVENTUREGAME_API APickupBase : public AActor
{
GENERATED_BODY()
public:
APickupBase();
void InitializePickup();
protected:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Pickup | Respawn")
float RespawnTime = 4.0f;
FTimerHandle RespawnTimerHandle;
//PickupBase.cpp
void APickupBase::OnSphereBeginOverlap( ... )
{
GetWorldTimerManager().SetTimer(RespawnTimerHandle, this, &APickupBase::InitializePickup, RespawnTime, false, 0);
}
For context, I am following the “Code a First-Person Adventure Game” tutorial, specifically the “Create a Respawning Pickup Item” part. Both PickupBase files are copy-pasted from the course. The other files from the course I wrote by hand, so there are minor differences, but I checked that everything worked at every step of the way.
The only unintended(?) behaviour I have seen is that I need to re-open a data table after restarting the editor in order for actors to be able to get their data from it.
Thanks in advance!
EDIT: I was able to reproduce this in a blank level
Code:
//TestActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Test_Actor.generated.h"
UCLASS()
class TIMERTEST_API ATest_Actor : public AActor
{
GENERATED_BODY()
public:
ATest_Actor();
protected:
virtual void BeginPlay() override;
FTimerHandle TestTimerHandle;
public:
virtual void Tick(float DeltaTime) override;
};
//TestActor.cpp
#include "Test_Actor.h"
ATest_Actor::ATest_Actor()
{
PrimaryActorTick.bCanEverTick = true;
}
void ATest_Actor::BeginPlay()
{
check(GEngine != nullptr);
Super::BeginPlay();
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Hi"));
GetWorldTimerManager().SetTimer(TestTimerHandle, this, &ATest_Actor::BeginPlay, 5.0f, false, 0);
}
void ATest_Actor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
I then added a single instance of this class into the world.
Instead of displaying a message every 5s, it spams debug messages on game start.
I have tried using a blueprint derived class, a c++ derived class, the class itself, and clearing the timer before setting it, but the behavior did not change in any of these cases.