Unreal Engine/etc.

[Unreal Engine 4 etc.] GlobalTimeDilation과 Tick, Timer

한돌이 2023. 8. 16. 15:22
// TestComponent.h

UCLASS()
class PROJECTREPLICA_API UTestComponent : public UActorComponent
{
	GENERATED_BODY()

public:
	UTestComponent();

public:
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

public:
	UFUNCTION(BlueprintCallable, Category = "Test")
	void ActivateTimer();

private:
	FTimerHandle Timer;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "test", meta = (AllowPrivateAccess = "true"))
	bool bActivateTimer;	

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "test", meta = (AllowPrivateAccess = "true"))
	float Countdown;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "test", meta = (AllowPrivateAccess = "true"))
	float TickCountdown;
};
// TestComponent.cpp

UTestComponent::UTestComponent()
{
	PrimaryComponentTick.bCanEverTick = true;
	
	bActivateTimer = false;
	Countdown = 0.0f;
	TickCountdown = 0.0f;
}

void UTestComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	if(bActivateTimer)
	{
		PR_LOG_SCREEN_INFO(0, "TickCountdown: %f", TickCountdown);
		PR_LOG_SCREEN_INFO(1, "Countdown: %f", Countdown);
		TickCountdown += DeltaTime;

		if(TickCountdown > 10.0f)
		{
			PR_LOG_SCREEN("Finish TickCountdown");
		}
	}
}

void UTestComponent::ActivateTimer()
{
	PR_LOG_SCREEN("activate");
	
	if(IsValid(GetOwner()) == true)
	{
		if(bActivateTimer == false)
		{
			bActivateTimer = true;
		}
		
		GetOwner()->GetWorldTimerManager().SetTimer(Timer, FTimerDelegate::CreateLambda([&]()
		{
			Countdown++;

			if(Countdown > 10.0f)
			{
				GetOwner()->GetWorldTimerManager().ClearTimer(Timer);
				PR_LOG_SCREEN("Finish Countdown");
			}
		}), 1.0f, true);
	}
}

 

 

 Tick은 GlobalTimeDilation에 영향을 받지 않아 TimeStop 기능을 구현할 때 유용하게 쓰일것으로 보인다.