Skip to content

Commit 9b48228

Browse files
fix: removing FindObjectOfType for a serialized reference (#754)
1 parent c885305 commit 9b48228

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

Assets/Prefabs/State/CharSelectState.prefab

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ MonoBehaviour:
8686
autoRun: 1
8787
autoInjectGameObjects: []
8888
m_NetcodeHooks: {fileID: 8494646029522094751}
89+
m_NetworkCharSelection: {fileID: 3565665953789623673}
8990
m_AnimationTriggerOnCharSelect: BeginRevive
9091
m_AnimationTriggerOnCharChosen: BeginRevive
9192
m_PlayerSeats:

Assets/Scripts/Gameplay/GameState/ClientCharSelectState.cs

+23-24
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public class ClientCharSelectState : GameStateBehaviour
2626
NetcodeHooks m_NetcodeHooks;
2727

2828
public override GameState ActiveState { get { return GameState.CharSelect; } }
29-
public NetworkCharSelection networkCharSelection { get; private set; }
29+
30+
[SerializeField]
31+
NetworkCharSelection m_NetworkCharSelection;
3032

3133
[SerializeField]
3234
[Tooltip("This is triggered when the player chooses a character")]
@@ -116,9 +118,6 @@ protected override void Awake()
116118
base.Awake();
117119
Instance = this;
118120

119-
// TODO inject or find another way to find CharSelectData
120-
// TODO CharSelectData should directly be in ServerCharSelectState
121-
networkCharSelection = FindObjectOfType<NetworkCharSelection>();
122121
m_NetcodeHooks.OnNetworkSpawnHook += OnNetworkSpawn;
123122
m_NetcodeHooks.OnNetworkDespawnHook += OnNetworkDespawn;
124123

@@ -155,10 +154,10 @@ protected override void Start()
155154

156155
void OnNetworkDespawn()
157156
{
158-
if (networkCharSelection)
157+
if (m_NetworkCharSelection)
159158
{
160-
networkCharSelection.IsLobbyClosed.OnValueChanged -= OnLobbyClosedChanged;
161-
networkCharSelection.LobbyPlayers.OnListChanged -= OnLobbyPlayerStateChanged;
159+
m_NetworkCharSelection.IsLobbyClosed.OnValueChanged -= OnLobbyClosedChanged;
160+
m_NetworkCharSelection.LobbyPlayers.OnListChanged -= OnLobbyPlayerStateChanged;
162161
}
163162
}
164163

@@ -170,8 +169,8 @@ void OnNetworkSpawn()
170169
}
171170
else
172171
{
173-
networkCharSelection.IsLobbyClosed.OnValueChanged += OnLobbyClosedChanged;
174-
networkCharSelection.LobbyPlayers.OnListChanged += OnLobbyPlayerStateChanged;
172+
m_NetworkCharSelection.IsLobbyClosed.OnValueChanged += OnLobbyClosedChanged;
173+
m_NetworkCharSelection.LobbyPlayers.OnListChanged += OnLobbyPlayerStateChanged;
175174
}
176175
}
177176

@@ -186,7 +185,7 @@ void OnAssignedPlayerNumber(int playerNum)
186185

