diff --git a/docs/basics/networkvariable.md b/docs/basics/networkvariable.md index 62532f40e..e377dc474 100644 --- a/docs/basics/networkvariable.md +++ b/docs/basics/networkvariable.md @@ -332,9 +332,57 @@ public class PlayerState : NetworkBehaviour // The weapon booster currently applied to a player private NetworkVariable PlayerWeaponBooster = new NetworkVariable(); - // A list of team members active "area weapon boosters" that could be applied if the local player - // is within their range. - private NetworkList TeamAreaWeaponBoosters = new NetworkList(); + /// + /// A list of team members active "area weapon boosters" that could be applied if the local player + /// is within their range. + /// + private NetworkList TeamAreaWeaponBoosters; + + void Awake() + { + //NetworkList can't be initialized at declaration time like NetworkVariable. It must be initialized in Awake instead. + TeamAreaWeaponBoosters = new NetworkList(); + } + + void Start() + { + /*At this point, the object has not been network spawned yet, so you're not allowed to edit network variables! */ + //list.Add(new AreaWeaponBooster()); + } + + void Update() + { + //This is just an example that shows how to add an element to the list after its initialization: + if (!IsServer) { return; } //remember: only the server can edit the list + if (Input.GetKeyUp(KeyCode.UpArrow)) + { + TeamAreaWeaponBoosters.Add(new AreaWeaponBooster())); + } + } + + public override void OnNetworkSpawn() + { + base.OnNetworkSpawn(); + if (IsClient) + { + TeamAreaWeaponBoosters.OnListChanged += OnClientListChanged; + } + if (IsServer) + { + TeamAreaWeaponBoosters.OnListChanged += OnServerListChanged; + TeamAreaWeaponBoosters.Add(new AreaWeaponBooster()); //if you want to initialize the list with some default values, this is a good time to do so. + } + } + + void OnServerListChanged(NetworkListEvent changeEvent) + { + Debug.Log($"[S] The list changed and now contains {TeamAreaWeaponBoosters.Count} elements"); + } + + void OnClientListChanged(NetworkListEvent changeEvent) + { + Debug.Log($"[C] The list changed and now contains {TeamAreaWeaponBoosters.Count} elements"); + } } ///