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 are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice

Question Questions Regarding MLAPI Code

Discussion in 'Netcode for GameObjects' started by jettzeong, Aug 26, 2021.

  1. jettzeong

    jettzeong

    Joined:
    Nov 5, 2018
    Posts:
    62
    Code (CSharp):
    1.  
    2. using MLAPI;
    3. using MLAPI.Messaging;
    4. using MLAPI.NetworkVariable;
    5. using UnityEngine;
    6.  
    7. namespace HelloWorld
    8. {
    9.     public class HelloWorldPlayer : NetworkBehaviour
    10.     {
    11.         public NetworkVariableVector3 Position = new NetworkVariableVector3(new NetworkVariableSettings
    12.         {
    13.             WritePermission = NetworkVariablePermission.ServerOnly,
    14.             ReadPermission = NetworkVariablePermission.Everyone
    15.         });
    16.  
    17.         public override void NetworkStart()
    18.         {
    19.             Move();
    20.         }
    21.  
    22.         public void Move()
    23.         {
    24.             if (NetworkManager.Singleton.IsServer)
    25.             {
    26.                 var randomPosition = GetRandomPositionOnPlane();
    27.                 transform.position = randomPosition;
    28.                 Position.Value = randomPosition;
    29.             }
    30.             else
    31.             {
    32.                 SubmitPositionRequestServerRpc();
    33.             }
    34.         }
    35.  
    36.         [ServerRpc]
    37.         void SubmitPositionRequestServerRpc(ServerRpcParams rpcParams = default)
    38.         {
    39.             Position.Value = GetRandomPositionOnPlane();
    40.         }
    41.  
    42.         static Vector3 GetRandomPositionOnPlane()
    43.         {
    44.             return new Vector3(Random.Range(-3f, 3f), 1f, Random.Range(-3f, 3f));
    45.         }
    46.  
    47.         void Update()
    48.         {
    49.             transform.position = Position.Value;
    50.         }
    51.     }
    52. }
    I am quite new to Unity and recently just started using MLAPI. Can I know why do we need to put (ServerRpcParams rpcParams = default)? What function does it have? And what happens if we dont put it in? I read the documentation but my understanding of it is still cloudy at best

    Code (CSharp):
    1.       [ServerRpc]
    2.         void SubmitPositionRequestServerRpc(ServerRpcParams rpcParams = default)
    3.         {
    4.             Position.Value = GetRandomPositionOnPlane();
    5.         }
    Any help is greatly appreciated!
     
  2. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    There is no need to include `ServerRpcParams` if you don't use it. One use case of the `ServerRpcParams` is to get access to the sender client of the RPC (rpcParams.Receive.SenderClientId`.
     
  3. jettzeong

    jettzeong

    Joined:
    Nov 5, 2018
    Posts:
    62
    Thanks for the explanation luke. Also if you dont mind, can you also give an example on a time where we would need to get access to the sender client of the RPC?
     
  4. jettzeong

    jettzeong

    Joined:
    Nov 5, 2018
    Posts:
    62
    And also, for this section for the code,


    Code (CSharp):
    1. public override void NetworkStart()
    2.         {
    3.             Move();
    4.         }
    is it really neccessary to override? And what are we overriding?
     
  5. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    NetworkStart gets called whenever a NetworkObject gets spawned. The base function is just empty so you are not overriding any existing behavior. You can think of it as something like `Awake` or `Start` it just uses a different syntax.
     
  6. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    For instance if you have an object in the scene with which any player can interact with. Let's say a button which they can press and if they do so a text changes to their name. probably not the best example but whatever :D. In that case you could use a ServerRPC to send that button press up and you would use the owner id to figure out which player pressed.
     
unityunity