PlayerData and PData
public class PData
{
public int playerId = 0;
public int skin = 0;
public string name = "???";
public int kills = 0, deaths = 0; //assists?
public PData(int playerId)
{
this.playerId = playerId;
}
}
public class PlayerData
{
public static int PlayerID = NetworkEntity.UNASSIGNED_ENTITY_ID;
public static PData myData = new PData(0);
//keyval = playerID : PData
public static Dictionary<int, PData> playersData = new Dictionary<int, PData>();
//...
This section could use a refactor…Anyway!
This file has 2 classes in it and I’ll explain each. The data here is likely to be very unique for each game, so edit and change as much as you need to.
PData class holds data we actually want to store on a player that is PUBLIC to all other players on the server. You can see I currently have playerId which is necessary to work out which PlayerNetworkEntity we are referring to, the other bits of data depend on your game. The sample project lets you name your character and set a skin before you go into battle, so those details are there, as well as how many kills and deaths they have which gets updated during the game to help decide on winners.
The PlayerData class is mostly useful for holding the playersData Dictionary which stores PData values against their playerId look up keys. When the server tells a client about all of the players data in a match, that static dictionary is where it lands. For convenience sake, we also have a static reference to our own PData if we’re a player in the game. The following functions help manage this data and you are welcome to chop and change these as you need to.
Methods
public static PData GetPlayerDataByPlayerID(int playerID)
Trys to find a players PData matching the playerID we pass in within the playersData dictionary. Returns null if it cant find a match
public static void AddKill(int playerID)
Attempts to add 1 kill point to players PData matching playerID within the playersData dictionary.
public static void AddDeath(int playerID)
Attempts to add 1 kill point to players PData matching playerID within the playersData dictionary.
public static void RemoveDisconnectedPlayerData(int playerID)
Remove players PData from the playersData dictionary with matching playerID
public static void ClearPlayerData()
Empties the playersData dictionary so that there are no entries in it
public static void ResetScores()
Sets kills and deaths back to zero for all players PData in the playersData dictionary
public static bool IsThereAWinner(int winningScoreCount, ref string winnerName)
Check to see if there is a players PData with a score matching or higher then the winningScoreCount. If there is a winner winnerName is set for the passed in reference for convenience. True or False is returned indicating if there was a winner or not