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.

Golden Path Module 2 exercise is broken/incomplete

Discussion in 'Community Learning & Teaching' started by jblandino, Jun 16, 2022.

  1. jblandino

    jblandino

    Joined:
    May 10, 2022
    Posts:
    2
    As part of my Unity learning experience, I was advised to learn netcode with the Golden Path. In the course of participating in these exercises, I found that in Module 2, (Golden Path Module Two | Unity Multiplayer Networking (unity3d.com)) the script provided for class NetworkVariableTest is incomplete; it is missing the part of the code that checks for client spawns and initializes the client network variable, and as a result, there is a compile error that prevents running the scene.



    I attempted to reproduce the functionality of this class, after looking at the old MLAPI usage that is still shown in the provided video, but as I'm unable to get my command line run client and host to share values, I have obviously failed, and so far am unable to fully grasp the concepts of this exercise. I'd still like to figure it out, however, so I'm hoping either someone in the community can show me how to do the same thing with netcode, or if someone from Unity could fix the exercise itself so that it's usable again.
     
  2. jblandino

    jblandino

    Joined:
    May 10, 2022
    Posts:
    2
    Update:

    By reverse-engineering the calls to the MLAPI-specific permissions settings, I found an equivalent for netcode's implementation here that seems to do the trick. If you replace the code for the NetworkVariableTest class with this, it should log the values correctly:

    Code (CSharp):
    1. using Unity.Netcode;
    2. using UnityEngine;
    3.  
    4. public class NetworkVariableTest : NetworkBehaviour
    5. {
    6.     private NetworkVariable<float> ServerNetworkVariable = new NetworkVariable<float>(default,
    7.         NetworkVariableReadPermission.Everyone, // Everyone
    8.         NetworkVariableWritePermission.Server);
    9.     private NetworkVariable<float> ClientNetworkVariable = new NetworkVariable<float>(default,
    10.         NetworkVariableReadPermission.Everyone, // Everyone
    11.         NetworkVariableWritePermission.Owner);
    12.     private float last_t = 0.0f;
    13.  
    14.  
    15.     public override void OnNetworkSpawn()
    16.     {
    17.  
    18.         if (IsServer)
    19.         {
    20.             ServerNetworkVariable.Value = 0.0f;
    21.             Debug.Log("Server's var initialized to: " + ServerNetworkVariable.Value);
    22.         } else
    23.         {
    24.             ClientNetworkVariable.Value = 0.0f;
    25.             Debug.Log("Client's var initialized to: " + ClientNetworkVariable.Value);
    26.         }
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         var t_now = Time.time;
    32.         if (IsServer)
    33.         {
    34.             ServerNetworkVariable.Value = ServerNetworkVariable.Value + 0.1f;
    35.             if (t_now - last_t > 0.5f)
    36.             {
    37.                 last_t = t_now;
    38.                 Debug.Log("Server set its var to: " + ServerNetworkVariable.Value);
    39.             }
    40.         } else {
    41.             ClientNetworkVariable.Value = ClientNetworkVariable.Value + 0.1f;
    42.             if (t_now - last_t > 0.5f)
    43.             {
    44.                 last_t = t_now;
    45.                 Debug.Log("Client set its var to: " + ClientNetworkVariable.Value);
    46.             }
    47.         }
    48.     }
    49. }
    You need code blocks to check for Client side encounters, and you need to add code in the network variable declarations to set the permissions through the NetworkVariable base class constructor. I've done that for the globals, and I've changed the logging around a bit in my example here, so you might need to shift it to match the exercise's old behavior, if that's what you want. Hopefully this will help! I'm off to the next part of the exercise now. Hopefully it'll be more fleshed out than this part was, lol.
     
  3. Grave188

    Grave188

    Joined:
    Dec 13, 2017
    Posts:
    2
    Great work! This is a part of lerning path by unity. We think of everything ourselves)
     
unityunity