However, when it comes to pooling it brushes off the information “Coherence supports pooling” and that is it. I followed a link on that article to Instantiating from CoherenceSyncConfig | coherence Documentation which seemed promising but it doesn’t even talk about pooling and the code there is designed to showcase how you create and destroy objects. The only reference I found was that to activate pooling I have to spawn the object deactivated. Naturally, I have a few questions:
Does this mean, I have to set up my prefabs with objects deactivated by default? So basically tell my team that everyone has to remember to deactivate objects after testing and whatnot?
Let’s assume I understood the above correctly, OK. How do I instantiate the object? Default Unity method? If that is the case, how does Coherence know that this particular object needs to be pooled and not something local only like a UI effect?
Let’s assume I instantiated the object deactivated, it entered the pool, I did that right at the moment I needed it so I activated it right after instantiation. Does that mess with the above rule?
Next, the object does its thing. How can I put it back in the pool?
How do I request an object from the pool again?
More importantly what happens when you pool an object in Coherence? Does it need to be a networked object and does it act as such? Is it just a local object with initialization data and left to its own devices?
In the CoherenceSync component inspector, you will find an option called “Instantiate Via”. If you click on this option you will see a dropdown with different options, one of the options is called “Pool”.
If you select this option, coherence will automatically create a pool of gameobjects of this prefab, and it will use these for remote entities of this prefab that other clients have instantiated.
If you want to use the same pooling system to instantiate your own local/authoritative objects, you can use the CoherenceSyncConfig asset corresponding to your prefab. If you reference this asset, you will see that at runtime, it has the appropriate method to instantiate and destroy gameobjects, these methods will use the underlying “Pool” implementation to do so.
If you already have a pooling system in place and you would like remote objects to use it, you can hook your custom instantiation implementation by using the INetworkObjectInstantiator interface. Once you have implemented this interface, you will able to select it in the “Instantiate Via” dropdown that I mentioned earlier from the CoherenceSync inspector.
Hope this info has clarified things, let me know if you have any more doubts.
@miguel-coherence Thank you very much for the clarification. @ciro Yes, I did search for pooling, didn’t bring up the link you posted. Then I looked manually through the pages till I found the two pages I linked – I didn’t expect the page about pooling to be under an example tutorial to be honest so I didn’t look there. Just after the “Manual” header.
Some follow up questions. What I am reading is that to pool an object through Coherence I have to have a CoherenceSync component on that object. Even if I am instantiating something like say an explosion that needs to happen on all clients (who care about it) and it won’t move, rotate or do anything aside from getting disabled after it was enabled, correct?
Anything else, I’d implement the interfaces mentioned or roll my own thing, right?
Finally, my number 1 question is moot right? That “they have to spawn deactivated first time” isn’t applied?
Yes, to take advantage of our pooling system (and so also for coherence to spawn them for you with the correct position and rotation) they’d need to be network entities, so Prefabs with a CoherenceSync attached.
You can understand that this is not always desirable. I think your example of the explosions is a good one: you might not want them to be entities, but perhaps just invoke their instantiation at a certain spot in the scene with a NetworkCommand, and then handle their destruction locally, when they’re done. But then you’d need to roll out your own pooling.