Having trouble reconciling Timer syntax with docs

I already have a working UseItem function, and I’m trying to require the player to hold the Use key, by setting a timer with UseKeyPressed, cancelling the timer with UseKeyReleased, and executing UseItem if the timer finishes successfully. I’m trying to mimic the syntax from Gameplay Timers | Unreal Engine Documentation , but it isn’t compiling:

void AMCharacter::UseKeyPressed(){
	GetWorldTimerManager().SetTimer(this, &AMCharacter::UseAction, 1.0f, true);	
}

void AMCharacter::UseKeyReleased(){
	GetWorldTimerManager().ClearTimer(this, &AMCharacter::UseAction);
}

The two errors I’m getting make me think I’m giving the wrong argument to the ClearTimer method: ‘FTimerManager::ClearTimer’ : function does not take 2 arguments
‘void FTimerManager::SetTimer(FTimerHandle &,float,bool,float)’ : cannot convert argument 1 from 'AMCharacter *const ’ to ‘FTimerHandle &’

However, that doesn’t make sense to me- if I’m reading these correctly, FTimerHandle wants a reference to itself and no reference to the function I want the timer to run, and that doesn’t seem right.

The C++ documentation is pretty outdated on some docs…

You can’t (or shouldn’t) use SetTimer() like that anymore.

You’ll need both a FTimerHandle and a FTimerDelegate to provide for the SetTimer method.

FTimerHandle myTimerHandle; ///<- Sets up the timer handle

const FTimerDelegate myTimerDelegate = FTimerDelegate::CreateUObject(this, &AMyCharacter::MyFunction); ///<- Sets up the Timer Delegate.

Then use SetTimer like this:

float loopingTimeInSeconds = 1.f; ///<- lets the timer call the function once per second.

GetWorldTimerManager().SetTimer(myTimerHandle, myTimerDelegate, loopingTimeInSeconds, true); ///<- Sets up the timer with the created Handle and Delegate to fire as often as specified in loopingTimeInSeconds and set to loop with true.

You can then clear the timer like this:

GetWorldTimerManager().ClearTimer(myTimerHandle);

Here’s the code i use for fireing a weapon in the game i’m working on:

void ABaseWeapon::PullTrigger()
{
	if (!isFireing)
	{
		Fire();
		const FTimerDelegate FireDelegate = FTimerDelegate::CreateUObject(this, &ABaseWeapon::Fire);
		GetWorldTimerManager().SetTimer(FireTimer, FireDelegate, (1.f / ShootsPerSecond), true);
		
		isFireing = true;
	}
}


void ABaseWeapon::ReleaseTrigger()
{
	if (isFireing)
	{
		GetWorldTimerManager().ClearTimer(FireTimer);
		isFireing = false;
	}
}

Hope this helps!

That does the trick, thank you! :slight_smile:

Thanks you! :slight_smile: