I have a RPC that sets the player text mesh to a name in the onstartlocal player, but if a new player joins, the text mesh has not been set. The only way to fix this is to call the rpc from update every frame. Is there a way to load previously called RPC’s.
By the way, the rpc is actually called from a command but that’s not the point
This is by design. If a client joins at a later time, he will not receive RPCs which were send in the past.
However, there are multiple solutions to work around this.
For example, a solution is that a joining client sends a command to the server to request up-to-date data. When the server receives this command, he can respond by sending a target RPC to the requesting client with the current data. Of course, you could also implement this logic with NetworkMessages (even before the player object has been spawned if you want).
Also, you can check for data which can make good use of SyncVars. These are automatically synced for new clients and guaranteed to be up-to-date at the time of the OnStartClient callback.
For your particular use case, I would not consider to call an RPC on each update as it would be a huge waste of bandwidth.
Yes that is how the sync var hook works, but note that is not called on object instantiation, which is why you need to do the setting in the OnStartClient as well as have it in the hook.
K I tried it out, and I know this is the right way to do it, but something isn’t working properly. Ill send you my script (before attempted with hooks) and maybe you could tell me what im doing wrong.
Here’s my whole script
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
[RequireComponent(typeof(Player))]
public class PlayerSetup : NetworkBehaviour {
[SerializeField]
Behaviour[] componentsToDisable;
[SerializeField]
GameObject[] gameObjectsToDisable;
[SerializeField]
string remoteLayerName = "RemotePlayer";
[SerializeField]
string dontDrawLayerName = "DontDraw";
[SerializeField]
GameObject PlayerGraphics;
[SerializeField]
GameObject playerUIPrefab;
[HideInInspector]
public GameObject playerUIInstance;
public GameObject[] ArmObjects;
NetworkManager networkManager;
GameObject networkManagerObject;
[SyncVar]
public int playerColourIndex;
public TextMesh nameText;
public Material currentPlayerMaterial;
public Material[] playerMaterials;
public GameObject[] playerObjectsWithMaterials;
[SyncVar]
public string playerName = "";
// Use this for initialization
void Start() {
networkManager = NetworkManager.singleton;
if (!isLocalPlayer)
{
DisableComponents();
AssignRemoteLayer();
}
if (isLocalPlayer)
{
DisableGameObjects();
SetLayerRecursively(PlayerGraphics, LayerMask.NameToLayer(dontDrawLayerName));
SetArmLayerRecursively();
playerUIInstance = Instantiate(playerUIPrefab);
playerUIInstance.name = playerUIPrefab.name;
PlayerUI ui = playerUIInstance.GetComponent<PlayerUI>();
if (ui == null)
Debug.LogError("NoPlayerUI");
ui.SetController(GetComponent<PlayerController>());
GetComponent<Player>().SetupPlayer();
}
}
void SetLayerRecursively (GameObject obj, int newLayer)
{
obj.layer = newLayer;
foreach (Transform child in obj.transform)
{
SetLayerRecursively(child.gameObject, newLayer);
}
}
void SetArmLayerRecursively()
{
for (int i = 0; i < ArmObjects.Length; i++)
{
Util.SetLayerRecursively(ArmObjects[i], LayerMask.NameToLayer("WeaponLayer"));
}
}
[Command]
void CmdSetPlayerName (string _name)
{
playerName = _name;
RpcSetPlayerName(playerName);
}
[ClientRpc]
void RpcSetPlayerName(string _name)
{
nameText.text = _name;
}
[Command]
void CmdSetPlayerColour(int _colour)
{
playerColourIndex = _colour;
RpcSetPlayerColour(playerColourIndex);
}
[ClientRpc]
void RpcSetPlayerColour(int _colour)
{
switch (_colour)
{
case 1:
currentPlayerMaterial = playerMaterials[0];
break;
case 2:
currentPlayerMaterial = playerMaterials[1];
break;
case 3:
currentPlayerMaterial = playerMaterials[2];
break;
case 4:
currentPlayerMaterial = playerMaterials[3];
break;
case 5:
currentPlayerMaterial = playerMaterials[4];
break;
}
for (int i = 0; i < playerObjectsWithMaterials.Length; i++)
{
playerObjectsWithMaterials[i].GetComponent<Renderer>().material = currentPlayerMaterial;
}
}
public override void PreStartClient()
{
playerName = PlayerPrefs.GetString("playerName");
playerColourIndex = PlayerPrefs.GetInt("playerColour");
}
public override void OnStartLocalPlayer()
{
CmdSetPlayerName(playerName);
CmdSetPlayerColour(playerColourIndex);
}
public override void OnStartClient()
{
string _netID = GetComponent<NetworkIdentity>().netId.ToString();
Player _player = GetComponent<Player>();
GameManager.RegisterPlayer (_netID, _player, "Player");
}
void AssignRemoteLayer ()
{
gameObject.layer = LayerMask.NameToLayer(remoteLayerName);
}
void DisableComponents ()
{
for (int i = 0; i < componentsToDisable.Length; i++)
{
componentsToDisable[i].enabled = false;
}
}
void DisableGameObjects()
{
for (int i = 0; i < gameObjectsToDisable.Length; i++)
{
gameObjectsToDisable[i].SetActive(false);
}
}
// Update is called once per frame
void Update ()
{
CmdSetPlayerName(playerName);
CmdSetPlayerColour(playerColourIndex);
}
void OnDisable ()
{
Destroy(playerUIInstance);
if (isLocalPlayer)
GameManager.instance.SetSceneCameraActive(true);
GameManager.UnRegisterPlayer(transform.name);
}
}