187186
void UpdatePlayerCount()
188187
{
189-
int count = networkCharSelection.LobbyPlayers.Count;
188+
int count = m_NetworkCharSelection.LobbyPlayers.Count;
190189
var pstr = (count > 1) ? "players" : "player";
191190
m_NumPlayersText.text = "<b>" + count + "</b> " + pstr + " connected";
192191
}
@@ -201,9 +200,9 @@ void OnLobbyPlayerStateChanged(NetworkListEvent<NetworkCharSelection.LobbyPlayer
201200

202201
// now let's find our local player in the list and update the character/info box appropriately
203202
int localPlayerIdx = -1;
204-
for (int i = 0; i < networkCharSelection.LobbyPlayers.Count; ++i)
203+
for (int i = 0; i < m_NetworkCharSelection.LobbyPlayers.Count; ++i)
205204
{
206-
if (networkCharSelection.LobbyPlayers[i].ClientId == NetworkManager.Singleton.LocalClientId)
205+
if (m_NetworkCharSelection.LobbyPlayers[i].ClientId == NetworkManager.Singleton.LocalClientId)
207206
{
208207
localPlayerIdx = i;
209208
break;
@@ -216,17 +215,17 @@ void OnLobbyPlayerStateChanged(NetworkListEvent<NetworkCharSelection.LobbyPlayer
216215
// this can happen for various reasons, such as the lobby being full and us not getting a seat.
217216
UpdateCharacterSelection(NetworkCharSelection.SeatState.Inactive);
218217
}
219-
else if (networkCharSelection.LobbyPlayers[localPlayerIdx].SeatState == NetworkCharSelection.SeatState.Inactive)
218+
else if (m_NetworkCharSelection.LobbyPlayers[localPlayerIdx].SeatState == NetworkCharSelection.SeatState.Inactive)
220219
{
221220
// we haven't chosen a seat yet (or were kicked out of our seat by someone else)
222221
UpdateCharacterSelection(NetworkCharSelection.SeatState.Inactive);
223222
// make sure our player num is properly set in Lobby UI
224-
OnAssignedPlayerNumber(networkCharSelection.LobbyPlayers[localPlayerIdx].PlayerNumber);
223+
OnAssignedPlayerNumber(m_NetworkCharSelection.LobbyPlayers[localPlayerIdx].PlayerNumber);
225224
}
226225
else
227226
{
228227
// we have a seat! Note that if our seat is LockedIn, this function will also switch the lobby mode
229-
UpdateCharacterSelection(networkCharSelection.LobbyPlayers[localPlayerIdx].SeatState, networkCharSelection.LobbyPlayers[localPlayerIdx].SeatIdx);
228+
UpdateCharacterSelection(m_NetworkCharSelection.LobbyPlayers[localPlayerIdx].SeatState, m_NetworkCharSelection.LobbyPlayers[localPlayerIdx].SeatIdx);
230229
}
231230
}
232231

@@ -258,7 +257,7 @@ void UpdateCharacterSelection(NetworkCharSelection.SeatState state, int seatIdx
258257
// change character preview when selecting a new seat
259258
if (isNewSeat)
260259
{
261-
var selectedCharacterGraphics = GetCharacterGraphics(networkCharSelection.AvatarConfiguration[seatIdx]);
260+
var selectedCharacterGraphics = GetCharacterGraphics(m_NetworkCharSelection.AvatarConfiguration[seatIdx]);
262261

263262
if (m_CurrentCharacterGraphics)
264263
{
@@ -269,15 +268,15 @@ void UpdateCharacterSelection(NetworkCharSelection.SeatState state, int seatIdx
269268
m_CurrentCharacterGraphics = selectedCharacterGraphics;
270269
m_CurrentCharacterGraphicsAnimator = m_CurrentCharacterGraphics.GetComponent<Animator>();
271270

272-
m_ClassInfoBox.ConfigureForClass(networkCharSelection.AvatarConfiguration[seatIdx].CharacterClass);
271+
m_ClassInfoBox.ConfigureForClass(m_NetworkCharSelection.AvatarConfiguration[seatIdx].CharacterClass);
273272
}
274273
}
275274
if (state == NetworkCharSelection.SeatState.LockedIn && !m_HasLocalPlayerLockedIn)
276275
{
277276
// the local player has locked in their seat choice! Rearrange the UI appropriately
278277
// the character should act excited
279278
m_CurrentCharacterGraphicsAnimator.SetTrigger(m_AnimationTriggerOnCharChosen);
280-
ConfigureUIForLobbyMode(networkCharSelection.IsLobbyClosed.Value ? LobbyMode.LobbyEnding : LobbyMode.SeatChosen);
279+
ConfigureUIForLobbyMode(m_NetworkCharSelection.IsLobbyClosed.Value ? LobbyMode.LobbyEnding : LobbyMode.SeatChosen);
281280
m_HasLocalPlayerLockedIn = true;
282281
}
283282
else if (m_HasLocalPlayerLockedIn && state == NetworkCharSelection.SeatState.Active)
@@ -307,7 +306,7 @@ void UpdateSeats()
307306
// But until a seat is locked in, we need to display each seat as being used by the latest player to choose it.
308307
// So we go through all players and figure out who should visually be shown as sitting in that seat.
309308
NetworkCharSelection.LobbyPlayerState[] curSeats = new NetworkCharSelection.LobbyPlayerState[m_PlayerSeats.Count];
310-
foreach (NetworkCharSelection.LobbyPlayerState playerState in networkCharSelection.LobbyPlayers)
309+
foreach (NetworkCharSelection.LobbyPlayerState playerState in m_NetworkCharSelection.LobbyPlayers)
311310
{
312311
if (playerState.SeatIdx == -1 || playerState.SeatState == NetworkCharSelection.SeatState.Inactive)
313312
continue; // this player isn't seated at all!
@@ -344,7 +343,7 @@ void OnLobbyClosedChanged(bool wasLobbyClosed, bool isLobbyClosed)
344343
else
345344
{
346345
ConfigureUIForLobbyMode(LobbyMode.SeatChosen);
347-
m_ClassInfoBox.ConfigureForClass(networkCharSelection.AvatarConfiguration[m_LastSeatSelected].CharacterClass);
346+
m_ClassInfoBox.ConfigureForClass(m_NetworkCharSelection.AvatarConfiguration[m_LastSeatSelected].CharacterClass);
348347
}
349348
}
350349
}
@@ -415,9 +414,9 @@ void ConfigureUIForLobbyMode(LobbyMode mode)
415414
/// <param name="seatIdx"></param>
416415
public void OnPlayerClickedSeat(int seatIdx)
417416
{
418-
if (networkCharSelection.IsSpawned)
417+
if (m_NetworkCharSelection.IsSpawned)
419418
{
420-
networkCharSelection.ChangeSeatServerRpc(NetworkManager.Singleton.LocalClientId, seatIdx, false);
419+
m_NetworkCharSelection.ChangeSeatServerRpc(NetworkManager.Singleton.LocalClientId, seatIdx, false);
421420
}
422421
}
423422

@@ -426,10 +425,10 @@ public void OnPlayerClickedSeat(int seatIdx)
426425
/// </summary>
427426
public void OnPlayerClickedReady()
428427
{
429-
if (networkCharSelection.IsSpawned)
428+
if (m_NetworkCharSelection.IsSpawned)
430429
{
431430
// request to lock in or unlock if already locked in
432-
networkCharSelection.ChangeSeatServerRpc(NetworkManager.Singleton.LocalClientId, m_LastSeatSelected, !m_HasLocalPlayerLockedIn);
431+
m_NetworkCharSelection.ChangeSeatServerRpc(NetworkManager.Singleton.LocalClientId, m_LastSeatSelected, !m_HasLocalPlayerLockedIn);
433432
}
434433
}
435434

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66

77
Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).
88

9+
## [Unreleased] - yyyy-mm-dd
10+
11+
### Added
12+
*
13+
14+
### Changed
15+
*
16+
17+
### Cleanup
18+
* Removed unnecessary FindObjectOfType usage inside of ClientCharSelectState (#754)
19+
20+
### Fixed
21+
*
22+
923
## [2.0.0] - 2022-10-06
1024

1125
### Added

0 commit comments

Comments
 (0)