How to Destroy Actor in Multiplayer?

I know this is an old thread but something no one has addressed here is that there are multiple actors involved in one multiplayer player: Controller, Character, Player State. Which ones are we supposed to delete in the server? I see the Lyra project deletes the controller and character, but I have a case where I have all three set to replicate and spawned on the server but when I delete the controller and the character on the server, the Player State persists on clients.

Actually, I found this gem in the source which explains why the character wasn’t getting fully deleted.

void AController::K2_DestroyActor()
{
    // do nothing, disallow destroying controller from Blueprints
}

So to fully delete a player (AI or otherwise) in multiplayer, it must be done in C++.

If you built things correctly deleting a player controller will cascade down and delete everything related to it.

If you didn’t build things correctly, then maybe its time to check if your current code can be ported over to be more correct.

The logical hierarchy is always
Game mode > player controller > possessed pawn/character.
This doesn’t mean much, since APlayerController is a subset of AActor and the possesed pawn is also an AActor.
This means deleting the player controller can sometimes orphan the controlled pawn/actor.

To prevent this, add a function to onDestroy of the player controller, find the controlled pawn, and fire off its replicated delete command.
This way everything will cascade from the player controller.

APlayerState is part of the game framework, but the hierarchy of it places it as a descendant of the actor, so it should be also destroyed along by removing a player controller along with the actor.
If you find that it doesnt, similarly to above:
Add onDestory to the actor, find its APlayerState, call a replicated delete for it.

Note: should ondestroy not work in ue5 or what have you, you can always make a custom event to fire off with the same code in it.

Also note:
Usually deleting a player controller is not desired. It could revert back to some sort of spectator or default player class.
Its best to hard lock the class down in some other way that syncs the status with the server to prevent cheating over deleting it, in my experience at least.
How you do it depends on the scope - and overall adding/removing players should not have any adverse effects on anything: performance or memory load, or whatever else.
So it could be valuable to leave player controllers in place even if not needed for those reasons.

1 Like

Thanks for your reply.
I was editing my post as you were writing your reply but I found deleting controllers is explicitly and purposely disabled from Blueprints which is what I was trying to do (I included the source snippet in my post).
I was deleting the Character and the Controller because that’s what Lyra does, but if just deleting the controller is enough, even better.