Custom Spawning

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 Fusion.

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.

Custom Spawning

myleslambert
2022-06-27 14:21:18

Hi! I've been using Photon Unity (PUN) for quite some time.

In most projects I've opted to handle the spawning of objects myself so I can use my own pooling. In PUN this was relatively straight forward, I'd just need to send over the ViewID and set it manually along with the correct owner.

Is this also manageable in Fusion or is it more complex?

Thanks,

Myles

Comments

OdoVR
2022-07-13 07:06:32

I would also like to know how this could be done. @myleslambert , have you figured it out?

Isaac_Augusto
2022-07-13 10:22:25

Hi, it's possible to do your custom pooling passing an instance of the INetworkObjectPool to your StartGameArgs.ObjectPool when starting the runner.

The interface ask for 2 methods implementations, referring to AdquireInstance() and ReleaseInstance().

If no INetworkPool implementation is specified, the NetworkRunner will use this internal default implementation. The default implementation does not pool objects, and instead will just creates a NetworkObject from scratch on Spawn() and destroys the instance on Despawn()

class NetworkObjectPoolDefault : INetworkObjectPool {    
    public NetworkObject AcquireInstance(NetworkRunner runner, NetworkPrefabInfo info) {    
      if (runner.Config.PrefabTable.TryGetPrefab(info.Prefab, out var prefab)) {    
        return Object.Instantiate(prefab);    
      }    

      return null;    
    }    

    public void ReleaseInstance(NetworkRunner runner, NetworkObject instance, bool isSceneObject) {    
      Object.Destroy(instance.gameObject);    
    }    
  }    

You can use it to create your own pooling system.

A page about it it's already being deployed and should be live soon, but i believe that the Tanknarok sample has a showcase for that.

Hope that helps.


Isaac Augusto

Photon Fusion Team

OdoVR
2022-07-14 02:21:00

Thank you @Isaac_Augusto , I'll give it a go.

myleslambert
2022-07-16 08:55:44

Thanks Isaac!

Is it possible to feed in a custom prefab id into the system? For example I already have a unique id for all spawnable prefabs for my save system and would like to reuse the same id for simplicity.

Also it doesn't look like this will work for any sort of async prefab loading. Is that something that could be supported? Where a few frames might pass when acquiring prefab instances?

Thanks,

Myles

Back to top