Skip to content

Commit 1e33158

Browse files
RikuTheFuffsLarah Armstrong
and
Larah Armstrong
authored
Fixed misleading example about NetworkList (#854)
Fixed misleading example about NetworkList, and added a more extensive example to display how to react on NetworkList changes specifically on the server/client Co-authored-by: Larah Armstrong <[email protected]>
1 parent f2bf2bf commit 1e33158

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

docs/basics/networkvariable.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,57 @@ public class PlayerState : NetworkBehaviour
332332
// The weapon booster currently applied to a player
333333
private NetworkVariable<WeaponBooster> PlayerWeaponBooster = new NetworkVariable<WeaponBooster>();
334334

335-
// A list of team members active "area weapon boosters" that could be applied if the local player
336-
// is within their range.
337-
private NetworkList<AreaWeaponBooster> TeamAreaWeaponBoosters = new NetworkList<AreaWeaponBooster>();
335+
/// <summary>
336+
/// A list of team members active "area weapon boosters" that could be applied if the local player
337+
/// is within their range.
338+
/// </summary>
339+
private NetworkList<AreaWeaponBooster> TeamAreaWeaponBoosters;
340+
341+
void Awake()
342+
{
343+
//NetworkList can't be initialized at declaration time like NetworkVariable. It must be initialized in Awake instead.
344+
TeamAreaWeaponBoosters = new NetworkList<AreaWeaponBooster>();
345+
}
346+
347+
void Start()
348+
{
349+
/*At this point, the object has not been network spawned yet, so you're not allowed to edit network variables! */
350+
//list.Add(new AreaWeaponBooster());
351+
}
352+
353+
void Update()
354+
{
355+
//This is just an example that shows how to add an element to the list after its initialization:
356+
if (!IsServer) { return; } //remember: only the server can edit the list
357+
if (Input.GetKeyUp(KeyCode.UpArrow))
358+
{
359+
TeamAreaWeaponBoosters.Add(new AreaWeaponBooster()));
360+
}
361+
}
362+
363+
public override void OnNetworkSpawn()
364+
{
365+
base.OnNetworkSpawn();
366+
if (IsClient)
367+
{
368+
TeamAreaWeaponBoosters.OnListChanged += OnClientListChanged;
369+
}
370+
if (IsServer)
371+
{
372+
TeamAreaWeaponBoosters.OnListChanged += OnServerListChanged;
373+
TeamAreaWeaponBoosters.Add(new AreaWeaponBooster()); //if you want to initialize the list with some default values, this is a good time to do so.
374+
}
375+
}
376+
377+
void OnServerListChanged(NetworkListEvent<AreaWeaponBooster> changeEvent)
378+
{
379+
Debug.Log($"[S] The list changed and now contains {TeamAreaWeaponBoosters.Count} elements");
380+
}
381+
382+
void OnClientListChanged(NetworkListEvent<AreaWeaponBooster> changeEvent)
383+
{
384+
Debug.Log($"[C] The list changed and now contains {TeamAreaWeaponBoosters.Count} elements");
385+
}
338386
}
339387

340388
/// <summary>

0 commit comments

Comments
 (0)