- Home /
SyncList not initialized?
I can't find a lot of documentation about the SyncLists variables, so I'm kind of stuck trying to use lists with gameplay important values. When I try to add values to a SyncList on a client, I always get the error: "SyncList not initialized". If I want to SyncVar one single variable I would do it simply like this:
[SyncVar(hook="OnCooldown")]
private float someCooldown;
public override void OnStartLocalPlayer()
{
CmdServerSetCooldown();
}
[Command]
public void CmdServerSetCooldown()
{
RpcInitializeCooldown();
}
[ClientRpc]
RpcInitializeCooldown();
{
if(!isLocalPlayer)
return;
someCooldown = 5;
}
void OnCooldown(float newCooldown)
{
someCooldown = newCooldown;
}
But let's say I have a list of all the players' cooldowns, how would I achieve the same thing? Given for example: "When the player connects, add 5 values to the Cooldowns list (e.g. all zero's) from the server and tell the client about it". Thanks in advance!
Answer by Baxter900 · Sep 20, 2015 at 10:36 PM
So first of all, it's worth pointing out that your current example is really weird, and shouldn't even work. So I'm going to start off by telling you a little about some of the netcode before moving on. First of all, you can only change SyncVar's and SyncLists from the server, never from the client. This is probably where your problem is actually, here's an outline of how your code currently runs.
[SyncVar(hook="OnCooldown")] // When someCooldown changes, run OnCooldown
private float someCooldown;
Now it's worth noting real quick that the code your have in OnCooldown is functionally identical to just typing: [SyncVar] private float someCooldown; And just plain ignoring the hook.
public override void OnStartLocalPlayer() // When local player starts
{
CmdServerSetCooldown(); // Tell the server to set the cooldown
}
[Command]
public void CmdServerSetCooldown() // When the server is told to set the cooldown
{
RpcInitializeCooldown(); // Tell the clients to set the cooldown instead
}
[ClientRpc]
RpcInitializeCooldown(); //When the client is told to set the cooldown
{
if(!isLocalPlayer) // Check if you're the local player
return;
someCooldown = 5; //This won't actually work...
}
What you most likely want is this:
[SyncVar] // Make someCooldown sync
private float someCooldown;
public override void OnStartLocalPlayer() // When local player starts
{
CmdServerSetCooldown(); // Tell the server to set the cooldown
}
[Command]
public void CmdServerSetCooldown() // When the server is told to set the cooldown
{
someCooldown = 5; // set someCooldown to 5, since this is a syncvar, it will be sent to all clients
}
Now if you want this to use SyncLists, it's not that much more difficult.
[SyncVar] // Make someCooldown sync
private SyncListFloat someCooldowns = new SyncListFloat();
public override void OnStartLocalPlayer() // When local player starts
{
CmdServerSetCooldown(); // Tell the server to set the cooldown
}
[Command]
public void CmdServerSetCooldown() // When the server is told to set the cooldown
{
for(var i : int = 5; i>0; i--){ // Do this five times
someCooldowns.Add(0);
}
}
If you want the equivalent of a hook on SyncLists, just add this code: void Awake() { someCooldowns.Callback = OnSomeCooldownsUpdated } private void OnSomeCooldownsUpdated(SyncListFloat.Operation op, int index) { // do something here }
Anyway, hope that helps!
Your answer tremendously helped me erase some confusions with SyncLists.
I was wondering why SyncLists aren't being initialized and was about to suffer major setback, before co$$anonymous$$g to this question. I really loved how you provided examples explaining the premises. $$anonymous$$eep it up.
Also, you may want to edit the code at the bottom of your answer. It's missing some formatting.
Your answer
![](https://koobas.hobune.stream/wayback/20220612034338im_/https://answers.unity.com/themes/thub/images/avi.jpg)