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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unet Messages Help

Discussion in 'UNet' started by Happy_Jingle, Mar 12, 2016.

  1. Happy_Jingle

    Happy_Jingle

    Joined:
    Mar 9, 2014
    Posts:
    103
    I've looked over the manual for network messages: http://docs.unity3d.com/Manual/UNetMessages.html
    I have a couple questions though, I have a player object, I want to send an integer message to the client side player from the server side so:
    1. Where should I run Init(NetworkClient client)? I would guess it's run client side Start()?
    2. How do I access my player's NetworkClient to pass to Init(NetworkClient client)?

    EDIT:
    I'm referring to the following block
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using UnityEngine.Networking.NetworkSystem;
    4.  
    5. public class Begin : NetworkBehaviour
    6. {
    7.     const short MyBeginMsg = 1002;
    8.  
    9.     NetworkClient m_client;
    10.  
    11.     public void SendReadyToBeginMessage(int myId)
    12.     {
    13.         var msg = new IntegerMessage(myId);
    14.         m_client.Send(MyBeginMsg, msg);
    15.     }
    16.  
    17.     public void Init(NetworkClient client)
    18.     {
    19.         m_client = client;
    20.         NetworkServer.RegisterHandler(MyBeginMsg, OnServerReadyToBeginMessage);
    21.     }
    22.  
    23.     void OnServerReadyToBeginMessage(NetworkMessage netMsg)
    24.     {
    25.         var beginMessage = netMsg.ReadMessage<IntegerMessage>();
    26.         Debug.Log("received OnServerReadyToBeginMessage " + beginMessage.value);
    27.     }
    28. }
     
    Last edited: Mar 12, 2016
  2. sovium

    sovium

    Joined:
    Mar 8, 2016
    Posts:
    27
    If you are using NetworkManager, you can get your client from the NetworkManager with NetworkManager.instance.client. If you are not using the NetworkManager, you probably have created your NetworkClient somewhere.

    Note that NetworkClient.Send will send a message to the server. So in this case your OnServerReadyToBeginMessage is ran on the server only. If you want to send a message to a client, you need to use NetworkServer.SendToClient(int connectionId, short msgType, message)
    or
    NetworkServer.SendToClientOfPlayer(GameObject player, short msgType, message)
    where ever you want to send a message to a client.

    You also need to register a handler for the message on the client with
    NetworkClient.RegisterHandler(short msgType, CallBack);
     
  3. Happy_Jingle

    Happy_Jingle

    Joined:
    Mar 9, 2014
    Posts:
    103
    Thanks for the response! I'm a bit confused about your last statement, if I'm to get my NetworkClient from my NetworkManager, then how is my non-host client going to register a handler to NetworkClient (since they can't access NetworkManager?)?

    Code (CSharp):
    1. NetworkClient.RegisterHandler (MyBeginMsg, OnClientReadyToBeginMessage);
    Just like that? EDIT nevermind the above doesn't work, still don't know what to do exactly.
     
    Last edited: Mar 13, 2016
  4. Happy_Jingle

    Happy_Jingle

    Joined:
    Mar 9, 2014
    Posts:
    103
    Also I don't know if I'm doing something wrong but NetworkManager.instance doesn't seem to reference anything?
    EDIT: it seems the client does have access to the NetworkManager, also a client instance is access via NetworkManager.client

    Everything seems to be working!
     
    Last edited: Mar 13, 2016
  5. sovium

    sovium

    Joined:
    Mar 8, 2016
    Posts:
    27
    Nice that you got it working. The network manager is just a component that does a lot of things automatically, like creating a server or a client. That means you can access the client and the server through the network manager. The RegisterHandler method on both client and server just registers an access point which is invoked when they receive a message of type x.

    Yeah I made a mistake, obviously you can not staticly access the NetworkClient.RegisterHandler, instead you should access it from your instance of the NetworkClient

    NetworkClient client;

    void Foo(){
    client = NetworkManager.instance.client;
    client.RegisterHandler...
    }