No null pointer check for barrel in UTankAimingComponent::AimAt?

Shouldn’t we check for if the class variable Barrel has been set before accessing it in AimAt? What happens if a designer never implements SetBarrelReference in the event graph? Should it be something more like this?
void UTankAimingComponent::AimAt(FVector HitLocation)
{
auto TankName = GetOwner()->GetName();
FString BarrelLocation = “”;
if (Barrel) {
BarrelLocation = Barrel->GetComponentLocation().ToString();
}
UE_LOG(LogTemp, Warning, TEXT("%s aiming at %s from %s"), *TankName, *HitLocation.ToString(), *BarrelLocation);
}

1 Like

I agree with this 100%. I had made a mistake in my blueprint when trying to set the barrel and UE4 was crashing because of the exception. Once I found the problem it was fine but I do think we should check to see if Barrel is null or not.

if (Barrel) {
		auto OurTankName = GetOwner()->GetName();
		auto BarrelLocation = Barrel->GetComponentLocation().ToString();
		UE_LOG(LogTemp, Warning, TEXT("%s aiming at %s from %s"), *OurTankName, *HitLocation.ToString(), *BarrelLocation);
	}
}

Privacy & Terms