Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2 Teams Score NOT Working !

Discussion in 'Multiplayer' started by Loaiq1107, Feb 12, 2016.

  1. Loaiq1107

    Loaiq1107

    Joined:
    Aug 25, 2014
    Posts:
    48
    I'm making a multiplayer game and want to make a Score system for both teams , so if you kill an enemy your team's score adds one point and so on...

    all the methods i tried it either added one point and Only showed at the dead player's score (didn't show at any another player's score) , or it added on the dead player's score and kept adding.

    Can someone please help and show me the code needed or the idea?
    Thanks a lot !
     
  2. Chom1czek

    Chom1czek

    Joined:
    Sep 19, 2015
    Posts:
    66
    Hello there, I think you should use SyncStructList, create your own struct and add callback on server side to see if it's working. SyncStruct should be placed on already existing object on the scene. Whenever someone is killed then you should use [Cmd] on the client side to let the server information to update the struct (go through the struct list with for loop, save iteration, use "removeAt" and then "insert" to fill the gap).
    I hope you can understand the point, if now I will try to give you an example code later today.

    Good luck
     
  3. Loaiq1107

    Loaiq1107

    Joined:
    Aug 25, 2014
    Posts:
    48
    Hey man :) thanks a lot but didnt really understand actually :( if you cangive me an example code i will reallyy really appreciate it !! thanks
     
  4. Chom1czek

    Chom1czek

    Joined:
    Sep 19, 2015
    Posts:
    66
    Sorry for the delay, anyway!

    About that SyncStructList, I use it but rather for bigger datas but when it comes to such small things like team points you can pretty much have it on your player prefab so you don't need to create that TeamScoreManager in seperate prefab. If you add it to your prefab then you need to add this:
    Code (CSharp):
    1. public class TeamScoreManager : NetworkBehaviour
    2. {
    3.    [SyncVar] public int teamScore1 = 0;
    4.    [SyncVar] public int teamScore2 = 0;
    5.  
    6. [Command]
    7. public void CmdAddPoints(int teamIndex, int pointsValue)
    8. {
    9.    if(teamIndex == 1)
    10.    {
    11.       teamScore1 += pointsValue;
    12.    }
    13.    else teamScore2 += pointsValue;
    14. }
    if you got more teams you can use "switch", I think that's it. TeamScore integers and SyncVariables so they will get updated on every player when you update it on the server side (Cmd thing)

    Edit:
    SyncStructList continued:
    If you want to check SyncStructList then create new prefab, attache NetworkIdentity to it and register it in your NetworkManager, create new Network component which should look something like that:

    Code (CSharp):
    1. public class ScoreManager : NetworkBehaviour
    2. {
    3.    public struct Scores
    4.    {
    5.       public int teamIndex;
    6.       public int teamGold;
    7.       public int teamKills;
    8.       public int teamDeaths;
    9.    }
    10.  
    11. public override string ToString()
    12. {
    13.    return string.Format("{0} : {1} : {2} : {3}", teamIndex, teamGold, teamKills, teamDeaths);
    14. }
    15.  
    16. public class ScoreStructSync : SyncListStruct<Scores>{}
    17. public ScoreStructSync scoreList = new ScoreStructSync();
    18.  
    19. private void ScoreStatsChanged(SyncList<Scores>.Operation op, int itemIndex)
    20. {
    21.    Debug.Log("List changed: " + op + " " + itemIndex);
    22. }
    23.  
    24. public override void OnStartClient()
    25. {
    26.    scoreList.Callback = ScoreStatsChanged;
    27. }
    28.  
    29. public void EditScores(int teamIndex, int teamGold, int teamKills, int teamDeaths)
    30. {
    31.    Scores updatedStats = scoreList[teamIndex];
    32.    updatedStats.teamGold = teamGold;
    33.    updatedStats.teamKills = teamKills;
    34.    updatedStats.teamDeaths = teamDeaths;
    35.    scoreList.RemoveAt(teamIndex);
    36.    scoreList.Insert(teamIndex, updatedStats);
    37. }
    38.  
    39. void OnGUI()
    40. {
    41.         GUILayout.BeginArea(new Rect(Screen.width - 300, 10, 250, 500));
    42.         GUILayout.BeginVertical("box");
    43.         GUILayout.Label("Scores:");
    44.         GUILayout.Space(10);
    45.  
    46.         if(scoreList.Count > 0)
    47.         {
    48.             foreach(var team in scoreList)
    49.             {
    50.                string result = string.Format("{0} - TeamIndex, {1} Gold, {2} Kills, {3} Deaths",
    51. scoreList.teamIndex, scoreList.teamGold, scoreList.teamKills, scoreList.teamDeaths);
    52.                GUILayout.Label(result);
    53.                GUILayout.Space(5);
    54.             }
    55.         }
    56.  
    57.         GUILayout.EndVertical();
    58.         GUILayout.EndArea();
    59. }
    60.  
    61. }
    62.  
    63. }
    PS well you can also use it on your playerPrefab but I rather get it seperated cause I've already got pretty much components on my prefabs and this scores are "common" so no need to keep them on every playerPrefab I think?

    Well I hope it will help someone :D If not then it was waste of time :D
     
    Last edited: Feb 25, 2016
  5. Loaiq1107

    Loaiq1107

    Joined:
    Aug 25, 2014
    Posts:
    48
    Thanks a lot really ! but i already discovered and changed the score system entirely !!
    But remember man , it'll be NEVER a waste of time because im gonna use it in the future for sure , and you just taught me a new thing :D , and ofcourse it'll help someone so thank you very much !
     
    Chom1czek likes this.