Tried to fix the bug where other clients receive the scoreboard

dev-hazim
Ali Sharoz 2 weeks ago
parent 4cc7e60091
commit 1f41843f4d

@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_IndirectSpecularColor: {r: 0.12731749, g: 0.13414757, b: 0.1210787, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:

@ -215,10 +215,35 @@ namespace Unity.BossRoom.Gameplay.GameState
// spawn players characters with destroyWithScene = true
newPlayer.SpawnWithOwnership(clientId, true);
//ScoreboardCaller(newPlayer);
ScoreboardCallerServerRpc(newPlayer);
//var playerScoreComponent = newPlayer.GetComponent<PlayerScoreComponent>();
}
void ScoreboardCaller(NetworkObject newplayer)
{
scoreboard.ScoreBoardListFiller(newplayer);
}
[ServerRpc(RequireOwnership = false)]
void ScoreboardCallerServerRpc(NetworkObject newPlayer)
{
// Call the method on the server first
scoreboard.ScoreBoardListFiller(newPlayer);
// Then invoke it on the client
ScoreboardCallerClientRpc(newPlayer);
}
[ClientRpc]
void ScoreboardCallerClientRpc(NetworkObject newPlayer)
{
// Call the method on the client
if (NetworkManager.Singleton.IsClient)
{
scoreboard.ScoreBoardListFiller(newPlayer);
}
}
void OnLifeStateChangedEventMessage(LifeStateChangedEventMessage message)
{

@ -22,7 +22,7 @@ namespace Unity.BossRoom.Gameplay.GameState
public override GameState ActiveState => GameState.CharSelect;
public NetworkCharSelection networkCharSelection { get; private set; }
//public static int numOfPlayers;
Coroutine m_WaitToEndLobbyCoroutine;
[Inject]
@ -165,6 +165,7 @@ namespace Unity.BossRoom.Gameplay.GameState
void SaveLobbyResults()
{
int numofPlayersInLobby = networkCharSelection.LobbyPlayers.Count;
//numOfPlayers = numofPlayersInLobby;
PlayerPrefs.SetInt("NumberOfLobbyPlayers", numofPlayersInLobby);
Debug.Log("Number of Players in lobby are: " + numofPlayersInLobby);
foreach (NetworkCharSelection.LobbyPlayerState playerInfo in networkCharSelection.LobbyPlayers)

@ -7,6 +7,7 @@ public class PlayerScoreComponent : NetworkBehaviour
public int CurrentScore { get; private set; }
public ServerCharacter serverCharacter;
public int m_index;
public PlayerItem playerItem;
public override void OnNetworkSpawn()
{
if (IsOwner && IsClient)
@ -34,4 +35,9 @@ public class PlayerScoreComponent : NetworkBehaviour
// Update the player's score display in the UI.
Debug.Log($"[PlayerScoreComponent] Updated score UI for Player {OwnerClientId}: {CurrentScore}");
}
private void OnDestroy()
{
Destroy(playerItem.gameObject);
Scoreboard.instance.DestroyChecker();
}
}

@ -5,6 +5,7 @@ using UnityEngine;
using Unity.BossRoom.Gameplay.GameState;
using Unity.Netcode;
using VContainer.Unity;
public class Scoreboard : MonoBehaviour
{
public List<PlayerScoreComponent> m_PlayerScoreComponents;
@ -13,56 +14,208 @@ public class Scoreboard : MonoBehaviour
public Transform Parent;
int index = 0;
bool coroutineStarter = false;
public static Scoreboard instance;
private void Awake()
{
instance = this;
}
public void DestroyChecker()
{
for (int i = 0; i < m_PlayerScoreComponents.Count; i++)
{
if (m_PlayerScoreComponents[i] == null)
{
m_PlayerScoreComponents.RemoveAt(i);
}
}
for (int i = 0; i < playerItems.Count; i++)
{
if (playerItems[i] == null)
{
playerItems.RemoveAt(i);
}
}
}
private IEnumerator Start()
{
Debug.Log("Scoreboard start1");
Debug.Log("Scoreboard Start - Coroutine Started");
// Wait until coroutineStarter is set to true
yield return new WaitUntil(() => coroutineStarter == true);
Debug.Log("Scoreboard start2");
Debug.Log("Scoreboard Start - Coroutine Finished");
yield return new WaitForSeconds(2);
Debug.Log("Scoreboard start3");
Debug.Log("Scoreboard Start - Starter Called");
Starter();
Debug.Log("Scoreboard start4");
}
public void ScoreBoardListFiller(NetworkObject playerObj)
{
if (!NetworkManager.Singleton.IsServer)
{
Debug.LogError("ScoreBoardListFiller called on client, should be on server");
return; // Only execute this on the server
}
Debug.Log("ScoreBoardListFiller called on server");
m_PlayerScoreComponents.Add(playerObj.GetComponent<PlayerScoreComponent>());
m_PlayerScoreComponents[m_PlayerScoreComponents.Count - 1].m_index = index;
Instantiator(index);
index++;
Instantiator(m_PlayerScoreComponents.Count);
if (!coroutineStarter)
{
coroutineStarter = true;
}
}
public void Starter()
{
// Start the coroutine for updating the scores
StartCoroutine(ScoreUpdater());
}
public void Instantiator(int count)
public void Instantiator(int index)
{
for (int i = 0; i < count; i++)
// Only instantiate UI elements on the client side
if (!NetworkManager.Singleton.IsClient)
{
Debug.LogError("Instantiator called on server, should be on client");
return;
}
Debug.Log("Instantiator called on client");
GameObject temp = Instantiate(playerItemPrefab, Parent);
PlayerItem item = temp.GetComponent<PlayerItem>();
m_PlayerScoreComponents[m_PlayerScoreComponents.Count - 1].playerItem = item;
playerItems.Add(item);
playerItems[playerItems.Count-1].PlayerScore.text = m_PlayerScoreComponents[i].CurrentScore.ToString();
playerItems[i].PlayerName.text = m_PlayerScoreComponents[i].serverCharacter.uIStateDisplayHandler.m_UIState.playerName.ToString();
}
playerItems[playerItems.Count - 1].PlayerScore.text = m_PlayerScoreComponents[index].CurrentScore.ToString();
playerItems[playerItems.Count - 1].PlayerName.text = m_PlayerScoreComponents[index].serverCharacter.uIStateDisplayHandler.m_UIState.playerName.ToString();
}
IEnumerator ScoreUpdater()
{
yield return new WaitForSeconds(0.5f);
Debug.Log("Scoreboard start5");
Debug.Log("Scoreboard Start - ScoreUpdater started");
while (true)
{
yield return new WaitForSeconds(0.1f);
m_PlayerScoreComponents.Sort((p1, p2) => p2.CurrentScore.CompareTo(p1.CurrentScore));
// Update the UI on the client side only
if (NetworkManager.Singleton.IsClient)
{
Debug.Log("Scoreboard Update - UI update on client");
for (int i = 0; i < playerItems.Count; i++)
{
playerItems[i].transform.SetSiblingIndex(m_PlayerScoreComponents[i].m_index);
}
}
else
{
Debug.LogWarning("Scoreboard Update - Trying to update UI on the server side");
}
}
}
}
//using System.Collections;
//using System.Collections.Generic;
//using Unity.Multiplayer.Samples.BossRoom;
//using UnityEngine;
//using Unity.BossRoom.Gameplay.GameState;
//using Unity.Netcode;
//using VContainer.Unity;
//public class Scoreboard : MonoBehaviour
//{
// public List<PlayerScoreComponent> m_PlayerScoreComponents;
// public List<PlayerItem> playerItems;
// public GameObject playerItemPrefab;
// public Transform Parent;
// int index = 0;
// bool coroutineStarter = false;
// public static Scoreboard instance;
// private void Awake()
// {
// instance = this;
// }
// public void DestroyChecker()
// {
// for(int i=0;i< m_PlayerScoreComponents.Count;i++)
// {
// if(m_PlayerScoreComponents[i]==null)
// {
// m_PlayerScoreComponents.RemoveAt(i);
// }
// }
// for(int i=0;i< playerItems.Count;i++)
// {
// if(playerItems[i]==null)
// {
// playerItems.RemoveAt(i);
// }
// }
// }
// private IEnumerator Start()
// {
// Debug.Log("Scoreboard start1");
// yield return new WaitUntil(() => coroutineStarter == true);
// Debug.Log("Scoreboard start2");
// yield return new WaitForSeconds(2);
// Debug.Log("Scoreboard start3");
// Starter();
// Debug.Log("Scoreboard start4");
// }
// public void ScoreBoardListFiller(NetworkObject playerObj)
// {
// m_PlayerScoreComponents.Add(playerObj.GetComponent<PlayerScoreComponent>());
// m_PlayerScoreComponents[m_PlayerScoreComponents.Count - 1].m_index = index;
// Instantiator(index);
// index++;
// if (!coroutineStarter)
// {
// coroutineStarter = true;
// }
// }
// public void Starter()
// {
// StartCoroutine(ScoreUpdater());
// }
// public void Instantiator(int index)
// {
// GameObject temp = Instantiate(playerItemPrefab, Parent);
// PlayerItem item = temp.GetComponent<PlayerItem>();
// m_PlayerScoreComponents[m_PlayerScoreComponents.Count - 1].playerItem = item;
// playerItems.Add(item);
// playerItems[playerItems.Count - 1].PlayerScore.text = m_PlayerScoreComponents[index].CurrentScore.ToString();
// playerItems[playerItems.Count - 1].PlayerName.text = m_PlayerScoreComponents[index].serverCharacter.uIStateDisplayHandler.m_UIState.playerName.ToString();
// }
// IEnumerator ScoreUpdater()
// {
// yield return new WaitForSeconds(0.5f);
// Debug.Log("Scoreboard start5");
// while (true)
// {
// yield return new WaitForSeconds(0.1f);
// m_PlayerScoreComponents.Sort((p1, p2) => p2.CurrentScore.CompareTo(p1.CurrentScore));
// for (int i = 0; i < playerItems.Count; i++)
// {
// playerItems[i].transform.SetSiblingIndex(m_PlayerScoreComponents[i].m_index);
// }
// }
// }
//}

Loading…
Cancel
Save