Skip to content

Commit bdbc71f

Browse files
eheimburgfernando-cortezdwoodruffsf
authored
(Cleanup) (feedback needed) Remove redundant list of ServerCharacters (#271)
* Remove redundant list of ServerCharacters * added missing Using * Revert and cleanup comments * Removing need for ServerCharacter list by iterating on player connections * remove unneeded usings * Refactoring to use a static list of player ServerCharacters * simplifying diff * Update Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs * moving to serialized field rather than GetComponent, and acting properly inert on client. Co-authored-by: Fernando Cortez <[email protected]> Co-authored-by: David Woodruff <[email protected]>
1 parent 6f87638 commit bdbc71f

File tree

7 files changed

+97
-35
lines changed

7 files changed

+97
-35
lines changed

Assets/BossRoom/Prefabs/Character/Player.prefab

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ MonoBehaviour:
1313
m_Name:
1414
m_EditorClassIdentifier:
1515
m_FeedbackPrefab: {fileID: 9137928905311479176, guid: 5d22c1d86e0e5604cbe14004bf924827, type: 3}
16+
--- !u!114 &-4877982356580531930
17+
MonoBehaviour:
18+
m_ObjectHideFlags: 0
19+
m_CorrespondingSourceObject: {fileID: 0}
20+
m_PrefabInstance: {fileID: 0}
21+
m_PrefabAsset: {fileID: 0}
22+
m_GameObject: {fileID: 6009713983291384756}
23+
m_Enabled: 1
24+
m_EditorHideFlags: 0
25+
m_Script: {fileID: 11500000, guid: a46d628dbb19f12449867d14fa5268b0, type: 3}
26+
m_Name:
27+
m_EditorClassIdentifier:
28+
m_CachedServerCharacter: {fileID: 6012286037226018870}
1629
--- !u!114 &2576537793715222015
1730
MonoBehaviour:
1831
m_ObjectHideFlags: 0
@@ -210,6 +223,17 @@ Transform:
210223
m_CorrespondingSourceObject: {fileID: 4600110157238723791, guid: 0d2d836e2e83b754fa1a1c4022d6d65d, type: 3}
211224
m_PrefabInstance: {fileID: 7831782662127126385}
212225
m_PrefabAsset: {fileID: 0}
226+
--- !u!114 &6012286037226018870 stripped
227+
MonoBehaviour:
228+
m_CorrespondingSourceObject: {fileID: 4602672899881656135, guid: 0d2d836e2e83b754fa1a1c4022d6d65d, type: 3}
229+
m_PrefabInstance: {fileID: 7831782662127126385}
230+
m_PrefabAsset: {fileID: 0}
231+
m_GameObject: {fileID: 6009713983291384756}
232+
m_Enabled: 1
233+
m_EditorHideFlags: 0
234+
m_Script: {fileID: 11500000, guid: 920a440eb254ba348915767fd046027a, type: 3}
235+
m_Name:
236+
m_EditorClassIdentifier:
213237
--- !u!114 &7751377510591478590 stripped
214238
MonoBehaviour:
215239
m_CorrespondingSourceObject: {fileID: 514105321093282895, guid: 0d2d836e2e83b754fa1a1c4022d6d65d, type: 3}

Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using MLAPI;
12
using UnityEngine;
23

34
namespace BossRoom.Server
@@ -33,7 +34,8 @@ protected void DetectFoes()
3334
float detectionRangeSqr = detectionRange * detectionRange;
3435
Vector3 position = m_Brain.GetMyServerCharacter().transform.position;
3536

36-
foreach (ServerCharacter character in ServerCharacter.GetAllActiveServerCharacters())
37+
// in this game, NPCs only attack players (and never other NPCs), so we can just iterate over the players to see if any are nearby
38+
foreach (var character in PlayerServerCharacter.GetPlayerServerCharacters())
3739
{
3840
if (m_Brain.IsAppropriateFoe(character) && (character.transform.position - position).sqrMagnitude <= detectionRangeSqr)
3941
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using MLAPI;
4+
using UnityEngine;
5+
6+
namespace BossRoom.Server
7+
{
8+
/// <summary>
9+
/// Attached to the player-characters' prefab, this maintains a list of active ServerCharacter objects for players.
10+
/// </summary>
11+
/// <remarks>
12+
/// This is an optimization. In server code you can already get a list of players' ServerCharacters by
13+
/// iterating over the active connections and calling GetComponent() on their PlayerObject. But we need
14+
/// to iterate over all players quite often -- the monsters' IdleAIState does so in every Update() --
15+
/// and all those GetComponent() calls add up! So this optimization lets us iterate without calling
16+
/// GetComponent(). This will be refactored with a ScriptableObject-based approach on player collection.
17+
/// </remarks>
18+
[RequireComponent(typeof(ServerCharacter))]
19+
public class PlayerServerCharacter : NetworkBehaviour
20+
{
21+
static List<ServerCharacter> s_ActivePlayers = new List<ServerCharacter>();
22+
23+
[SerializeField]
24+
ServerCharacter m_CachedServerCharacter;
25+
26+
public override void NetworkStart()
27+
{
28+
if( !IsServer )
29+
{
30+
enabled = false;
31+
}
32+
}
33+
34+
void OnEnable()
35+
{
36+
s_ActivePlayers.Add(m_CachedServerCharacter);
37+
}
38+
39+
void OnDisable()
40+
{
41+
s_ActivePlayers.Remove(m_CachedServerCharacter);
42+
}
43+
44+
/// <summary>
45+
/// Returns a list of all active players' ServerCharacters. Treat the list as read-only!
46+
/// The list will be empty on the client.
47+
/// </summary>
48+
public static List<ServerCharacter> GetPlayerServerCharacters()
49+
{
50+
51+
return s_ActivePlayers;
52+
}
53+
}
54+
}

Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ public bool IsNpc
4242
// Cached component reference
4343
private ServerCharacterMovement m_Movement;
4444

45-
/// <summary>
46-
/// Temp place to store all the active characters (to avoid having to
47-
/// perform insanely-expensive GameObject.Find operations during Update)
48-
/// </summary>
49-
private static List<ServerCharacter> s_ActiveServerCharacters = new List<ServerCharacter>();
50-
5145
private void Awake()
5246
{
5347
m_Movement = GetComponent<ServerCharacterMovement>();
@@ -60,21 +54,6 @@ private void Awake()
6054
}
6155
}
6256

63-
private void OnEnable()
64-
{
65-
s_ActiveServerCharacters.Add(this);
66-
}
67-
68-
private void OnDisable()
69-
{
70-
s_ActiveServerCharacters.Remove(this);
71-
}
72-
73-
public static List<ServerCharacter> GetAllActiveServerCharacters()
74-
{
75-
return s_ActiveServerCharacters;
76-
}
77-
7857
public override void NetworkStart()
7958
{
8059
if (!IsServer) { enabled = false; }

Assets/BossRoom/Scripts/Server/Game/Entity/ServerWaveSpawner.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,9 @@ bool IsAnyPlayerNearbyAndVisible()
248248

249249
// iterate through clients and only return true if a player is in range
250250
// and is not occluded by a blocking collider.
251-
foreach (KeyValuePair<ulong, NetworkClient> idToClient in NetworkManager.Singleton.ConnectedClients)
251+
foreach (var serverCharacter in PlayerServerCharacter.GetPlayerServerCharacters())
252252
{
253-
if (idToClient.Value.PlayerObject == null)
254-
{
255-
// skip over any connection that doesn't have a PlayerObject yet
256-
continue;
257-
}
258-
259-
var playerPosition = idToClient.Value.PlayerObject.transform.position;
253+
var playerPosition = serverCharacter.transform.position;
260254
var direction = playerPosition - spawnerPosition;
261255

262256
if (direction.sqrMagnitude > squaredProximityDistance)

Assets/BossRoom/Scripts/Server/Game/State/ServerBossRoomState.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,10 @@ private void OnHeroLifeStateChanged(LifeState prevLifeState, LifeState lifeState
174174
if (lifeState == LifeState.Fainted)
175175
{
176176
// Check the life state of all players in the scene
177-
foreach (var p in NetworkManager.Singleton.ConnectedClientsList )
177+
foreach (var serverCharacter in PlayerServerCharacter.GetPlayerServerCharacters())
178178
{
179-
if (p.PlayerObject == null) { continue; }
180-
// if any player is alive just retrun
181-
var netState = p.PlayerObject.GetComponent<NetworkCharacterState>();
182-
if ( netState.NetworkLifeState.Value == LifeState.Alive ) { return; }
179+
// if any player is alive just retun
180+
if (serverCharacter.NetState && serverCharacter.NetState.NetworkLifeState.Value == LifeState.Alive) { return; }
183181
}
184182

185183
// If we made it this far, all players are down! switch to post game

0 commit comments

Comments
 (0)