From 7813920f52cdad2944f84d9177ab3512ec963138 Mon Sep 17 00:00:00 2001 From: eheimburg <74330250+eheimburg@users.noreply.github.com> Date: Mon, 12 Apr 2021 06:09:42 -0400 Subject: [PATCH 1/9] Remove redundant list of ServerCharacters --- .../Server/Game/AIState/IdleAIState.cs | 13 +++++++++--- .../Server/Game/Character/ServerCharacter.cs | 21 ------------------- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs index 95dc27637..eb8cbb41e 100644 --- a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs +++ b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs @@ -33,11 +33,18 @@ protected void DetectFoes() float detectionRangeSqr = detectionRange * detectionRange; Vector3 position = m_Brain.GetMyServerCharacter().transform.position; - foreach (ServerCharacter character in ServerCharacter.GetAllActiveServerCharacters()) + foreach (var spawnedObject in MLAPI.Spawning.NetworkSpawnManager.SpawnedObjectsList) { - if (m_Brain.IsAppropriateFoe(character) && (character.transform.position - position).sqrMagnitude <= detectionRangeSqr) + if (!spawnedObject) { continue; } // must have been Destroy()ed very recently + if ((spawnedObject.transform.position - position).sqrMagnitude <= detectionRangeSqr) { - m_Brain.Hate(character); + // they're within range... are they an appropriate foe? + ServerCharacter serverChar = spawnedObject.GetComponent(); + if (!serverChar) { continue; } // not even a character at all... + if (m_Brain.IsAppropriateFoe(serverChar)) + { + m_Brain.Hate(serverChar); + } } } } diff --git a/Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs b/Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs index 6986ee231..76d3c129b 100644 --- a/Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs +++ b/Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs @@ -42,12 +42,6 @@ public bool IsNpc // Cached component reference private ServerCharacterMovement m_Movement; - /// - /// Temp place to store all the active characters (to avoid having to - /// perform insanely-expensive GameObject.Find operations during Update) - /// - private static List s_ActiveServerCharacters = new List(); - private void Awake() { m_Movement = GetComponent(); @@ -60,21 +54,6 @@ private void Awake() } } - private void OnEnable() - { - s_ActiveServerCharacters.Add(this); - } - - private void OnDisable() - { - s_ActiveServerCharacters.Remove(this); - } - - public static List GetAllActiveServerCharacters() - { - return s_ActiveServerCharacters; - } - public override void NetworkStart() { if (!IsServer) { enabled = false; } From 5b7c1bf1920da607cf48872c36612744c2747e50 Mon Sep 17 00:00:00 2001 From: eheimburg <74330250+eheimburg@users.noreply.github.com> Date: Mon, 12 Apr 2021 06:39:30 -0400 Subject: [PATCH 2/9] added missing Using --- Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs index eb8cbb41e..3f5dde7ac 100644 --- a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs +++ b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs @@ -1,3 +1,4 @@ +using MLAPI.Spawning; using UnityEngine; namespace BossRoom.Server @@ -33,7 +34,7 @@ protected void DetectFoes() float detectionRangeSqr = detectionRange * detectionRange; Vector3 position = m_Brain.GetMyServerCharacter().transform.position; - foreach (var spawnedObject in MLAPI.Spawning.NetworkSpawnManager.SpawnedObjectsList) + foreach (var spawnedObject in NetworkSpawnManager.SpawnedObjectsList) { if (!spawnedObject) { continue; } // must have been Destroy()ed very recently if ((spawnedObject.transform.position - position).sqrMagnitude <= detectionRangeSqr) From 5fe91faa14e5fb58fb92e459ad6f45f674ceded3 Mon Sep 17 00:00:00 2001 From: eheimburg <74330250+eheimburg@users.noreply.github.com> Date: Tue, 13 Apr 2021 00:42:14 -0400 Subject: [PATCH 3/9] Revert and cleanup comments --- .../Server/Game/AIState/IdleAIState.cs | 14 ++------- .../Server/Game/Character/ServerCharacter.cs | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs index 3f5dde7ac..95dc27637 100644 --- a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs +++ b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs @@ -1,4 +1,3 @@ -using MLAPI.Spawning; using UnityEngine; namespace BossRoom.Server @@ -34,18 +33,11 @@ protected void DetectFoes() float detectionRangeSqr = detectionRange * detectionRange; Vector3 position = m_Brain.GetMyServerCharacter().transform.position; - foreach (var spawnedObject in NetworkSpawnManager.SpawnedObjectsList) + foreach (ServerCharacter character in ServerCharacter.GetAllActiveServerCharacters()) { - if (!spawnedObject) { continue; } // must have been Destroy()ed very recently - if ((spawnedObject.transform.position - position).sqrMagnitude <= detectionRangeSqr) + if (m_Brain.IsAppropriateFoe(character) && (character.transform.position - position).sqrMagnitude <= detectionRangeSqr) { - // they're within range... are they an appropriate foe? - ServerCharacter serverChar = spawnedObject.GetComponent(); - if (!serverChar) { continue; } // not even a character at all... - if (m_Brain.IsAppropriateFoe(serverChar)) - { - m_Brain.Hate(serverChar); - } + m_Brain.Hate(character); } } } diff --git a/Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs b/Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs index 76d3c129b..7c3e6c5c7 100644 --- a/Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs +++ b/Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs @@ -42,6 +42,11 @@ public bool IsNpc // Cached component reference private ServerCharacterMovement m_Movement; + /// + /// Stores a list of all the active characters. + /// + private static List s_ActiveServerCharacters = new List(); + private void Awake() { m_Movement = GetComponent(); @@ -54,6 +59,32 @@ private void Awake() } } + private void OnEnable() + { + s_ActiveServerCharacters.Add(this); + } + + private void OnDisable() + { + s_ActiveServerCharacters.Remove(this); + } + + /// + /// Retrieve a list of all active ServerCharacters. Treat the list as read-only! + /// + /// + /// MLAPI also has a built-in list for this: you can just iterate over + /// MLAPI.Spawning.NetworkSpawnManager.SpawnedObjectsList. However, that's a list of + /// NetworkObjects, so we'd still still need to call GetComponent on those to get a + /// ServerCharacter reference. This function is an optimization for when we just need + /// to iterate on ServerCharacters. + /// + /// list of all ServerCharacters which are active and enabled + public static List GetAllActiveServerCharacters() + { + return s_ActiveServerCharacters; + } + public override void NetworkStart() { if (!IsServer) { enabled = false; } From a0258730014f435b4f9137f96823f5da4ffe7eab Mon Sep 17 00:00:00 2001 From: eheimburg <74330250+eheimburg@users.noreply.github.com> Date: Wed, 14 Apr 2021 01:00:30 -0400 Subject: [PATCH 4/9] Removing need for ServerCharacter list by iterating on player connections --- .../Server/Game/AIState/IdleAIState.cs | 23 +++++++++++--- .../Server/Game/Character/ServerCharacter.cs | 31 ------------------- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs index 95dc27637..b61b09811 100644 --- a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs +++ b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs @@ -1,3 +1,6 @@ +using MLAPI; +using MLAPI.Connection; +using System.Collections.Generic; using UnityEngine; namespace BossRoom.Server @@ -31,13 +34,25 @@ protected void DetectFoes() float detectionRange = m_Brain.CharacterData.DetectRange; // we are doing this check every Update, so we'll use square-magnitude distance to avoid the expensive sqrt (that's implicit in Vector3.magnitude) float detectionRangeSqr = detectionRange * detectionRange; - Vector3 position = m_Brain.GetMyServerCharacter().transform.position; + Vector3 myPosition = m_Brain.GetMyServerCharacter().transform.position; - foreach (ServerCharacter character in ServerCharacter.GetAllActiveServerCharacters()) + // in this game, NPCs only attack players (and never other NPCs), so we can just iterate over the connected players to see if any are nearby + foreach (var networkClient in NetworkManager.Singleton.ConnectedClientsList) { - if (m_Brain.IsAppropriateFoe(character) && (character.transform.position - position).sqrMagnitude <= detectionRangeSqr) + if (networkClient.PlayerObject == null) { - m_Brain.Hate(character); + // skip over any connection that doesn't have a PlayerObject yet + continue; + } + + if ((networkClient.PlayerObject.transform.position - myPosition).sqrMagnitude <= detectionRangeSqr) + { + // they're in range! Make sure that they're actually an enemy + var serverCharacter = networkClient.PlayerObject.GetComponent(); + if (serverCharacter && m_Brain.IsAppropriateFoe(serverCharacter)) + { + m_Brain.Hate(serverCharacter); + } } } } diff --git a/Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs b/Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs index 7c3e6c5c7..76d3c129b 100644 --- a/Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs +++ b/Assets/BossRoom/Scripts/Server/Game/Character/ServerCharacter.cs @@ -42,11 +42,6 @@ public bool IsNpc // Cached component reference private ServerCharacterMovement m_Movement; - /// - /// Stores a list of all the active characters. - /// - private static List s_ActiveServerCharacters = new List(); - private void Awake() { m_Movement = GetComponent(); @@ -59,32 +54,6 @@ private void Awake() } } - private void OnEnable() - { - s_ActiveServerCharacters.Add(this); - } - - private void OnDisable() - { - s_ActiveServerCharacters.Remove(this); - } - - /// - /// Retrieve a list of all active ServerCharacters. Treat the list as read-only! - /// - /// - /// MLAPI also has a built-in list for this: you can just iterate over - /// MLAPI.Spawning.NetworkSpawnManager.SpawnedObjectsList. However, that's a list of - /// NetworkObjects, so we'd still still need to call GetComponent on those to get a - /// ServerCharacter reference. This function is an optimization for when we just need - /// to iterate on ServerCharacters. - /// - /// list of all ServerCharacters which are active and enabled - public static List GetAllActiveServerCharacters() - { - return s_ActiveServerCharacters; - } - public override void NetworkStart() { if (!IsServer) { enabled = false; } From 455b87b09f19153196dbccf6237f2146e5ddd655 Mon Sep 17 00:00:00 2001 From: eheimburg <74330250+eheimburg@users.noreply.github.com> Date: Wed, 14 Apr 2021 01:01:32 -0400 Subject: [PATCH 5/9] remove unneeded usings --- Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs index b61b09811..e4db3c4c5 100644 --- a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs +++ b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs @@ -1,6 +1,4 @@ using MLAPI; -using MLAPI.Connection; -using System.Collections.Generic; using UnityEngine; namespace BossRoom.Server From 1ab6c66fbef6e63d2d17d17fdab73c54fab1512f Mon Sep 17 00:00:00 2001 From: eheimburg <74330250+eheimburg@users.noreply.github.com> Date: Thu, 15 Apr 2021 06:03:55 -0400 Subject: [PATCH 6/9] Refactoring to use a static list of player ServerCharacters --- .../BossRoom/Prefabs/Character/Player.prefab | 12 +++++ .../Server/Game/AIState/IdleAIState.cs | 19 ++------ .../Game/Character/PlayerServerCharacter.cs | 44 +++++++++++++++++++ .../Character/PlayerServerCharacter.cs.meta | 11 +++++ .../Server/Game/Entity/ServerWaveSpawner.cs | 10 +---- .../Server/Game/State/ServerBossRoomState.cs | 8 ++-- 6 files changed, 76 insertions(+), 28 deletions(-) create mode 100644 Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs create mode 100644 Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs.meta diff --git a/Assets/BossRoom/Prefabs/Character/Player.prefab b/Assets/BossRoom/Prefabs/Character/Player.prefab index 652270524..ea810099b 100644 --- a/Assets/BossRoom/Prefabs/Character/Player.prefab +++ b/Assets/BossRoom/Prefabs/Character/Player.prefab @@ -61,6 +61,18 @@ MonoBehaviour: m_EditorClassIdentifier: Name: m_InternalValue: +--- !u!114 &-4877982356580531930 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6009713983291384756} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a46d628dbb19f12449867d14fa5268b0, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1001 &3097905377639811107 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs index e4db3c4c5..2232cdc90 100644 --- a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs +++ b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs @@ -34,23 +34,12 @@ protected void DetectFoes() float detectionRangeSqr = detectionRange * detectionRange; Vector3 myPosition = m_Brain.GetMyServerCharacter().transform.position; - // in this game, NPCs only attack players (and never other NPCs), so we can just iterate over the connected players to see if any are nearby - foreach (var networkClient in NetworkManager.Singleton.ConnectedClientsList) + // 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 + foreach (var serverCharacter in PlayerServerCharacter.GetPlayerServerCharacters()) { - if (networkClient.PlayerObject == null) + if (m_Brain.IsAppropriateFoe(serverCharacter) && (serverCharacter.transform.position - myPosition).sqrMagnitude <= detectionRangeSqr) { - // skip over any connection that doesn't have a PlayerObject yet - continue; - } - - if ((networkClient.PlayerObject.transform.position - myPosition).sqrMagnitude <= detectionRangeSqr) - { - // they're in range! Make sure that they're actually an enemy - var serverCharacter = networkClient.PlayerObject.GetComponent(); - if (serverCharacter && m_Brain.IsAppropriateFoe(serverCharacter)) - { - m_Brain.Hate(serverCharacter); - } + m_Brain.Hate(serverCharacter); } } } diff --git a/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs b/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs new file mode 100644 index 000000000..40606b9fb --- /dev/null +++ b/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs @@ -0,0 +1,44 @@ +using System.Collections; +using System.Collections.Generic; +using MLAPI; +using UnityEngine; + +namespace BossRoom.Server +{ + /// + /// Attached to the player-characters' prefab, this maintains a list of active ServerCharacter objects for players. + /// + /// + /// This is an optimization. In server code you can already get a list of players' ServerCharacters by + /// iterating over the active connections and calling GetComponent() on their PlayerObject. But we need + /// to iterate over all players quite often -- the monsters' IdleAIState does so in every Update() -- + /// and all those GetComponent() calls add up! So this optimization lets us iterate without calling + /// GetComponent(). + /// + [RequireComponent(typeof(ServerCharacter))] + public class PlayerServerCharacter : NetworkBehaviour + { + static List s_ActivePlayers = new List(); + + ServerCharacter m_CachedServerCharacter; + + void OnEnable() + { + m_CachedServerCharacter = GetComponent(); + s_ActivePlayers.Add(m_CachedServerCharacter); + } + + void OnDisable() + { + s_ActivePlayers.Remove(m_CachedServerCharacter); + } + + /// + /// Returns a list of all active players' ServerCharacters. Treat the list as read-only! + /// + public static List GetPlayerServerCharacters() + { + return s_ActivePlayers; + } + } +} diff --git a/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs.meta b/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs.meta new file mode 100644 index 000000000..a9c3da118 --- /dev/null +++ b/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a46d628dbb19f12449867d14fa5268b0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/BossRoom/Scripts/Server/Game/Entity/ServerWaveSpawner.cs b/Assets/BossRoom/Scripts/Server/Game/Entity/ServerWaveSpawner.cs index f4d56bd81..69d113c77 100644 --- a/Assets/BossRoom/Scripts/Server/Game/Entity/ServerWaveSpawner.cs +++ b/Assets/BossRoom/Scripts/Server/Game/Entity/ServerWaveSpawner.cs @@ -248,15 +248,9 @@ bool IsAnyPlayerNearbyAndVisible() // iterate through clients and only return true if a player is in range // and is not occluded by a blocking collider. - foreach (KeyValuePair idToClient in NetworkManager.Singleton.ConnectedClients) + foreach (var serverCharacter in PlayerServerCharacter.GetPlayerServerCharacters()) { - if (idToClient.Value.PlayerObject == null) - { - // skip over any connection that doesn't have a PlayerObject yet - continue; - } - - var playerPosition = idToClient.Value.PlayerObject.transform.position; + var playerPosition = serverCharacter.transform.position; var direction = playerPosition - spawnerPosition; if (direction.sqrMagnitude > squaredProximityDistance) diff --git a/Assets/BossRoom/Scripts/Server/Game/State/ServerBossRoomState.cs b/Assets/BossRoom/Scripts/Server/Game/State/ServerBossRoomState.cs index dc1df6054..b2668dc23 100644 --- a/Assets/BossRoom/Scripts/Server/Game/State/ServerBossRoomState.cs +++ b/Assets/BossRoom/Scripts/Server/Game/State/ServerBossRoomState.cs @@ -184,12 +184,10 @@ private void OnHeroLifeStateChanged(LifeState prevLifeState, LifeState lifeState if (lifeState == LifeState.Fainted) { // Check the life state of all players in the scene - foreach (var p in NetworkManager.Singleton.ConnectedClientsList ) + foreach (var serverCharacter in PlayerServerCharacter.GetPlayerServerCharacters()) { - if (p.PlayerObject == null) { continue; } - // if any player is alive just retrun - var netState = p.PlayerObject.GetComponent(); - if ( netState.NetworkLifeState.Value == LifeState.Alive ) { return; } + // if any player is alive just retun + if (serverCharacter.NetState && serverCharacter.NetState.NetworkLifeState.Value == LifeState.Alive) { return; } } // If we made it this far, all players are down! switch to post game From aaec37b529f9b211d87afb55512005242dea4fb5 Mon Sep 17 00:00:00 2001 From: eheimburg <74330250+eheimburg@users.noreply.github.com> Date: Thu, 15 Apr 2021 06:09:14 -0400 Subject: [PATCH 7/9] simplifying diff --- .../BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs index 2232cdc90..e5846b1de 100644 --- a/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs +++ b/Assets/BossRoom/Scripts/Server/Game/AIState/IdleAIState.cs @@ -32,14 +32,14 @@ protected void DetectFoes() float detectionRange = m_Brain.CharacterData.DetectRange; // we are doing this check every Update, so we'll use square-magnitude distance to avoid the expensive sqrt (that's implicit in Vector3.magnitude) float detectionRangeSqr = detectionRange * detectionRange; - Vector3 myPosition = m_Brain.GetMyServerCharacter().transform.position; + Vector3 position = m_Brain.GetMyServerCharacter().transform.position; // 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 - foreach (var serverCharacter in PlayerServerCharacter.GetPlayerServerCharacters()) + foreach (var character in PlayerServerCharacter.GetPlayerServerCharacters()) { - if (m_Brain.IsAppropriateFoe(serverCharacter) && (serverCharacter.transform.position - myPosition).sqrMagnitude <= detectionRangeSqr) + if (m_Brain.IsAppropriateFoe(character) && (character.transform.position - position).sqrMagnitude <= detectionRangeSqr) { - m_Brain.Hate(serverCharacter); + m_Brain.Hate(character); } } } From 7f5e8ff9716efba19ba7343a342f05eca594b005 Mon Sep 17 00:00:00 2001 From: Fernando Cortez <75813458+fernando-cortez@users.noreply.github.com> Date: Fri, 16 Apr 2021 16:02:35 -0400 Subject: [PATCH 8/9] Update Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs --- .../Scripts/Server/Game/Character/PlayerServerCharacter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs b/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs index 40606b9fb..11f56124d 100644 --- a/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs +++ b/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs @@ -13,7 +13,7 @@ namespace BossRoom.Server /// iterating over the active connections and calling GetComponent() on their PlayerObject. But we need /// to iterate over all players quite often -- the monsters' IdleAIState does so in every Update() -- /// and all those GetComponent() calls add up! So this optimization lets us iterate without calling - /// GetComponent(). + /// GetComponent(). This will be refactored with a ScriptableObject-based approach on player collection. /// [RequireComponent(typeof(ServerCharacter))] public class PlayerServerCharacter : NetworkBehaviour From 341a6de688d9b174a00c19d97e67fad26a45b591 Mon Sep 17 00:00:00 2001 From: David Woodruff Date: Fri, 16 Apr 2021 16:24:52 -0400 Subject: [PATCH 9/9] moving to serialized field rather than GetComponent, and acting properly inert on client. --- .../BossRoom/Prefabs/Character/Player.prefab | 36 ++++++++++++------- .../Game/Character/PlayerServerCharacter.cs | 12 ++++++- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/Assets/BossRoom/Prefabs/Character/Player.prefab b/Assets/BossRoom/Prefabs/Character/Player.prefab index ea810099b..51d735021 100644 --- a/Assets/BossRoom/Prefabs/Character/Player.prefab +++ b/Assets/BossRoom/Prefabs/Character/Player.prefab @@ -13,6 +13,19 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_FeedbackPrefab: {fileID: 9137928905311479176, guid: 5d22c1d86e0e5604cbe14004bf924827, type: 3} +--- !u!114 &-4877982356580531930 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6009713983291384756} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a46d628dbb19f12449867d14fa5268b0, type: 3} + m_Name: + m_EditorClassIdentifier: + m_CachedServerCharacter: {fileID: 6012286037226018870} --- !u!114 &2576537793715222015 MonoBehaviour: m_ObjectHideFlags: 0 @@ -61,18 +74,6 @@ MonoBehaviour: m_EditorClassIdentifier: Name: m_InternalValue: ---- !u!114 &-4877982356580531930 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 6009713983291384756} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: a46d628dbb19f12449867d14fa5268b0, type: 3} - m_Name: - m_EditorClassIdentifier: --- !u!1001 &3097905377639811107 PrefabInstance: m_ObjectHideFlags: 0 @@ -222,6 +223,17 @@ Transform: m_CorrespondingSourceObject: {fileID: 4600110157238723791, guid: 0d2d836e2e83b754fa1a1c4022d6d65d, type: 3} m_PrefabInstance: {fileID: 7831782662127126385} m_PrefabAsset: {fileID: 0} +--- !u!114 &6012286037226018870 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 4602672899881656135, guid: 0d2d836e2e83b754fa1a1c4022d6d65d, type: 3} + m_PrefabInstance: {fileID: 7831782662127126385} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6009713983291384756} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 920a440eb254ba348915767fd046027a, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!114 &7751377510591478590 stripped MonoBehaviour: m_CorrespondingSourceObject: {fileID: 514105321093282895, guid: 0d2d836e2e83b754fa1a1c4022d6d65d, type: 3} diff --git a/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs b/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs index 40606b9fb..f89e98db7 100644 --- a/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs +++ b/Assets/BossRoom/Scripts/Server/Game/Character/PlayerServerCharacter.cs @@ -20,11 +20,19 @@ public class PlayerServerCharacter : NetworkBehaviour { static List s_ActivePlayers = new List(); + [SerializeField] ServerCharacter m_CachedServerCharacter; + public override void NetworkStart() + { + if( !IsServer ) + { + enabled = false; + } + } + void OnEnable() { - m_CachedServerCharacter = GetComponent(); s_ActivePlayers.Add(m_CachedServerCharacter); } @@ -35,9 +43,11 @@ void OnDisable() /// /// Returns a list of all active players' ServerCharacters. Treat the list as read-only! + /// The list will be empty on the client. /// public static List GetPlayerServerCharacters() { + return s_ActivePlayers; } }