Allow passing spawn metadata to Instantiate via PrefabHandler

Hi @Extrys,
You can send meta data via the OnNetworkBehaviour.OnSynchronize method which is invoked on the server/authority side prior to serializing a NetworkObject.

The spawn process on the client side flows as such:

  • CreateObjectMessage: FastBufferReader contains serialized information.
    • NetworkObject.AddScenObject: SceneObject deserialized from FastBufferReader.
      • NetworkSpawnManager.CreateLocalNetworkObject: Network prefab is instantiated =or= an instance is pulled from an object pool. No stream buffer is used during this phase.
      • All NetworkBehaviour.OnNetworkPreSpawn methods are invoked.
      • NetworkObject.SynchronizeNetworkBehaviours:
        • NetworkVariables are deserialized and assigned their current value(s).
        • NetworkBehaviour.Synchronize: If you have used NetworkBehaviour.OnSynchronize then this will be invoked on each NetworkBehaviour component where your instance specific meta data would reside (unique to each NetworkBehaviour component type on the network prefab instance).
      • NetworkSpawnManager.SpawnNetworkObjectLocally: This is where OnNetworkSpawn and OnNetworkPostSpawn is invoked on each NetworkBehaviour component and where you could use the deserialized meta data you obtained during the OnSynchronize invocation(s) to uniquely setup your network prefab instance.

So the flow I think you are looking for is this:

Server Side:

  • Network prefab is pulled from a pool and spawned locally on the server
    • The CreateObjectMessage is created and sent.
      • During the serialization of the network prefab instance, one or more NetworkBehaviour components will have their OnSynchronize method invoked and will write serialized meta data specific to the NetworkBehaviour and/or the network prefab. This information is included in the CreateObjectMessage’s serialization data.

*Note: when you are spawning the object on the server side you are just directly pulling from the object pool (or directly creating an instance of the network prefab), the INetworkPrefabInstanceHandler implementation is not being invoked on the server side to obtain an instance for you. Including any form of meta data would require the INetworkPrefabInstanceHandler implementation to be invoked on the server side and would also require synchronizing the specific instance ids (i.e. NetworkObjectId and NetworkBehaviourId) in order to assure that specific meta data was associated with the spawned instance. However, since at this point the NetworkObject is not spawned there is no way to have that information.

Client Side:

  • CreateObjectMessage is received, deserialized, and then its Handle method invoked.
    • The SceneObject structure is used to create the network prefab instance.
      • If registered using a INetworkPrefabInstanceHandler implementation, then the handler provides an instance from the pool.
    • Prior to spawning, each NetworkBehaviour that provided OnSynchronize meta data will have their OnSynchronize method invoked where you can deserialize the unique meta data for your network prefab instance.
    • The network prefab’s NetworkObject is spawned.
      • Each NetworkBehaviour’s OnNetworkSpawn is invoked.
        • Any deserialized data obtained from OnSynchronize can be used to further define the unique traits of the instance.

This is the recommended way to handle adding custom serialized data to your network prefab instance when spawning it. It assures that when you spawn a new network prefab instance there is custom serialized data associated with the specific NetworkObject and will assure that custom information is always associated with that specific NetworkObject for late joining clients.

Let me know if this works for your project’s needs or what aspects of the above flow doesn’t work with your project’s needs and why adding the serialization data within one or more NetworkBehaviours doesn’t work and perhaps more of a description as to why you need to add all of your serialization data when getting a network prefab instance from a pool?