#Lets do an example with NetworkVariables

1 messages · Page 1 of 1 (latest)

sage cedar
#

You need to use OnNetworkSpawn() instead of Awake(). Awake happens before connecting to the network
https://docs-multiplayer.unity3d.com/netcode/current/basics/networkvariable/#initializing-a-networkvariable

NetworkVariables are a way of synchronizing properties between servers and clients in a persistent manner, unlike RPCs and custom messages, which are one-off, point-in-time communications that aren't shared with any clients not connected at the time of sending. NetworkVariables are session-mode agnostic and can be used with either a client-serve...

rapid seal
#

So all NetworkVariables will be synced by the time OnNetworkSpawn happens?

sage cedar
#

Thats right

rapid seal
#
public class PlayerInfo: NetworkBehaviour
{
    [SerializeField]
    private TextMeshPro _nameText;

    private NetworkVariable<FixedString32Bytes> playerName = new();
    private NetworkVariable<ulong> clientID = new();
    private NetworkVariable<ulong> steamID = new();
    
    public override void OnNetworkSpawn()
    {
        playerName.OnValueChanged += UpdateName;
        if (!IsOwner) return;
        
        playerName = new NetworkVariable<FixedString32Bytes>(SteamClient.Name);
        steamID = new NetworkVariable<ulong>(SteamClient.SteamId);
    }
    
    private void UpdateName(FixedString32Bytes oldName, FixedString32Bytes newName)
    {
        _nameText.text = newName.ToString();
    }
}```
So I can just do this instead
#

Is there a way to get the clientID locally?

sage cedar
#

Network Behavior has OwnerClientId or you can use NetworkManager.Singleton.LocalClientId

rapid seal
#

Okay that's really helpful thanks.

I just add clientID = new NetworkVariable<ulong>(OwnerClientId);

sage cedar
rapid seal
#

oh...

sage cedar
rapid seal
sage cedar
#

exactly

rapid seal
#

On a collision they can do other.GetComponent<NetworkBehaviour>().OwnerClientId?

sage cedar
#

Yea that will work

rapid seal
#

Thanks, I really appreciate the help, I was struggling to get a hang of how it all works because all the guides online were mixing me up with old Netcode logic and very poorly written new stuff

sage cedar
#

The official docs are very good and kept reasonably up to date. Code Monkey still has the best NGO tutorials out there.

rapid seal
#

Yeh I've been reading a bit more on the docs recently and got the hang of Rpc. I'll have to look at Code Monkey, I've not watched any of his newer stuff.

A big problem was figuring out how to mix steamworks with this, the help available for Steamworks and the facepunch transport is terrible

#

So I'm really glad to have it out of the way now!

#

Okay so I have this on the player

public class PlayerInfo: NetworkBehaviour
{
    [SerializeField]
    private TextMeshPro _nameText;

    private NetworkVariable<FixedString32Bytes> playerName = new();
    private NetworkVariable<ulong> steamID = new();
    private NetworkVariable<int> playerNumber = new();
    // To get the clientID just get the NetworkBehaviour component and get OwnerClientId
    
    public override void OnNetworkSpawn()
    {
        playerName.OnValueChanged += UpdateName;
        if (!IsOwner) return;
        
        playerName.Value = SteamClient.Name;
        steamID.Value = SteamClient.SteamId;
        GameNetworkManager.RequestPlayerNumber();
    }
    
    private void UpdateName(FixedString32Bytes oldName, FixedString32Bytes newName)
    {
        _nameText.text = newName.ToString();
    }
}```

And I have this in my NetworkManager
```csharp
    [Rpc(SendTo.Server)]
    public static int RequestPlayerNumber(ServerRpcParams rpcParams = default)
    {
        for (int i = 1; i <= 4; i++)
        {
            if (!_playerNumbers.ContainsKey(i))
            {
                ulong senderID = rpcParams.Receive.SenderClientId;
                Debug.Log($"Assigning ID {i} to player {senderID}");
                _playerNumbers[i] = senderID;
                return i;
            }
        }
        Debug.LogError("All 4 slots are taken up but we are trying to assign a new player? This will break things");
        return 1;
    }```