Search Unity

Question Sending data to a new client before loading the scene

Discussion in 'Netcode for GameObjects' started by CosmoM, Sep 4, 2022.

  1. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    My game's initial screen is a login screen that contains a NetworkManager. When a valid server IP is entered and the client connects, the scene the server is in (typically a game in progress) is loaded automatically. From there, Rpcs are called to send data around before the first frame is drawn on the client (e.g. current game state, data on other players).

    My problem is that I now want some of these processes to depend on parameters set by the server when the game was started. Unlike all of the other data (like current game state), these parameters need to be known by the client already by the time the active game scene is loaded (this is because the parameters are needed before some OnNetworkSpawn calls). However, I don't know how to get this data to the new client before then.

    One solution would be to send this data in the login scene, however I'm finding trouble adding NetworkObjects/NetworkBehaviours to this scene since it is non-networked, at least when first loaded.

    Is there any way to send simple data from the host to the client before loading the host's scene?
     
    Last edited: Sep 5, 2022
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    I had a little look into this. It looks like OnClientConnectCallback happens too late to be useful, you may be able to use an in-scene object on the login screen as it looks to spawn first, unless the player object is auto-spawned as this appears to happen before other spawns.

    A better solution may be using the scene manager's callbacks. Sending a custom message in OnSynchronize arrives on the client before the scene objects are spawned so it sounds like it might achieve what you're after.
     
    CosmoM likes this.
  3. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    That's very useful, thanks!
     
    cerestorm likes this.