#how can I sync [SyncList] variable between client

2 messages · Page 1 of 1 (latest)

burnt compass
#

I have a struct like this

[System.Serializable]
public class PlayerInfo
{
    public int ConnId;
    public int TeamIndex;
    public int PlayerIndex;
}

[System.Serializable]
public class TeamInfo
{
    public int TeamIndex;
    public List<PlayerInfo> Player;

    public TeamInfo(int teamIndex, List<PlayerInfo> player = null)
    {
        TeamIndex = teamIndex;
        Player = new List<PlayerInfo>();
    }

    public TeamInfo() { }
}

//...
public readonly SyncList<TeamInfo> TheTeamInfo = new SyncList<TeamInfo>();

public override void  OnStartServer()
{
    base.OnStartServer();
    // init teaminfo, two teams
    for (int i = 0; i < 2; i++)
    {
        TheTeamInfo.Add(new TeamInfo(i));
    }
}

//...
[Command]
public void CmdAddPlayerToTeam(int teamIndex, int playerIndex, NetworkConnectionToClient conn = null)
{
    PlayerInfo newPlayer = new PlayerInfo
    {
        ConnId = conn.connectionId,
        TeamIndex = teamIndex,
        PlayerIndex = playerIndex
    };

    // to make Mirror detect the change and sync
    var teamRef = TheTeamInfo[teamIndex];
    teamRef.Player.Add(newPlayer);
    TheTeamInfo[teamIndex] = teamRef;
}

when I called the function CmdAddPlayerToTeam to change the TheTeamInfo, the client did not sync that, is me miss something? some one could help me?

void gazelle
# burnt compass I have a struct like this ```c# [System.Serializable] public class PlayerInfo { ...

You need [Command(requiresAuthority = false)] if this script is on some object that client doesn't own. You'd have received a warning on server about that. You need to use structs, not classes in SyncList. Having said all that, doing team info like this isn't a good idea. Just set the team identifyer on each player object...that's all clients need to know. conn id is useless information for clients.