Delay in C++

I’m trying to implement a delay between when a variable for a characters state (in this case, IsFiring/IsReloading) and I’m having a bit of trouble with setting up a delay in C++. I’ve looked at the possibility of using a timer, but I’m not sure that might be the right implementation. As far as I can tell there is not a similar function in the API for C++ like the Delay node in Blueprint. How would I go about adding a delay to my code?

void AAirwaveCharacter::OnReload()
{
	if (!IsReloading)
	{
		IsReloading = true;

		if (Weapon != NULL)
		{
			Weapon->Reload();
			//delay here
			IsReloading = false;
		}
	}
}

Unfortunately, this isn’t possible in standard C++

To do this, functions would need to be ‘resumable’.

A timer is probably where I’d go for this one, while another possibility is to query every so often (eg. on tick, if the amount of time has elapsed, do something).

Hm, I was afraid of that. Though I think I have an idea of how to make this possibly work with a timer by creating a function to change the state for IsRelading to call from the timer.

Thanks for your input!

it’s not so true. this is a native C++ code:

 #include <thread>   

 std::this_thread::sleep_for(std::chrono::milliseconds(420)); //c++11

this however should not be used in UE4, because it will freeze the whole thread. UE4 has it’s own timers mechanism.

look at timer in http://www.tomlooman.com/using-timers-in-ue4/

FTimerHandle ReloadHandle;//you typically define it as member variable

//simple use of lambda
GetWorld()->GetTimerManager().SetTimer(ReloadHandle, [this]() {
	IsReloading = false;
}, Delay, false);

//this one checks the validity of a UObject* so that its not called on a dangling pointer
GetWorld()->GetTimerManager().SetTimer(ReloadHandle, FTimerDelegate::CreateWeakLambda(this, [this]() {
	IsReloading = false;
}), Delay, false);

//you can also bind to a member function 
GetWorld()->GetTimerManager().SetTimer(ReloadHandle, this, &AMyActor::ReloadFinished, Delay, false);
1 Like

Ty, I didn’t event know that i can use Lambda expressions in UE4 :slight_smile:

As Arty said exists a native C++ function for delay,but UE4 use C++ and give to the developers a different language based on C++ so you only can use UE4 functions like SetTimer();

This is not equivalent, in fact UE4 got this function wrapped too which you can use in UE4 which calls that function anyway:

This function tells OS (yes this function is just a wrapper for proper OS function call) to not schedule thread to CPU for specific amount of time, it does not delay, entirely stops the thread on OS level (which as you notice with UE4 is not good idea to do this in main thread :p). So technically not even C++ feature, but it can be emulated if platform don’t support thread sleeping, but thats not concern of C++ standard as it only tells what behavior should be when you call a function it doesn’t care how it’s done.

Standard C++ does not have proper delay implementation that does not lock the thread, primerly because C++ standard don’t want to impose specific application designs, that why applications need to create there own systems based on how they been designed. And looping programs like UE4 it’s usually, list the actions to do on specific time, check the clock (not system clock, theres more reliable clocks in OSs) against listed actions on each loop, if time for listed action passes, execute. And thats how UE4 is doing with Timer System, can be also done with Latent Action system which Delay nodes are using, can be also used for “wait for” operations, but it kind of messy to use it in C++.

1 Like

Delay blueprint node does not delay inline, it actually sets a timer and then exits current scope (which is probably why it isn’t allowed inside functions). Otherwise it would freeze your entire game until the delay was over.

You should mark this as answered as its the only real way to do it in c++