Should DetachFromControllerPendingDestroy() be called on both the clients and the server when a player dies in a multiplayer game?
The way my code is set up, only the server handles changing the health and will call Character::HealthModified(). In this function, there’s a check if Health <= 0 and if so, it calls Die() which calls DetachFromControllerPendingDestroy().
For some reason the DetachFromControllerPendingDestroy() is causing my game to crash when the client dies.
From the look of the code, you don’t need to call it at all. It’s called internally when the pawn is destroyed as well as a few other places. I may be missing something because I don’t know why it’s even exposed to blueprints. So you can just destroy the pawn and the detach should get called for you. Omit that explicit detach call and you should see the pawn despawn, leaving you with a camera floating in space.
Ok, I didn’t know it was called anyway when the pawn was destroyed. I see that ShooterGame and other C++ examples explicitly call it in their Character Death related functions (example: AShooterCharacter::OnDeath()). I thought they might call it explicitly as a way to disable player controls immediately when the player dies but wasn’t sure.
Oh. Maybe. It could also be that it wasn’t called in the engine when those projects were made.
If they still run then you should be able to call it yourself. If you need to, that is. If you do, then post the log with the error and maybe we can fix the issue.
And remember that once detached, the controller will get errors if you’re not checking “IsValid?” on the controlled pawn.
I had to install debug symbols but I found the source of the crash. I was using the MoveForward() function from Epic’s FPS tutorial (https://docs.unrealengine.com/latest/INT/Programming/Tutorials/FirstPersonShooter/2/3/index.html). In that function they don’t check if the controller is null before dereferencing. For some reason MoveForward() still gets called even without pressing any keys leading up to or after the death.