Multiplayer Storing Data on The Host Machine for Clients

Hey guys,

So I’ve decided I’m going to be keeping my data on the host machine and I just need to jump into the network code some more myself.

This seems to help answer my questions

I’ve edited this post as my current questions have been answered.

If anyone is curious about the basics of it.

A [Command] has to be in a script that is attached to the PlayerObject.

A [ClientRpc] can be on a script attached to another object.

YOUR_RPC_HOLDER_CLASS is just to show that you can have a class containing your RPC calls and you can attach it to whatever object you want as long as that object is added to the Registered Spawnable Prefabs list on the NetworkManager.

public class YOUR_RPC_HOLDER_CLASS : NetworkBehaviour
{
    [SyncVar]
    string textForRPC;

    [ClientRpc]
    public void RpcSetText(string text)
    {
        textForRPC = text;
    }
}

The SOME_RANDOM_CLASS might need to be attached to your playerObject, I’m not exactly sure on that one.
If you want this script to also control your [Command] calls, then it definitely needs to be attached to the playerObject. I’ve included one script with and one without below.

public class SOME_RANDOM_CLASS : MonoBehaviour
{
    YOUR_RPC_HOLDER_CLASS rpcClass;

    void Start()
    {
        rpcClass = FindObjectOfType<YOUR_RPC_HOLDER_CLASS>();
    }

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            rpcClass.RpcSetText("RPC::We've Changed!");
        }
    }

}
public class SOME_RANDOM_CLASS : MonoBehaviour
{
    YOUR_RPC_HOLDER_CLASS rpcClass;

    [SyncVar(hook = "SetTextClient")] string textForCommand;

    string otherTextForCommand;

    void Start()
    {
        rpcClass = FindObjectOfType<YOUR_RPC_HOLDER_CLASS>();
    }

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            rpcClass.RpcSetText("RPC::We've Changed!");
        }
    }

    [Command]
    public void CmdSetText(string text)
    {
        textForCommand = text;
    }

    public void SetTextClient(string text)
    {
        otherTextForCommand = text;
    }
}

Hopefully that helps you get started with sending info between the Server & Client and then sending data between the Client & Server.

There might be better ways to do this / more optimal, but currently this works for me!

The example is simply changing a string which you could read or display from somewhere else. The easiest way to display it on both client and server would be to create a UI Text element that gets changed via this string.