Search Unity

Send init messages to client on connect

Discussion in 'Multiplayer' started by oLDo, Sep 6, 2017.

  1. oLDo

    oLDo

    Joined:
    Mar 14, 2017
    Posts:
    55
    Hi.

    I have some multiplayer logic which is builded on network messages. I need to send to new connected client some messages. First client is also server.

    I tried many methods and different ways in NetworkManager like OnClientConnect, OnServerConnect, OnServerAddPlayer and some more. I tried coroutine with second delay. But nothings works how I want.

    The example of my imagination:
    Code (CSharp):
    1. public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    2. {
    3.   base.OnServerAddPlayer(conn, playerControllerId);
    4.  
    5.   NetworkServer.SendToClient(playerControllerId, <someMyMessageId>, <someMyCustomMessage>);
    6. }
    That send message to specified client don't work. Just nothing happen. How can I do it?
     
  2. Leoo

    Leoo

    Joined:
    May 13, 2013
    Posts:
    96
    Why would you need to send a message to the (Host) ? , Check if you are indeed the host using that NetworkConnection getting passed in into the "OnServerAddPlayer", then just call the right func with the message as a parameter directly. Even if you send it as a message there will be a delay EVEN if the msg is getting send&received by the same machine, thats how UNET works. xd
     
  3. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    If you want it to happend after the player object has been spawned. Easiest is to just have a MonoBehaviour with a start method. If you want it before. Just insert it before your code that spawns the player object.
     
  4. oLDo

    oLDo

    Joined:
    Mar 14, 2017
    Posts:
    55
    I'm not sure if I understand, or if you don't understand :)

    Leoo
    I'm not sending message to the (Host). I'm sending message to client from server, it's NetworkServer.SendToClient().

    TwoTen
    I can't handle this on the client side, because this client don't have all important information which handle the server. I need to send messages from server to this client.

    Another explanation
    I have one computer, which is client and server (host). Then I have another client computer. Both computers are clients with same scene. Most of network logic is with network messages. And my reason, why I'm want to solve this, it's if host is created and the player do some stuff and after then another client connects (second computer), this new client don't have this stuff informations. I need to send some messages only to this client from server.
     
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    If you make the properties in question SyncVars, then when the new client connects it will automatically get the up to date values.
     
  6. Deleted User

    Deleted User

    Guest

    Put this script on your Player's prefab.

    Code (CSharp):
    1. public class WelcomeMessage : NetworkBehaviour
    2. {
    3.     public override void OnStartServer()
    4.     {
    5.          // TargetRpc or connectionToClient.Send()
    6.     }
    7. }
     
    oLDo likes this.
  7. oLDo

    oLDo

    Joined:
    Mar 14, 2017
    Posts:
    55
    I was thinking about it, but my system it's too complex to do it with syncVar. It's VR with leapmotion, and we are creating procedural mesh in runtine.