[Solved] How to sync network object changes for late client joining/reconnecting clients game in netcode?

I understand I can use network variables, I am unsure if this is the way to go. I also want to know how I can create variable objects that automatically sync everything with server.

In my game a network variable for skincolor on value changed calls a method to slowly lerp to current value over floatX seconds. But for late join clients, I want to skip lerping the color for immediate effect. How can I do this? For the new player, I want immediate sync, not a method to lerp it through OnValueChanged.


Infact what I really want is for already joined clients to get a clientRpc to set the changes, then on server side I want to change the NetworkVariable which does not invoke onvaluechanged on these clients I sent the clientrpc to. But for late clients have its onvaluechanged for immediate syncing called.

Something like this. How can I do this?

1 Answer

1

Ok so I see that for newly connected clients, they can sync immedately with networkvariable.value. And for already connected clients, on value changed, they can call custom code.
Here is what I did in my player’s class:

public NetworkVariable<Color> SkinColor = new (default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);

public override void OnNetworkSpawn()
    {
        base.OnNetworkSpawn();
        // subscribe on value changed, or send a clientRpc. In this case subscribe.
        SkinColor.OnValueChanged += (previous, current) => SetSkin(SkinColor.Value, 7f);
        // To immediately sync for late join players.
        SetSkin(SkinColor.Value, 0f);
    }
    public override void OnNetworkDespawn()
    {
        SkinColor.OnValueChanged -= (previous, current) => SetSkin(SkinColor.Value, 7f);
        base.OnNetworkDespawn();
    }

public void SetSkin(Color color, float time)
{
    spriterenderer.DOColor(color, time);
}

My player skin color was red, I set it to blue, it lerped the value on connected, but set immediately for new. Good x. ONE MORE THING. I am being an idiot by subscribing and then trying to unsubscribe a completely new lambda, even though it has the same code. No, this does not work as it does not unsubscribe the initial lambda, and I need to actually create a function to subscribe. I did it this way because I like to play around with code for learning purposes. And I’m leaving it in for future reference for myself incase I forget that each lambda is a new functions. Yes I do forget such simple to remember things :. ( You don’t? haha.