How to prevent Netcode from destroying spawned NetworkObjects on Shutdown

Hi!

In my game, I have to dynamically instantiate the players’ NetworkObjects during gameplay, but I do NOT want Netcode to destroy them automatically on Shutdown, because such a spontanous destruction of the player’s object in the midst of gameplay, e.g. when losing connection to the host, causes glitches with the camera and more.

I want Netcode to leave the players’ NetworkObjects alone, so I can shut the game session down in a controlled way.

Is there any possibility to achieve this?

I delved into the Netcode source to find out if I can set any properties to prevent Netcode from destroying the NetworkObjects, but everything that influences this behaviour is private or internal. No chance. :slight_smile:

There is NetworkObject.DontDestroyWithOwner, but this only has an effect on the host if a client disconnects, but not when a client loses connection to the host.

I would recommend using either the OnClientDisconnectCallback in NetworkaManager.Singeltion or to override the OnNetworkDespawn method in each NetworkBehaviour class.

I hope that helps

1 Like

Thanks for your reply.
In these callbacks we can react to the disconnect event, but we cannot prevent the immediate destruction of the player’s NetworkObjects.
I looked into the Boss Room source, and they immediately show the loading screen back to menu when disconnection happens, so they circumvent the problem of the destruction of the objects.
This is not what we want to do. We want to stay in the game, show an error popup, and when the user confirms the popup, then return to main menu. However, this is not possible, because in the moment the disconnect happens, the player character is deleted, leaving us with countless exceptions and a broken camera pointing on nothing, and there is nothing we can do against it. :slight_smile:

I think Netcode’s built-in logic: “It if is a scene object, leave it, if it is a prefab, delete it” is too much behaviour enforced on the game developer. The developer should be able decide what to do with the object. I cannot tell the game design department that we must immediately exit the game and cannot show a popup message before, because the network library wants it this way. :slight_smile:

I think (not 100% sure and I’m not a unity c# expert at all) there is no way around not destroying the spawned NetworkObjects because I needed this once and couldn’t find a solution.

However you can still use OnClientDisconnectCallback as the other person suggested (which is what I use too). I think you need the message to pop on the client’s screen (because when clients leave the game, it generally keeps going on the host side) so what you can do in that callback is : if it’s not the host leaving, don’t change the scene and enable or instantiate a simple Panel with a button which will trigger the return to menu function on click. ->change scene after the button was pressed. But obviously your client would already have left the networking part of the game (because the host closed the server by leaving, by calling NetworkManager.Singleton.Shutdown I think) and the button will only change the scene back to the main menu.

The panel sould also cover up the fact that the player doesn’t exist anymore (or you can spawn another fake player while waiting for the player to press return to menu but that’s an annoying workaround tbh)

For the exceptions, since the host left, the NetworkManager.Singleton is destroyed so maybe that’s where those errors might come from, you can check “if (NetworkManager.Singleton == null) { return; }” before using any of the singleton functionnality but idk if that’s really your issue.

1 Like