BeardedManStudios / ForgeNetworkingRemastered

See various forks, also please join the Forge Community fork of Forge Alloy! -> https://github.com/ForgeAlloyCommunity/ForgeAlloy
https://twitter.com/FarrisFaulds
Apache License 2.0
1.49k stars 305 forks source link

Destroy network object if behavior (or game object) is destroyed #392

Closed NoTuxNoBux closed 3 years ago

NoTuxNoBux commented 3 years ago

This seems kind of logical to me, but perhaps there is a reason that this doesn't happen currently?

The reverse already happens: destroying the network object will also destroy the game object.

I have application code that operates on game objects and has no knowledge of the network, which can remove game objects - that aren't always accompanied by network behaviors -, which currently causes them to linger around on other clients, as the network object isn't automatically destroyed along with the game object (or its behaviors). Not doing this would force me to make that code aware of the networkObject internals just to be able destroy it properly.

An alternative solution would be that I do this in a class that extends from this, but this seemed sufficiently desirable for everyone that I created this PR instead 😄.

NoTuxNoBux commented 3 years ago

I think I figured out why this is not the default: Unity calls OnDestroy on client shutdown, which in turn means any network object will now be destroyed for everyone if a client disconnects after this change. This prevents you from having objects that linger around on the server after the client disconnects, and this is thus probably not what you want.

This means that you need to make the decision yourself and this cannot be automated without breaking the other use case or adding an opt-in flag, so I'm closing this for now.