How to NOT destroy pawn after logout/exit?

Hello,

So what I need to do is possess a pawn (in this case a vehicle) but have it unpossess and not be destroyed when the player exits or disconnects.

That way the vehicle can be possessed by another player for example.
Problem is the vehicle is destroyed before the controller so there is no easy way of doing that.

How would you go about doing this? There should be an easy way for something like this… Any help would be greatly appreciated.

I’m assuming from your post that this is a multiplayer game. I’m not super solid on the multiplayer aspects of UE4, but if I understand correctly, so long as you do not destroy the vehicle on the server when you disconnect the player (on logout) the vehicle should still remain – if it is not actively possessed by a player; however, I think the issue you’re running into is that the pawn is getting removed because it is possessed by a player controller on disconnect.

To resolve this, you can try to using either the Pawn > Un Possess node in Blueprints or using the APlayerController::UnPossess() method() to separate the player controller and pawn before disconnecting the player.

Hope that helps, if not, well at least I tried.
Best of Luck

On LogOut is too late, the pawn seems to be already unpossessed, so it gets destroyed. Do you found any solution ?

Did you find a solution? I’m facing the same issue - to store the pawn transformation and other state date from it

I found a solution for my problem… i guess its the same then yours. In my gamemode BP i want to store in the “OnLogout” several informations of the player pawn, like position, but the pawn is already destroyed in the “OnLogout” by the Unpossess’ing.

So i didnt found a way in BP honestly… my solution for it is to overwrite the UnPossess() of my custom class C_PlayerController and override the “UnPossess()” method to store stuff from the Pawn before it gets unpossessed further. In BP i then just use the value of my storeage variable “unpossessPawnTransform”.

The Header part:

UCLASS()
class CLIENTSERVERTEST_API AC_PlayerController : public APlayerController
{
   GENERATED_BODY()

public:
   /** Stores the transformation of the unpossessed pawn */
   UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=PlayerController)
   FTransform unpossessPawnTransform;

   virtual void UnPossess() override;
};

The CPP part:

void AC_PlayerController::UnPossess()
{
   UE_LOG(LogTemp, Warning, TEXT("UnPossess()"));

   if(this->GetPawn() != NULL)
   {
      UE_LOG(LogTemp, Warning, TEXT("UnPossess() i got some pawn"));
      this->unpossessPawnTransform = this->GetPawn()->GetActorTransform();
      this->unpossessPawnTransform.DebugPrint();
   }
   else
   {
      UE_LOG(LogTemp, Warning, TEXT("UnPossess() dont have a pawn here too, wtf"));
   }
   
   APlayerController::UnPossess();
}

On my gamemode BP it looks like this then:

[Solution found here.][1]

Variant for changing the engine.
In your C++ class, simply redefine the method by using it.

Create your Controller Class, and make it a function in the similarity:

.h

UFUNCTION (BlueprintNativeEvent, BlueprintCallable, Category = Miheev)
void PawnLeavingGame ();

UPROPERTY(EditAnywhere, BlueprintReadWrite, config, Category = Miheev)
bool DestroyActorThenUnpossess;

.cpp

void AMController :: PawnLeavingGame_Implementation ()
{
if (GetPawn ()! = NULL && DestroyActorThenUnpossess)
  {
   GetPawn () -> Destroy ();
   SetPawn (NULL);
   }
}

The redefinition source is in the code:
UnrealEngine\Engine\Source\Runtime\Engine\Classes\GameFramework\PlayerController.h
UnrealEngine\Engine\Source\Runtime\Engine\Private\PlayerController.cpp

And now the destruction can be regulated through the Blueprints! Yess!

220225-2017-11-15-18-55-18-miheev-unreal-editor.png

I hope this will add to the standard functionality. After all, the weight of this code is only 138 bytes, and the use of space.

5 Likes

Hi, thank you for this solution!

My solution here, faster I presume + BP expose :

Create sub class of PlayerController then put :

in SubPlayerController.h

UCLASS()
class MYGAME_API ASubPlayerController : public APlayerController
{
	GENERATED_BODY()
public:

	UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "OnPawnLeavingGame"))
	void ReceivePawnLeavingGame(APawn * aPawn);

	virtual void PawnLeavingGame() override;

};

in SubPlayerController.cpp :

void ASubPlayerController::PawnLeavingGame()
{
// This part is the original destroying effect. Remove if unwanted
	// if (GetPawn() != NULL)
	// {
	//	GetPawn()->Destroy();
	//	SetPawn(NULL);
	// }
// 
	ReceivePawnLeavingGame(GetPawn());
}

And voila! You can manually destroy or not the pawn in BP:

image

1 Like