Unity-Technologies/com.unity.multiplayer.samples.coop

If a server was started for a 2nd time, NetworkObjectPool doesn't work anymore

AnomalousUnderdog opened this issue · 6 comments

Describe the bug
I was reading the documentation for Object Pooling since I wanted to add pooled bullet prefabs for my game, and it linked the source code for NetworkObjectPool.cs.

The code looked very strange to me as the pool is initialized only once, then never again. This leads me to believe it won't have correct behaviour if the server is shutdown, then started again for a 2nd time.

I don't know if this is proper Netcode for GameObjects style of coding, but anyway it does give me a KeyNotFoundException when trying to instantiate a pooled prefab when the server is started a 2nd time, because at that point, the _pooledObjects Dictionary has been cleared.

m_HasInitialized is never set back to false when ClearPool is called. Also the existing pooled prefabs don't seem to be cleaned up or reused properly after the server is started for a 2nd time.

I don't get why you have to unregister the prefab handlers anyway upon server shutdown. If the server program is about to exit, then it wouldn't matter since the entire program will close anyway. If the server is merely resetting itself, then wouldn't you want those prefab handlers to still be there?

I'm also confused why this is using its own queue instead of using the already existing ObjectPool class.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • OS: Windows 10
  • Unity Version: 2021.3.10f1
  • BossRoom Version: n/a I got the NetworkObjectPool.cs from a link in the documentation.

Additional context
Add any other context about the problem here.

Boss Room's pool assumes you won't shutdown and restart the NetworkManager inside the same scene. It uses the fact it lives in the Boss Room scene to get destroyed and recreated again when switching scene. Our "replay" flow takes us back to the main menu or character select to then start a new game manually for example.
If we wanted to make this a reusable component for users, I agree we could improve it like you mention. But for now this is a boss room specific implementation.

I'm also confused why this is using its own queue instead of using the already existing ObjectPool class.

Yeah, that one is because boss room was first built with 2020LTS which didn't have ObjectPool at the time. It's in our backlog to use this instead, didn't get the bandwidth for it yet.

Setting label "won't fix" but if you do find an issue in boss room directly, let us know!

Thanks, that makes more sense. I just think it's confusing to see this as a sample code in the documentation without being given the context explanation like you shared just now.

Ah right I see what you mean. Fair point, I'll reopen this issue and make sure we add some comments in doc and/or in code comments.

backlog item MTT-4696

This issue is fixed with PR #824. Now the pool gets properly reset when shutting down the server/client and restarting it. Updates to documentation incoming as well