Having problem with synchronize player's nickname

Options

I tried to synchronize players(NetworkObject) nickname by using


[Networked(OnChanged = nameof(OnNickNameChanged))]

[Capacity(20)]

public NetworkString<_32> NetNickName { get; set; }


and OnNickNameChangedStatic is


public static void OnNickNameChanged(Changed<ExamplePlayer> changed)

{

    changed.Behaviour.Synchronize();

}


Synchronize is


public void Synchronize()

{

var nick = "";

NetNickName.Get(ref nick);

textMeshPro.text = nick;

}


anyway this is my nickname synchronize code


if I change NetNickName in host it work

but if I change NetNickName in client it doesn't work


if i use changed.loadNew() and changed.loadOld to know what happend


if I change NetNickName in host

old data is empty and new data is nickname what I set


I change NetNickName in client

first old data is empty and new data is nick name what I set

but suddenly after it show message again old data is nick name what i set and new data is empty data



if I run 2 game host, client and change host's nickname in host game it synchronized both in host and client


but when I change client's nickname in client game it show 2 message that I said before and in host game nothing happen


I want to know why change NetworkStirng work in host work and in client doesn't work


ps. host and client both has it's player's(NetworkObject) Input Authority

Best Answer

  • emotitron
    emotitron ✭✭✭
    Answer ✓
    Options

    NetworkString sets its capacity with the <_x> value (in your case above that is _32), so the [Capacity] attribute should be removed.

    Only the server can change Networked values with authority. Changes on clients (such as in the case with prediction) will be overwritten by server value changes. If you want to change that name as a client, you would want to send an RPC to the server asking it to change the name.

Answers

  • emotitron
    emotitron ✭✭✭
    Answer ✓
    Options

    NetworkString sets its capacity with the <_x> value (in your case above that is _32), so the [Capacity] attribute should be removed.

    Only the server can change Networked values with authority. Changes on clients (such as in the case with prediction) will be overwritten by server value changes. If you want to change that name as a client, you would want to send an RPC to the server asking it to change the name.