How to Create Custom Timers?

I added Tick() to my character for health and stamina regeneration ect. It appears that the Tick() function ticks very quickly, almost every 1/10 second. I was wondering if anyone knew exactly how fast it ticks and if it is possible to change it to tick once every second?

MyGameCharacter.h

virtual void Tick(float DeltaTime) OVERRIDE;

MyGameCharacter.cpp

AMyGameCharacter::AMyGameCharacter(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
	PrimaryActorTick.bCanEverTick = true;

	// Set size for collision capsule
	CapsuleComponent->InitCapsuleSize(42.f, 96.0f);

	// set our turn rates for input
	BaseTurnRate = 45.f;
	BaseLookUpRate = 45.f;

	Health = 1000.0f;
	Stamina = 100.0f;
	Energy = 100.0f;
	Shields = 0.0f;

	IsSprinting = false;
    
    ...

}

void AMyGameCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	if (IsSprinting == true)
	{
		Stamina -= 0.2f;
		if (Stamina <= 0)
		{
			EndSprint();
		}
	}
	if (Stamina < 100)
	{
		Stamina += 0.1f;
	}
}

The Tick function executes once per frame regardless of your framerate. The higher your framerate the more ticks you will get per second. You may want to look at using a Timer that you set up in the BeginPlay function.

This page may give you some useful information: Gameplay Timers in Unreal Engine | Unreal Engine 5.1 Documentation

@ Marc Audy Thanks I got it working perfectly! The example shows using the GetWorldTimerManager(). I was wondering how I would go about creating custom timers instead of using that one for everything?

Tick is called every frame.

Use a variable to store the last regeneration time stamp.
Eg. in your class, define a few variables like

float LastHealthRegenerationTime;
float HealthRegenerationInterval = 1.0F; // Seconds between each regeneration.

and in your tick:

float gameTime = this->()->GetTimeSeconds();
if (gameTime > this->LastHealthRegenerationTime + HealthRegenerationInterval)
{
    this->LastHealthRegenerationTime = gameTime;
    // Regeneration code here.
}

I am trying to create multiple times, In the long run I will at least a bakers dozen. I was wondering why this doesn’t work.
.h file

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Pawn)
		TSubobjectPtr<class FTimerManager> StaminaRegen;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Pawn)
		TSubobjectPtr<class FTimerManager> SprintingFatigue;

.cpp file

StaminaRegen().SetTimer(this, &AMyGameCharacter::RegenerateStamina, 1.0f, true);
SprintingFatigue().SetTimer(this, &AMyGameCharacter::SprintFatigue, 1.0f, true);

I am getting an error in the .cpp file when I try to set the timers. it says

	12	IntelliSense: call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type	c:\Users\jeffd_000\Documents\Unreal Projects\MyGame\Source\MyGame\MyGameCharacter.cpp	60	2	MyGame

You just need one timermenager instance which is actually generated by the engine (you can get it via GetWorldTimerManager()
). Individual timers are assigned to function pointer (2nd argument) to each individual object (1st argument), note that operational functions in timer manager always ask you for those two, they need that to know which timer you refereeing to.

Also if you want to comment to anwser use comment don’t create your on anwser which is extra question :stuck_out_tongue: