IPunPrefabPool Example for Object Pool

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

IPunPrefabPool Example for Object Pool

unitylearner
2016-04-08 13:40:51

Hi ,

I want to pool my effects in my network game.

Can you pleas provide some example how to do it .

What i tried

  1. Add PhotonView in Effect and Call PhotonNetwork.Instantiate

And it gets visible in all screens.

How to pool this ?

What i want to do is throw grenade and show effect.

Regards
Unity Learner

Comments

vadim
2016-04-14 09:43:46

Hi,

Not sure what you mean by 'pool'. Please explain.
But in any case, using PhotonView is overkill. Better send RPC to all with effect parameters and instantiate effect locally on each client. With such approach, you can use any non-network techniques for effects including 'pool' .

mmax
2016-08-08 17:07:39

He means probably this https://doc-api.photonengine.com/en/pun/current/interface_i_pun_prefab_pool.html#details

I would like to see an example how to use this pooling too. Where and how should I implenent my functions ( or from any 3rd party pool manager)

jeanfabre
2016-08-15 12:22:32

Hi,

Give me few days, I'll come up with a sample. Thanks

Bye,

Jean

jeanfabre
2016-08-26 11:28:50

Hi,

Here it is:

http://forum.photonengine.com/discussion/8214/ipunprefabpool-working-sample-with-smartpool-free

Let me know it goes for you and if you have more questions.

Bye,

Jean

mmax
2016-08-28 12:55:20

Hi Jean

Thank you very much for the sample. I tried to adjust it to my needs to use with this Poolmanager docs.poolmanager.path-o-logical.com/home
Basically it is working.

What is the proper call for this "pool enabled" instanciating
PhotonNetwork.Instantiate or PhotonNetwork.PrefabPool.Instantiate in the main game loop ?
This is the only thing I am missing in the example you have created.

When now Im using spawn and despawn function, at each spawn there is a new (higher) viewID assigned, but at despawning the viewID number is not "cleared". So I would run out of view IDs (I already tried it). How can I recycle view IDs when Im not using "standard" Instantiate and destroy ?

jeanfabre
2016-08-29 08:39:39

Hi,

Sorry, I don't understand your question. Can you rephrase or explain differently, thanks :)

As for manually recycling viewID, this is opening a can of worm. Are you sure you want to go this way?

  • How do you create viewIDs?
  • what do you spawn, bullets?! it's not recommand to spawn bullets or any items of large quantity, you should keep networked gameobject to the strict minmum otherwise your game isn't going to perform at all. Let me know your case, and we can discuss possible avenues.

Bye,

Jean

mmax
2016-08-29 10:00:36

hello, sorry for complicated not-explanation :-)

Issue is following.
Sometimes I want to instanciate in a regular way, without using the pool. (e.g. players motorbike)
For some specific objects - in my case guided missiles - I want to instanciate them from pool.
If I implement pool in similar way as in example, it is still trying to instanciate them from pool, regardless if I use PhotonNetwork.Instanciate or Photon Network.PrefabPool.Instanciate.
Basicaly, I would like to know in which case should I use PhotonNetwork.Instanciate and in which Photon Network.PrefabPool.Instanciate.

To the second part:
I am obtaining view ID by AllocateviewID function and assigning to missile which I spawn from pool. When I want to destroy the missile I just despawn it back to the pool. But what should I do with the already used/allocated viewID ?
I tried unallocate function, but that doesnt work. Because when I want to "reuse" the missile, Im getting a completely new viewID from allocateviewid function.

I know it is complicated, in the meantime I would also try to solve it without manual assigning viewid

jeanfabre
2016-08-29 11:46:49

Hi,

It's possible, however, the best answer would be that pool should be used always, it's never a bad idea to have a pool for even a small number of instantiation needs. so create as many pool as you have prefabs.

Now for the solution: If you want to toggle pooling, simply set PhotonNetwork.PrefabPool = null inside PunSmartPoolBridge by created two new functions "EnablePooling()" and "DisablePooling()" for example , and before instantiating you toggle pooling as you wish.

Let me know how it works out for you, if you have trouble, I'll provide a modification to bring that feature in.

Bye,

Jean

mmax
2016-08-29 18:22:09

hi

that actually works !

But Im still having issues with viewIds.

