Casted Object not recognized in TMap until GetClass()/GetName() is called?

Hi,

May you be great.

Wrap your checks separetably, I mean:

UBoxComponent* HitBox1 = Cast<UBoxComponent>(HitResult.GetComponent());

if (HitBox1)
{
	HitBox1->GetClass(); // Or HitBox1->GetName() also works
}

if (HitBox1)
{
	if (HitboxMap.Contains(HitBox1))
	{
	}
}

UBoxComponent* HitBox2 = Cast<UBoxComponent>(HitResult.GetComponent());
if (HitBox2)
{
	if (HitboxMap.Contains(HitBox2))
	{
	}
}

You can also do cast wrapped in an if statement:

if (UBoxComponent* HitBox2 = Cast<UBoxComponent>(HitResult.GetComponent()))
{
	if (HitboxMap.Contains(HitBox2))
	{
	}
}

This is important, imagine your cast HitBox2 fails, so in your if will check nullptr and return false, that’s great! So the second check gets called and may do some validations to the nullptr, and that’s where the problem comes in, since the TMap is going to do some checks and the pointer is a nullptr the validations will fail and raise an exception trying to read nullptr, etc.