- Home /
UNET SyncVar on a NetworkInstanceId SyncList structure not updating across network
Hi,
I've been looking for a solution on the web for the past hours, time to ask it myself... I have a SyncList structure with NetworkInstanceIds in it to share across all clients but it doesn't get updated. The list is up to date on the server for each player when I modify it, but it doesn't spread. I'm doing all the changes on the server and I don't know what to do to solve this.
Here's when I define the SyncVar :
[SyncVar]
public Structures.SyncListNetworkInstanceId secrets = new Structures.SyncListNetworkInstanceId();
Then I modify it here on the server only (I guess you don't need the full details of the project but I just need to pass the id of the secret objects) :
[Server]
public void DispatchSecrets()
{
// TODO : Something more proper and modulable to the number of players (have a problem with syncvar first to resolve)
int i = 0;
secrets[0].GetComponent<Secret>().shared = true;
secrets[1].GetComponent<Secret>().shared = true;
secrets[6].GetComponent<Secret>().shared = true;
List<GameObject> tmpSecrets = secrets;
tmpSecrets.Insert(5, secrets[0]);
tmpSecrets.Insert(11, secrets[1]);
tmpSecrets.Insert(10, secrets[6]);
foreach (Player p in team1.GetComponent<Team>().players)
{
Debug.Log(p.deviceName);
Debug.Log(tmpSecrets[i].GetComponent<Secret>().netId);
p.secrets.Add(tmpSecrets[i].GetComponent<Secret>().netId);
p.secrets.Add(tmpSecrets[i + 1].GetComponent<Secret>().netId);
p.secrets.Add(tmpSecrets[i + 2].GetComponent<Secret>().netId);
p.secrets.Add(tmpSecrets[i + 3].GetComponent<Secret>().netId);
p.secrets.Add(tmpSecrets[i + 4].GetComponent<Secret>().netId);
p.secrets.Shuffle();
p.RpcUpdateProfile();
i++;
}
i = 15;
secrets[15].GetComponent<Secret>().shared = true;
secrets[16].GetComponent<Secret>().shared = true;
secrets[21].GetComponent<Secret>().shared = true;
tmpSecrets.Insert(20, secrets[0]);
tmpSecrets.Insert(26, secrets[1]);
tmpSecrets.Insert(25, secrets[6]);
foreach (Player p in team2.GetComponent<Team>().players)
{
Debug.Log(p.deviceName);
Debug.Log(tmpSecrets[i].GetComponent<Secret>().netId);
p.secrets.Add(tmpSecrets[i].GetComponent<Secret>().netId);
p.secrets.Add(tmpSecrets[i + 1].GetComponent<Secret>().netId);
p.secrets.Add(tmpSecrets[i + 2].GetComponent<Secret>().netId);
p.secrets.Add(tmpSecrets[i + 3].GetComponent<Secret>().netId);
p.secrets.Add(tmpSecrets[i + 4].GetComponent<Secret>().netId);
p.secrets.Shuffle();
p.RpcUpdateProfile();
i++;
}
}
Can anyone help me ? That'd be great ! Thanks !
Answer by Darrek13 · Apr 06, 2017 at 10:04 PM
OK so I figured out what was the problem !
The definition of my structure was wrong to be used with UNET, that's why it wasn't synchronizing over the network. Below is the correct code, hoping it'd help anyone :
public static class Structures : object {
public class SyncListNetworkInstanceId : SyncListStruct<NetID> { }
public struct NetID
{
public NetworkInstanceId netID;
}
}
I changed a bit the core of the function below, but you just need to look at how I add new items to my structure.
[Server]
public void DispatchSecrets()
{
List<GameObject> tmpListSecrets = secrets.Take(secrets.Count / NUMBER_TEAMS).ToList();
foreach (GameObject team in teams)
{
Team t = team.GetComponent<Team>();
int numberOfSharedSecrets = t.players.Count == 1 ? 1 : t.players.Count - 1; // DEBUG : 1 to test with one player only (host test)...
for (int i = 0; i < t.players.Count; i++)
{
Player p = t.players[i];
for (int j = 0; j < numberOfSharedSecrets; j++)
{
Player nextPlayer = t.players[(i + 1) % t.players.Count];
Secret secret = tmpListSecrets[0].GetComponent<Secret>();
secrets.Find(s => s.GetComponent<Secret>().Equals(secret)).GetComponent<Secret>().shared = true;
p.secrets.Add(new Structures.NetID() { netID = secret.netId });
if (!nextPlayer.playerName.Equals(p.playerName))
{
nextPlayer.secrets.Add(new Structures.NetID() { netID = secret.netId });
}
tmpListSecrets.Remove(secret.gameObject);
}
int tmp = 5 - p.secrets.Count;
for (int j = 0; j < tmp; j++)
{
Secret secret = tmpListSecrets[0].GetComponent<Secret>();
p.secrets.Add(new Structures.NetID() { netID = secret.netId });
tmpListSecrets.Remove(secret.gameObject);
}
p.secrets.Shuffle();
p.RpcUpdateProfile();
}
}
}
Your answer
Follow this Question
Related Questions
Networking Sync SetActive Not Working 0 Answers
Help| Sync' boolean variable (Friendly Fire) 0 Answers
Networking lobby player image not working on host and other clients 0 Answers
How fast do Syncvars synchronize? 1 Answer
Unity hlapi networktransform rigidbody rotation and interpolation issue 1 Answer