# PlayerController spawning and possesing new Character

**URL:** <https://forums.unrealengine.com/t/playercontroller-spawning-and-possesing-new-character/1248375>\
**Category:** C++\
**Tags:** unreal-engine, question\
**Created:** [August 2, 2023, 2:37pm UTC](https://forums.unrealengine.com/t/playercontroller-spawning-and-possesing-new-character/1248375 "2023-08-02T14:37:47Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [August 2, 2023, 2:54pm UTC](https://forums.unrealengine.com/t/playercontroller-spawning-and-possesing-new-character/1248375/2 "2023-08-02T14:54:03Z")

</div>

> [@Koucen](#):
>
> but if I run this one it leaves the previous pawn hanging or running

running as if you are still holding a run button on the keyboard? Could be you need to clear inputs when unpossessing, pretty sure I had some code for that ready years ago let me take a look. \* can’t find it immediately I thought it was a method called flush / clear which would be logical on an input component.

> [@Koucen](#):
>
> I’ve noticed that when using the first block of code, when it enters the code in OnPosses it does not call UnPossess();

That part of controller code is a shitshow at the moment:

> [[UE5.2.1, Bug report] Controller::Possess broken if Pawn auto possess AI. - #2 by Roy\_Wierer.Seda145](https://forums.unrealengine.com/t/ue5-2-1-bug-report-controller-possess-broken-if-pawn-auto-possess-ai/1218849/2)

\*Edit question, why are you not using Possess but OnPossess? What version of UE are you on?

```auto
void AController::Possess(APawn* InPawn)
{
	if (!bCanPossessWithoutAuthority && !HasAuthority())
	{
		FMessageLog("PIE").Warning(FText::Format(
			LOCTEXT("ControllerPossessAuthorityOnly", "Possess function should only be used by the network authority for {0}"),
			FText::FromName(GetFName())
			));
		UE_LOG(LogController, Warning, TEXT("Trying to possess %s without network authority! Request will be ignored."), *GetNameSafe(InPawn));
		return;
	}

	REDIRECT_OBJECT_TO_VLOG(InPawn, this);

	APawn* CurrentPawn = GetPawn();

	// A notification is required when the current assigned pawn is not possessed (i.e. pawn assigned before calling Possess)
	const bool bNotificationRequired = (CurrentPawn != nullptr) && (CurrentPawn->GetController() == nullptr);

	// To preserve backward compatibility we keep notifying derived classed for null pawn in case some
	// overrides decided to react differently when asked to possess a null pawn.
	// Default engine implementation is to unpossess the current pawn.
	OnPossess(InPawn);

	// Notify when pawn to possess (different than the assigned one) has been accepted by the native class or notification is explicitly required
	APawn* NewPawn = GetPawn();
	if ((NewPawn != CurrentPawn) || bNotificationRequired)
	{
		ReceivePossess(NewPawn);
		OnNewPawn.Broadcast(NewPawn);
		OnPossessedPawnChanged.Broadcast(bNotificationRequired ? nullptr : CurrentPawn, NewPawn);
	}
	
	TRACE_PAWN_POSSESS(this, InPawn); 
}

```

---

_[View the full topic](https://forums.unrealengine.com/t/playercontroller-spawning-and-possesing-new-character/1248375)._
