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

Finding and counting players in groups.

Discussion in 'Scripting' started by KlivenPL, Nov 29, 2014.

  1. KlivenPL

    KlivenPL

    Joined:
    Nov 7, 2014
    Posts:
    4
    Hello everyone, I'm making a multiplayer FPS, and I need to count players, which are in group (eg. 1 or 2), and count them.
    The group information is stored in GO "Skrypty" in "MenadzerPolaczen" script. Players are tagged as "Players".

    I'm using Photon Network plugin.

    How to check which group a player has and set the number of "int obecnaLiczbaZywychGraczyT;" to the amount of them.
    I've tried (gracz = player, gracze = players, IDdruzyny = group ID, which can be 1 or 2)
    Code (CSharp):
    1. foreach (GameObject gracz in gracze){
    2.             if (gracz.transform.Find ("Skrypty").GetComponent<MenadzerPolaczen>().IDdruzyny == 1){
    3.                 obecnaLiczbaZywychGraczyCT = gracze.Count;
    4.  
    5.             }
    6.             if (gracz.transform.Find ("Skrypty").GetComponent<MenadzerPolaczen>().IDdruzyny == 2){
    7.                 obecnaLiczbaZywychGraczyT = gracze.Count
    8.  
    9.             }
    10.         }
    but it doesn't seem to work.
    Also, this code is in "Rozgrywka" script in GO "Skrypty".

    Any ideas?
     
  2. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    Doubt this will help you much, I'm sorry, but I wouldn't do it this way.

    As the players were sent to each group, I'd add them to a list. One list for each group. Then I just use list.Count for each list to get the number of players.

    This also makes it easier to get at each team of players whenever I want them.
     
  3. KlivenPL

    KlivenPL

    Joined:
    Nov 7, 2014
    Posts:
    4
    Yep, that's what I do now, but there's problem, when player gets killed, he sends RPC, to remove him from living guys in group, but that NOT ALWAYS works, idk why, I'm looking for another way :D
     
  4. KlivenPL

    KlivenPL

    Joined:
    Nov 7, 2014
    Posts:
    4
    OK I thought about it, and I'm doing quite diffrent thing, could you explain it more, or give an example how to do it in code?
     
  5. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    The way it sounds, I would keep multiple lists.

    I would have a list of people who are alive. I would have a list of everyone living and dead on a team. And if you have more than two teams, I would have a list of the lists of the teams... haha painful.

    You have some sort of class called Player (I am making assumptions)

    You also have another class that runs the game or at least runs which teams people are on.

    In this class, call it Manager, you do the following:

    You create lists for the teams and for the dead. The player class should also have a team id and a flag for whether they are alive or not as well as all the other stuff you already have.

    Code (CSharp):
    1. using System.Collections.Generic;
    2.  
    3. public List<Player> team1;
    4. public List<Player> team2;
    5. public List<Player> dead;
    6.  
    Create a function that adds a player to a team.

    Code (CSharp):
    1.  
    2. public void SetTeam(List<Player> team, Player player)
    3. {
    4.     // whatever other code you need to put in.. but also the following line
    5.     team.Add(player);
    6. }
    7.  
    Create another function that adds the person to the dead list

    Code (CSharp):
    1.  
    2. public void AddToDeadList(Player player)
    3. {
    4.    // might be other stuff here
    5.    dead.Add(player);
    6. }
    7.  
    These functions can be ones called through RPC or however you want to handle that.

    Does that make any sense?

    This system isn't perfect, probably has flaws.. but I hope it gives you a direction to explore.
     
    KlivenPL likes this.