Hi Guys,
I have a timer that loops and gathers info from all players. One of the methods called is GetLocation(player). This works until a new player joins. The player has no location when they join the server (presumable as they haven't spawned in yet).
How can I check that each player in the loop has fully spawned so that I don't call GetLocation() and cause an error?
Current code for your reference:
Code:void sendLocationLoop() { // IF no players online then no need to bother var playerSessions = GameManager.Instance.GetSessions().Values; timer.Repeat(10f, 0, () => { if (playerSessions.Count() > 0) { var playerLocData = new Dictionary<string, string>(); var players = new List<PlayerSession>(); foreach (var player in GameManager.Instance.GetSessions().Values) { players.Add(player); } foreach (var player in players) { var payload = new PlayerInfo { player_name = player.Name, player_location = GetLocation(player) }; playerLocData.Add(player.SteamId.ToString(), JsonConvert.SerializeObject(payload)); } PrintWarning(JsonConvert.SerializeObject(playerLocData)); playerLocData.Clear(); players.Clear(); } }); }
Solved Checking if player is fully loaded?
Discussion in 'Hurtworld Development' started by Sionn, Jul 25, 2016.
-
Wulf Community Admin
You can check if player.transform is null perhaps, else player.transform.position maybe.
-
Thanks. You narrowed it down for me.
It was actually WorldPlayerEntity that hadn't been set when calling GetLocation so checking for a null value in that resolved the issue.
Like so:
Code:foreach (var player in GameManager.Instance.GetSessions().Values) { // PrintWarning(player.WorldPlayerEntity.transform.position.ToString("G4")); if (player.WorldPlayerEntity != null) players.Add(player); }
-
Why not use session.isLoaded?
-
Cheers mate. Another method to add to my arsenal