Using SetPawn causes Access violation

Hello,

Here is my issue I’ve just added a Dynamic_Multicast_delegate to my code SetPawn seems to be the best method to use for the listening function however when I hit play I get Acces Violation from the PlayerController in BeginPlay() on line 13: auto AimingComponent = GetPawn()->FindComponentByClass<UTankAimingComponent>(); using dump file I see that FindComponentByClass is the method causing the unhandled exception stating this->Pawn was nullptr here is the code for SetPawn `void ATankAIController::SetPawn(APawn* InPawn)
{
Super::SetPawn(InPawn);

if (InPawn)
{
	auto PossesedTank = Cast<ATank>(InPawn);
	if (!ensure(PossesedTank)) { return; }

	PossesedTank->OnTankDeath.AddUniqueDynamic(this, &ATankPlayerController::OnPossesedTankDeath);
}

}`

What is your delegate and where does it get triggered from?

Declerations Tank.h: DECLARE_DYNAMIC_MULTICAST_DELEGATE(FTankDelegate); FTankDelegate OnTankDeath .cpp if (CurrentHealth <= 0) { OnTankDeath.Broadcast(); }
Listeners ATankAIController & PlayerController .h ( UFUNCTION() void OnPosssesedTankDeath() )

this void is the place where it is triggered from so in AIController and PlayerController

Unless you’re just on about the.Broadcast?

Oh wait. Pawn won’t be valid yet in player controller begin play. Try using OnPossess.

Thank you I will try that now :smiley:

using OnPossess end up with Ensure condition failed: PlayerTank [File:E:\Unreal Projects\Tank-Game-master\Source\BattleTank\Private\TankAIController.cpp] [Line: 50] ( auto PlayerTank = Cast<ATank>(GetWorld()->GetFirstPlayerController()->GetPawn()); )

Try auto PlayerTank = Cast<ATank>(InPawn);

Well I just noticed the issue didn’t put Super::SetPawn(InPawn) into the PlayerController facepalm

Woops! Remember your ABCs (Always Be Calling super)

Thank you for your time I’m a bit of a n00b with UE and C++