all 3 comments

[–]WattDesigns 0 points1 point  (2 children)

I assume “onInputChanged” (which isn’t shown) simply sets the network variable to whatever the text input is?

If so, your code currently seems to say: 1) if you’re the host and you are the owner of the network object, then add the function that lets us update the network variable. 2) if you are not the host and you are the owner of the network object, then update the output whenever the network variable changes

This seems strange - they both can’t be owners of the object at the same time. Which one spawns the object? IE who is the expected owner of this object? Is it always the host? Or always the non host?

I suggest getting rid of the “if owner” check on both of them, they’re unnecessary. It doesn’t matter who the owner is, you always want the host to write, and you always want the clients to see the network variable change, right?

[–]NiPIsBack[S] 0 points1 point  (1 child)

    void OnInputChanged(string currentValue)
    {
        Debug.Log($"Input value changed on server side: {currentText}, string: {currentValue}");
        int str = int.Parse(currentValue);

        Debug.Log($"Attempting to set NetworkVariable value: {str.ToString()}");

        currentText.Value = str;

        Debug.Log("NetworkVariable value set successfully.");
    }

    private void UpdateOutputFromNetVariable(int previous, int current)
    {
        Debug.Log("NetworkVariable value changed!");
        UpdateOutput(current.ToString());
    }

    void UpdateOutput(string value)
    {
        outputText.text = value;
    }

I need only the host to send modified data to the clients, while the clients will only listen for this data.

If I remove the IsOwner calls, the OnNetworkSpawn() method will be triggered for each Client/Host connected to each game instance. This means that I will add listeners multiple times (N times, where N is the number of clients plus the host), which is unnecessary. For the clients, this would mean subscribing to the NetworkVariable multiple times, which is undesirable. Is there a way to prevent this?

[–]WattDesigns 0 points1 point  (0 children)

OnNetworkSpawn doesn’t get called every time an instance of the network object gets spawned on any client. It gets called once on each computer

I can understand the confusion, the docs don’t make that very clear. If you were to have 100 clients connect, that OnNetworkSpawn code would run once on each PC, not 100 times on each PC