unitystation icon indicating copy to clipboard operation
unitystation copied to clipboard

Move IServerSpawn invocation back to PreRoundStart

Open NoooneyDude opened this issue 3 years ago • 6 comments

There's a lot of lag at RoundStart, a critical time where players start spawning.

Most of this lag is because we're now invoking all IServerSpawn implementations at RoundStart (as of https://github.com/unitystation/unitystation/pull/2793) to temporarily "fix" an issue where the implementations on some objects weren't getting invoked. However, as the saying goes, nothing's more permanent than a temporary fix. Until now!

Here's a summary:

The code to invoke all IServerSpawn implementations, FindObjectsOfType<MonoBehaviour>().OfType<IServerSpawn>(), used to be triggered at PreRoundStart. Now, this is fine for MonoBehaviours that implement this interface, because they are ready by this time. That is, the invoking code is triggered when Unity finishes loading a scene (and so all MonoBehaviour objects are enabled and ready to be found). However, at this point in time (OnSceneChange is triggered and scene is loaded), all (or perhaps not strictly all, but enough) NetworkBehaviour objects (with NetIdentities) are disabled! The invoking code never knows they exist. Mirror uses the OnSceneChange hook, too (or a similar hook), to enable all NetworkBehaviour objects - so our code to invoke OnSpawnServers as above has missed 'em.

When the invoking code was at PreRoundStart, the issue wasn't immediately apparent because there were not many NetworkBehaviours that implemented IServerSpawn, but this changed over time as more things were added. A quick but poor-performing fix (https://github.com/unitystation/unitystation/pull/2793) was to move the IServerSpawn invocation to RoundStart, when all NetworkBehaviour objects are enabled for sure.

The invoking of IServerSpawn should be refactored to occur at (or approximately at) PreRoundStart again. This will require finding a good invocation point. It needs to be after all NetworkBehaviour objects are enabled, and matrices should be ready. It needs to consider additional scenes.

Such a refactor will likely mean some implementations of IServerSpawn will break, because they expect to be called at PreRoundStart. These will need to be identified and fixed.

Could also lend some consideration to staggering OnSpawnServer calls, say, a few dozen a frame or something. Might smooth things over.

NoooneyDude avatar Jun 21 '21 12:06 NoooneyDude

Some Discord discussions on the topic that may prove to be helpful:

  • https://discord.com/channels/273774715741667329/312454684021620736/672618553463144489
  • https://discord.com/channels/273774715741667329/312454684021620736/843086843314307074
  • https://discord.com/channels/273774715741667329/312454684021620736/856495363405250580

NoooneyDude avatar Jun 21 '21 12:06 NoooneyDude

Related to #6491.

NoooneyDude avatar Jun 21 '21 12:06 NoooneyDude

this issue is being promoted to bounty status, valued at 100 USD.

PerfectTangent avatar Aug 20 '21 05:08 PerfectTangent

50 USD of the bounty paid out to Aranclanos for work done in #7395

PerfectTangent avatar Nov 30 '21 15:11 PerfectTangent

The bounty status for this issue is suspended until new notice

corp-0 avatar Nov 13 '23 00:11 corp-0