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 Likes
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:
using Unity.Netcode;
using UnityEngine;
public class NetworkVariableTest : NetworkBehaviour
{
private NetworkVariable<float> ServerNetworkVariable = new NetworkVariable<float>(default,
NetworkVariableReadPermission.Everyone, // Everyone
NetworkVariableWritePermission.Server);
private NetworkVariable<float> ClientNetworkVariable = new NetworkVariable<float>(default,
NetworkVariableReadPermission.Everyone, // Everyone
NetworkVariableWritePermission.Owner);
private float last_t = 0.0f;
public override void OnNetworkSpawn()
{
if (IsServer)
{
ServerNetworkVariable.Value = 0.0f;
Debug.Log("Server's var initialized to: " + ServerNetworkVariable.Value);
} else
{
ClientNetworkVariable.Value = 0.0f;
Debug.Log("Client's var initialized to: " + ClientNetworkVariable.Value);
}
}
void Update()
{
var t_now = Time.time;
if (IsServer)
{
ServerNetworkVariable.Value = ServerNetworkVariable.Value + 0.1f;
if (t_now - last_t > 0.5f)
{
last_t = t_now;
Debug.Log("Server set its var to: " + ServerNetworkVariable.Value);
}
} else {
ClientNetworkVariable.Value = ClientNetworkVariable.Value + 0.1f;
if (t_now - last_t > 0.5f)
{
last_t = t_now;
Debug.Log("Client set its var to: " + ClientNetworkVariable.Value);
}
}
}
}
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.
4 Likes
Great work! This is a part of lerning path by unity. We think of everything ourselves)
1 Like
THANK YOU! I’ve been trying to figure out the error for days.
Thank you so much for the solution. Did some tweaking to it.
First and foremost, the variable declarations can be shortened since the default settings for NetworkVariables are as follows. Which is the same as the ServerNetworkVariables parameters.
- default
- NetworkVariableReadPermission.Everyone
- NetworkVariableWritePermission.Server
For the ClientNetworkVariable only the writePerm is different so we can specify only that parameter and use the default for the rest.
private NetworkVariable<float> ServerNetworkVariable = new();
private NetworkVariable<float> ClientNetworkVariable = new(writePerm: NetworkVariableWritePermission.Owner);
Also hade some minor errors while running the code with simple “else” statements so changed it to check if its the owner before assigning any values to the variables
else if (IsOwner)
{
ClientNetworkVariable.Value = 0.0f;
Debug.Log("Client's var initialized to: " + ClientNetworkVariable.Value);
}
-----
else if (IsOwner)
{
ClientNetworkVariable.Value += 0.1f;
Debug.Log("Server set its var to: " + ServerNetworkVariable.Value +
", has client var at: " + ClientNetworkVariable.Value);
}
2 Likes