James T
James T

Reputation: 1156

Get item of c# dictionary by index?

Im working on a project in unity, im using a dictionary to store player information and then assigning a userID int to the players which is what im attempting to make the index position of the players information in the dictionary, but i think im trying to use this system completely wrong. Currently i have this code:

public class Players : IComparable<Players> {
public int userID;
public string userName;
public int userHealth;
public GameObject userPlayer;

public Players(int newID,string Name,int Health,GameObject player){
        userID = newID;
        userName = Name; 
        userHealth = Health;
        userPlayer = player;
    }

    public int CompareTo(Players other){
        if(other == null){
            return 1;
        }
        return userID - other.userID;
    }
}

to create the Dictionary i use

private Dictionary<NetworkPlayer, Players> playerList = new Dictionary<NetworkPlayer,Players>();

to add to it i use

playerList.Add(player,new Players(playerList.Count,"Test", 100, playerObj));

I was hoping to use the playerList.Count part as a method of indexing it and then sorting it by this index to get the player i wanted back... is there a way of doing this correctly? This is my first time attempting to use dictionarys in c# and im finding it hard to understand how they work, if someone could help lead me to a working method of doing this. All i need to be able to do is to return data based off its index OR using the NetworkPlayer class.

If anyone could help lead me to a working method of doing this id be grateful, Thanks.

Upvotes: 3

Views: 8797

Answers (2)

Erfo77
Erfo77

Reputation: 1

You can also index the dictionary values by yourself enveloping the key-value pair you are interested into a KeyValuePair and associating it with an int like so:

Dictionary<int, KeyValuePair<string, int>> indexedDictionary = new Dictionary<int, KeyValuePair<string, int>>
    {
        {0, new KeyValuePair<string, int>("my entry", 13) },
        {1, new KeyValuePair<string, int>("whatever", 5) },
        {............}
    };

Upvotes: -1

Reed Copsey
Reed Copsey

Reputation: 564333

A standard dictionary's items are not sorted in this way. Normally, if you want to pull out the player by a specific ID, it would be better to make that the key in the dictionary, ie:

private Dictionary<int, Players> playersByID = new Dictionary<int, Players>();
private Dictionary<NetworkPlayer, Players> playersByNetwork = new Dictionary<NetworkPlayer, Players>();

Note that you could store two dictionaries, one for each form of lookup:

You could then store:

int id = nextID; // Using ID counter...

var newPlayer = new Players(id, "Test", 100, playerObj);
playersById.Add(id, newPlayer);
playersByNetwork.Add(player, newPlayer);

And fetch via:

var player = playersById[120];

Or via:

var player = playersByNetwork[netPlayer];

On a side note: I didn't use the Count as an ID, since that will fail if you ever remove players... If that is something you will never do in your system, you could go back to using the Count property for your next id.

Upvotes: 4

Related Questions