I have a function I use to for soft target lock which iterates through a list of nearby objects and guesses which one you’re trying to use based on distance and proximity to screen center. It’s very slightly expensive, so I want to ensure that it’s only called on frames where the player has either physically moved, or moved the camera. Right now I’m doing it like this:
InputComponent->BindAxis("Turn", this, &AMCharacter::RequestAttention);
InputComponent->BindAxis("Lookup", this, &AMCharacter::RequestAttention);
InputComponent->BindAxis("MoveForward", this, &AMCharacter::RequestAttention);
InputComponent->BindAxis("MoveRight", this, &AMCharacter::RequestAttention);
void AMCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
UpdateAttention();
}
void AMCharacter::UpdateAttention(){
if (bCheckSurroundings){
characterPerception->SetAttentionTarget(); //Calculate soft lock
bCheckSurroundings = false;
}
}
void AMCharacter::RequestAttention(float Value){
if (Value != 0) //If the provided axis is recieving input...
bCheckSurroundings = true; //request a new soft lock test
}
This works, but it’s really sloppy and wasteful: I check four axes every frame, and if the player is moving and looking around at the same time, which he pretty much always will be, I set the same boolean four times per frame, three of which are redundant. Is there any better way to detect when movement/mouselook is occurring, and only make a single call to SetAttentionTarget on that frame?