Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

NetworkServer.Spawn: initializing spawned objects after awake

Discussion in 'Multiplayer' started by Curious-George, Jul 8, 2017.

  1. Curious-George

    Curious-George

    Joined:
    Nov 8, 2012
    Posts:
    130
    I have an object that I've converted to a NetworkBehaviour. That object needs to be initialized after it's been spawned, but before it starts updating.

    The ideal solution would be something like passing arguments to NetworkServer.Spawn() that were then passed to callbacks on the spawned NetworkBehaviours, that ran immediately after Awake().

    A workaround could be calling a [ClientRPC] with the arguments I need, then calling Instantiate() manually on the clients, but then how would I associate the NetworkIdentity on the client object with the one on the server?

    Or is there some other way to do this?

    Thanks
     
  2. Curious-George

    Curious-George

    Joined:
    Nov 8, 2012
    Posts:
    130
    It seems ClientScene.RegisterSpawnHandler() comes close to what's needed except that there is no way to send a payload/userdata to the handler.
     
  3. Curious-George

    Curious-George

    Joined:
    Nov 8, 2012
    Posts:
    130
  4. marcV2g

    marcV2g

    Joined:
    Jan 11, 2016
    Posts:
    115
    Try putting the data in sync vars set before calling network.spawn and use the data in start()
     
  5. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Still no guarantee that the syncvars are actually synced up when the Spawn message arrives.
     
  6. Curious-George

    Curious-George

    Joined:
    Nov 8, 2012
    Posts:
    130
    Thanks - not a bad idea, but doesn't exactly fit my current situation.

    I've worked around the problem for now (the hard way, which took all day). If NetworkServer.Spawn was able to send extra data along for the ride though, this issue would have been instantly resolved.

    I remember wanting this yeeears ago when I did a school project with Unity's first iteration of networking. I'm surprised this is still not possible. It seems like a very reasonable thing to want to do, to perform initialization on a newly spawned object before updates/collision/etc start running. Duplicating per-level logic into the MonoBehaviour.Start function of every object that could possibly spawn is not a solution for this.
     
  7. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    I currently just do a dirty workaround. I need to basically spawn the object and then run some code on clients. So I just send a rpc that runs a coroutine and while the object that is supposed to be spawned doesn't exist. It yields to next frame.
     
  8. Curious-George

    Curious-George

    Joined:
    Nov 8, 2012
    Posts:
    130
    I'm still not sure that the spawn-initialize process will be guaranteed to be atomic with this approach.

    I'm still finding bugs due to this problem. For example, I've got an AI character with a boid-like steering behaviour on it. So if the AI is placed in the scene, I can assign a collider in the scene as an inspector reference on the character. Then, in Start(), the character's script uses the collider to create the appropriate data structure representing the boid's boundaries in the scene.

    The problem comes in when I want to spawn more AI characters at runtime. It should be as simple as instantiating the AI's prefab, assigning the collider reference from the scene immediately after, and then letting Start() do what it needs to do to prepare the object for Updating/Steering. In a non-networked game, it would be that simple, but If I can't get code to run on clients between instantiation and Start(), then I have to start polling for changes in state every single frame, which is a real kludge.
     
  9. firebird721

    firebird721

    Joined:
    Jun 8, 2022
    Posts:
    90
    In unity 2023. How the relationship of awake start and on network spawn works? Sometimes it seem initializatin doesnt get to be performed for some weird reason thst i dont know...