This script is attached to Player Object and assign Team for each player object on Start Game.
[The Current Issues]
However, I can only successfully assign team to every player on the Server side(When I’m a Host). On the client side, when I join the Game my player object will get to assigned into a team, but this only work for my own’ player object. For the other player it did not manage to get sync and update to me(When I’m a Client).
In other words, only clients don’t see the team assigning when they join the game. I know the issue is that client must hook up to latest information on the server but how? So if anyone can tell me the solution of what is wrong and what is right, I will be appreciate it!
Networking Engine & NetworkBehaviour are in the script. Below is my script:
[SyncVar(hook = "AssignTeam")] private int TeamNo;
void Start()
{
int IndexRandomise = Random.Range(1,3);
TeamNo = IndexRandomise;
if(!isLocalPlayer)
{
}
else
{
AssignTeam(TeamNo);
}
}
}
public void AssignTeam(int _TeamNo)
{
if(!isLocalPlayer)
return;
if (_TeamNo == 1)
{
this.transform.GetComponent<SpriteRenderer>().color = Color.blue;
}
else
{
this.transform.GetComponent<SpriteRenderer>().color = Color.red;
}
CmdTransmitToAll(_TeamNo);
}
[Command]
void CmdTransmitToAll(int _TeamNo)
{
if (_TeamNo == 1)
{
this.transform.GetComponent<SpriteRenderer>().color = Color.blue;
}
else
{
this.transform.GetComponent<SpriteRenderer>().color = Color.red;
}
}
To make it work you need to be sure that assigning TeamNo will be on server side so it would be easier if you could do it in a [Cmd] method like this:
void OnStartClient()
{
CmdRandomTeam();
}
[Command]
public void CmdRandomTeam()
{
int IndexRandomise = Random.Range(1,3);
TeamNo = IndexRandomise;
}
@Chom1czek , hey budds thank you for your reply! I follow your technique which do the calculation on the server. But it seems when a player that just join the game are unable to see the other player team that has been assigned. I’m trying to inspect the problem, I’m now trying to figure out “How to sync when player just join the game”. But if any of you understand, you may tell me.
Latest Code below:
[SyncVar(hook = "TeamAssign")]
private int TeamNo;
void Start()
{
if(!isLocalPlayer)
{
}
else
{
CmdRandomTeam();
}
}
[Command]
public void CmdRandomTeam()
{
int IndexRandomise = Random.Range(1,3);
TeamNo = IndexRandomise;
TeamAssign(TeamNo);
}
void TeamAssign(int _TeamNo)
{
if (_TeamNo == 1)
{
this.transform.GetComponent<SpriteRenderer>().color = Color.blue;
}
else
{
this.transform.GetComponent<SpriteRenderer>().color = Color.red;
}
}
You don’t have to all TeamAssign(TeamNo); in the command method. It will be called either way through SyncVar hook method. I will try to investigate your problem in my spare time.
Edit: You need to also call your sync hook method in start method for init purpose, I will try to implement the same thing and see how it works.
Edit2: I just tested it on my own and as I said, you need to call your Hook methond OnStart to cause the new player to initialize values and on top of that you need to update hook to look like that:
[SyncVar] public T someValue;
public void AnyHookMethod(T someValue)
{
this.someValue = someValue; // <--- This is very important,
//because if you don't have hook method then it's done automatically
//but since you override it then you need to take care of it on your own
}
1 Like
@Chom1czek , you know what. It solvedddd~~ Yes I didn’t know it was that simple, just put the “Hook” method on start. So that anyone that just joined will automatically synchronized. Thank you budds! Appreciated!