Jump to content
  • Advertisement
Sign in to follow this  
Iderik

[SOLVED] [C#] Store connections

This topic is 3632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've been trying to ask for help in another thread, but I think it was so confusing so people didn't understand what I was saying. When a client connects, I want to create a new Player object which contains IP, name, x and y. Then for example I want to update or send a message to a player, I want to search/look up/access with IP or name. Here is a way I thought about doing, but doesnt really work. And I'm pretty sure this isnt the way of doing it. Maybe a dictionary would work, but how?
[SOURCE]
List<Player> lPlayerList = new List<Player>();

string myString = "127.0.0.1";

// Here I want to create a player object with the name of string playerName1, but it doesnt work.
Player myString = new Player();
[/SOURCE]
[/source] Could anyway PLEASE explain so a beginner understands, I would be really thankful for help, because I've been stuck here for ages. [Edited by - Iderik on October 9, 2008 7:38:15 PM]

Share this post


Link to post
Share on other sites
Advertisement
In that code sample, you are trying to create two objects, both of which have the same name "myString". Objects have to have unique names. You can't create a Player called myString if you've already created a string called myString.

Share this post


Link to post
Share on other sites
Using a dictionary would look something like:

Dictionary<string, Player> dict = new Dictionary<string,Player>();
dict["127.0.0.1"] = new Player();

For the most part you just use the string inside the []s whenever you want to lookup an entry in the Dictionary.

Share this post


Link to post
Share on other sites
I've got it:

Dictionary<IPEndPoint, Player> dPlayerDict = new Dictionary<IPEndPoint, Player>();

public void Add(IPEndPoint ip, string sPlayerID, int iPlayerX, int iPlayerY, int iOutfit)
{
dPlayerDict.Add(ip, new Player(sPlayerID, iPlayerX, iPlayerY, iOutfit));
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!