The pool manager which I use, simply creates objects at the start in disabled state. That means all objects in pool get viewID "0" and owner is "Scene" by default. At spawning form pool, the objects get activated, but the viewId reamain default ! That was the reason, why I assigned manualy viewID to objects, which were spawned from pool.

I do not know if also the other pool managers work like this.

Is there other way how get the automatic reuse of viewIDs and still use that kind of pooling ?

jeanfabre
2016-08-30 11:20:05

Hi,

that's odd, because smartPool also maintain pooled gameobject in a disabled state during storage, so I am not sure what's happening on your end.

So, my example as is works, but with PoolManager it doesn't. What's your poolManager pool settings? can you share a screenshot of how you've set that up, I'll try to replicate.

Bye,

Jean

jeanfabre
2016-08-30 11:29:32

Ok,

I just tested with PoolManager and it works as well.

If you still have problems, pm me, I can take a look at your project.

Bye,

Jean

mmax
2016-08-30 13:25:07

hi,

I tried also the Smart pool and the result is the same. (view ID is still 0 always, even after spawning)
When should be the viewID changed ? I suppose after the object is spawned.

here the settings

poolmanager

smartpool

Anyway, thanks for your help, I will investigate it, I think that problem have to be "Me" :smile:

jeanfabre
2016-08-30 13:30:28

Hi,

what version of Unity are you using?

cause I can get this to work with smartPool too.

Where do you look for the viewID?

Bye,

Jean

mmax
2016-08-30 13:54:59

hi,

Thanks for your help and effort

Problem solved, it was -as always - Me.

I called PhotonNetwork.PoolPrefab.Instantiate and I should call just PhotonNetwork.Instantiate

jeanfabre
2016-08-30 14:15:26

no worries :)

SteveR
2017-09-23 05:04:35

Hi,
I'm having same problem (viewID is still 0) with using smart pool, how can I fix this?
Unity version is 2017.1 p3 and photon version is 1.86
Please help !

jeanfabre
2017-09-25 06:35:29

Hi,

Are you calling PhotonNetwork.Instantiate ?

Is the example project provided works?

Bye,

Jean

SteveR
2017-09-26 05:33:37

@jeanfabre wrote:

Hi,

Are you calling PhotonNetwork.

Hi,

Are you calling PhotonNetwork.Instantiate ?

Is the example project provided works?

Bye,

Jean

Yep I called PhotonNetwork.Instantiate.
hm in example demo viewID isn't 0

SteveR
2017-09-26 08:13:59

wait example demo is also 0

jeanfabre
2017-09-27 12:08:05

Hi,

odd, I don't get this here. What version of un and Unity do you have?

the pool sample needs an update btw, I'll get on this as it uses an old pun version. but that did not affect eh view Id, I tried with the latest pun on the asset store as well.

Bye,

Jean

Spaggi
2017-11-11 23:14:35

Hi,

I just stumbled over the same problem and wanted to share my solution, maybe someone else has this issue.

What happened to me is that I was calling an RPC on my Photon Object via Update from another script. This however can lead to the described issue as the object might not have received its viewid when coming from the pool. The object is ready once OnPhotonInstantiate has been called.

So my solution was to set an bool in OnPhotonInstantiate and wait in my update until the bool has been set.

SteveR
2017-12-09 14:30:09

Hi, I fixed strange bug btw, but having problem with checking ower. It doesn't works.
public bool isMine
{
get
{
return (this.photonView.ownerId == PhotonNetwork.player.ID) || (!this.photonView.isOwnerActive && PhotonNetwork.isMasterClient);
}
}
I called this on OnEnable()
Help me :(

jeanfabre
2017-12-11 12:14:33

Hi,

Can you confirm it is because you are using iPunPrefabPool interface?

If you try later and not on Enabled, is it then working?

Bye,

Jean

SteveR
2017-12-18 06:47:42

When use OnPhotonInstantiate, It works fine.
But when use OnEnable/OnDisable, It doesn't work.

I called isMine from Component.

And When use OnPhotonInstantiate I'm having this error:
SmartPool (MPlayer): Cant DespawnMPlayer_stock' because it's not managed by this pool! However, SmartPool destroyed it!
UnityEngine.Debug:LogWarning(Object)

Help me

Hekeni
2021-02-07 06:49:46

@jeanfabre Could to please do an new object pooling sample over network using Pun2 because currently we don't have smart pool and a lot changed.

Thanks in advance,.

vinibeg
2021-12-02 16:35:48

We need smart pool for PUN2, please!

Back to top