FindInputComponent and Pointer Protection Process

For the Escape game, I have a question about Pointer Protection in the SetupInputComponent function. The instructor says that it is not necessary to initialize the variable InputComponent as a nullptr because it is being declared in the SetupInputComponent function. If that is so, then why do we need to check whether a input component is declared if we are certain it will. Does every actor have an input component? In the function, why do we need to check if there is an input component and it is not null before we “BindAction.” I understand we need to check for the Physics Handle component because not every actor has it. There is a risk of it of Unreal crashing because there is no component but when we initialize the input component, I believe it works every time because all actors have input components? Here is code for reference :

void UGrabber::SetupInputComponent()
{
	InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
	if (InputComponent)
	{
		InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
		InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
	}
}

My question is that do we need the if statement in this scenario?

Blockquote

Yes but it can be nullptr.

Privacy & Terms