IPunPrefabPool Example for Object Pool

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

  • 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' .
  • 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)
  • Hi,

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

    Bye,

    Jean
  • 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
    mmax
    edited August 2016
    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 ?
  • 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
  • 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
  • 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
  • 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 ?
  • 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
  • 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
    mmax
    edited August 2016
    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:
  • 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
  • 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
  • no worries :)
  • 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 !
  • Hi,

    Are you calling PhotonNetwork.Instantiate ?

    Is the example project provided works?

    Bye,

    Jean
  • jeanfabre said:

    Hi,

    Are you calling PhotonNetwork.

    jeanfabre said:

    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
  • wait example demo is also 0
  • 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
  • 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.
  • 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 :(

  • 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
    SteveR
    edited December 2017
    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
  • @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,.
  • We need smart pool for PUN2, please!