Skip to content

fix: destroy object spam#651

Merged
mattwalsh-unity merged 2 commits intodevelopfrom
fix/destroyobjectspam
Mar 23, 2021
Merged

fix: destroy object spam#651
mattwalsh-unity merged 2 commits intodevelopfrom
fix/destroyobjectspam

Conversation

@NoelStephensUnity
Copy link
Copy Markdown
Member

This fixes an issue where the destroy method can get called twice on the client side after receiving a destroy object command.

This fix is pertaining to MTT-556
This is a temporary fix until the spawning and de-spawning process has been fully vetted.
This fix prevents the MonoBehavior.OnDestroy method from invoking the NetworkSpawnManager.OnDestroyObject method if it does not exist in the NetworkSpawnManager.SpawnedObjects list.

This fixes an issue where the destroy method can get called twice on the client side after receiving a destroy object command.
private void OnDestroy()
{
if (NetworkManager.Singleton != null)
if (NetworkManager.Singleton != null && NetworkSpawnManager.SpawnedObjects.Count > 0 && NetworkSpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count > 0 check shouldn't be necessary as it is always true if ContainsKey returns true right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking an Int value first is faster than checking a hashed set, so it is just an early out optimization. Not required.
Of course, if it is > then you have the int compare plus the hashed set check... :/

50/50 on this.. :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went ahead and removed it in 5e04e7e.

Copy link
Copy Markdown
Contributor

@LukeStampfli LukeStampfli Mar 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah I see I guess yours is much faster. I hoped dictionary would make an empty check with contains key but apparently after checking the source it doesn't.

Decided this isn't worth the optimization.
@mattwalsh-unity mattwalsh-unity merged commit 5a5fc05 into develop Mar 23, 2021
@mattwalsh-unity mattwalsh-unity deleted the fix/destroyobjectspam branch March 23, 2021 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants