Server Command Call not working when inside an if which checks for hasAuthority and a bool
Hi, let me start by saying that I'm trying to find a way to make this only happen once when a player connects in a function like OnClientConnect or something (and haven't found it yet - any help is welcome), but the actual problem here is this weird behaviour; I'm not sure if it's something I'm doing wrong or if it's a bug... Here's the thing; inside the Update() I call a server command to update the player name and player skill on all the clients joining the server. I wanted it tell the server only once so to call this only once I added a boolean which would make that loop run continuously until it gets updated on the clients. So... Without the added bool condition, it works, the player name and skill is updated, but the server is "spammed" continuously by the Update() method to tell it to update the name on all the other clients even after the update has been done. With the added bool condition, it gets inside the loop (I do get the Debug.Log message in the console) but the player name and skill are never updated ... What gives ? Thanks in advance.
Working code:
public override void OnStartAuthority()
{
CmdSet_pe_Player(PlayerPrefs.GetString("player_name"), PlayerPrefs.GetString("win_ratio"));
}
private void Update()
{
if (hasAuthority)
CmdSet_pe_Player(PlayerPrefs.GetString("player_name"), PlayerPrefs.GetString("win_ratio"));
}
[Command]
void CmdSet_pe_Player(string n, string s)
{
playerName.text = n;
playerSkill.text = s;
RpcSet_pe_Player(n, s);
}
[ClientRpc]
void RpcSet_pe_Player(string n, string s)
{
playerName.text = n;
playerSkill.text = s;
}
Not working code :
bool clientUpdated = false;
public override void OnStartAuthority()
{
CmdSet_pe_Player(PlayerPrefs.GetString("player_name"), PlayerPrefs.GetString("win_ratio"));
}
private void Update()
{
if (hasAuthority && !clientUpdated)
{
Debug.Log("ClientUpdated");
CmdSet_pe_Player(PlayerPrefs.GetString("player_name"), PlayerPrefs.GetString("win_ratio"));
clientUpdated = true;
}
}
[Command]
void CmdSet_pe_Player(string n, string s)
{
playerName.text = n;
playerSkill.text = s;
RpcSet_pe_Player(n, s);
}
[ClientRpc]
void RpcSet_pe_Player(string n, string s)
{
playerName.text = n;
playerSkill.text = s;
}
Your answer
Follow this Question
Related Questions
Calling Bool in Network Command Not Working 0 Answers
Network is spawning more objects then desired 1 Answer
Instantiating over the Network, good way? 1 Answer
CommandAttribute doesn't work properly 1 Answer
i just can't seem to figure out how to make a voting system, using SyncVar hooks and commands 0 Answers