Is there a more efficient way to use VInterpTo than my implementation?

I’m building a simple free flow combat system, where the character always covers the distance between their current location, and an enemy’s position, in a set period of time. I’m using VInterpTo to do this, but since it needs to run every frame, I can’t simply call it once and forget about it. Currently I’m using a bool to check if I should be moving towards an enemy, and running this in tick:


void AMCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	if (bIsAttacking){
		float InterpSpeed = 0.5f;
		destinationLocation = FMath::VInterpTo(myActor->GetActorLocation(), endLocation, FrameDeltaTime, InterpSpeed);
		myCharacter->SetActorLocation(destinationLocation, true);
	}
}

It works, but it’s super-wasteful, since IsAttacking will only be true a tiny percent of the game’s total runtime- is there a better way to do this, which doesn’t involve checking a bool every single frame the game is running?

You could create a Timer to call a function on loop, and when it’s over clear the Timer again. Just use the delay between timers as the ‘FrameTimeDelta’. though it may not be ‘perfectly’ smooth it’ll still probably hold up okay if your framerate is consistent.

Honestly though. I’m not sure if that would be more or less efficient than just checking a bool every frame (which isn’t too bad in the grand scheme of things). Sometimes the simplest ways are the best :stuck_out_tongue:

Alright, I’ll give it a try and compare… thank you for the advice :slight_smile:

I had the same problem, for example I needed to wait until a variable has replicated the first time but it would be not as nice to always check a boolean in the tick function.

so I created a TickEvent


// .h
FTickEvent TickEvent;


void AMCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	TickEvent.Broadcast(DeltaTime);
}

Then I create some work function and subscribe it to the TickEvent and when it’s done it will unsubscribe itself.

Oooh, that is really slick- I haven’t done the numbers, but I’m pretty sure that gets faster than checking a single bool the instant you have more than one frame-dependent function that doesn’t always need to be on.