Search Unity

Remote player objects spawning before local player object?

Discussion in 'Multiplayer' started by ghostravenstorm, May 18, 2020.

  1. ghostravenstorm

    ghostravenstorm

    Joined:
    Oct 18, 2015
    Posts:
    88
    I have UNET application I'm developing where the player objects on the server are spawning into the client's scene before the local object is created when the client connects. I need the local player object to spawn first because I'm setting the local player object to a static field so that Command functions are easier to use through out the program.

    There's additional info that the player objects need to fetch from a custom TCP server, so each player object, after spawning in a local scene, needs to basically send a post request to the UNET server which then the server delegates this extra data to the client via ClientRpc.

    NOTE: The Unity clients themselves are not connected directly to this TCP server. The Unity server is delegating that connection and the data to & from it. This is done for performance reasons on the clients.

    For all player objects in a local scene to be able to send this request, they need a static reference to the local player object so they can use its Command functions.

    The problem is that some player object are spawning into a local scene on load before the local player object does, so there's not any time to set this static reference before the remote player objects go to invoke on in their Start method. Is there a way I can enforce the instantiation order of player objects when a scene is loaded?

    e.g.

    On Player A's client:
    1) Player A connects to the server.
    2) Player A is first on the server.
    3) Player A's game object is the only one to spawn in Player A's scene.
    4) Player A's object is set as the static reference for Command calls.

    On Player A's client:
    1) Player B connects to the server.
    2) Player B's game object spawns in Player A's scene.
    3) Player B's object in Player A's scene invokes a Command method on Player A's static reference.
    4) Player B's object receives data via ClientRpc in Player A's scene.

    On Player B's client:
    1) Player B connects to the server
    2) Player A's game object spawn in Player B's scene.
    3) Player A's game objects attempts to invoke a Command method on Player B's static reference which is null
    4) Player B's game object spawns in Player B's scene.
    5) Player B's game object is set as the static reference for Command calls.


    If I could swap steps 2 & 3 with 4 & 5, that will fix the issue with the race condition. I wish there was a way to guarantee that the local player object is always first to spawn.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't believe so without modifying the HLAPI code itself (which is publicly available if you wanted to investigate that). I'd just switch all these commands over to Messages to avoid the race condition, or figure out a way to redesign so you don't need to send a Command when a Player prefab spawns.

    The server already knows the Player prefab is spawning, so I don't see why it needs a Command from the client telling it so. But maybe I'm just not seeing what you're doing.