C#: A way to handle players in terms of memory and storage

Started by
3 comments, last by CrashStar 15 years, 5 months ago
I was wondering, what is the best way to handle player data when it comes to memory and storage. For example, i have an MMO with 1000 players on the server. So they, and all of their data should be stored somewhere. Of course, there is data that is stored in a database and pulled out when needed, but there is some basic data like player name, current position and level, and so on, which should be presented to all of the players. So how to store that in C#? I am currently learning C# and this is the topic that bothers me at the moment. So there is a class "player", with... i dont know - "name", "x", "y", "level" elements. How to store that? I was thinking about making an array list to handle that because you can add elements in it without saying how big it is, so it can hold data about 10 players, as well as for 100 players. And you can easily put an object in it, as well as remove it. What do you think? Sorry for my bad english, and maybe noobishnes, I am still learning. :) Thank you in advance.
Advertisement
There are a number of ways that you can go about this, first of all, you should create a class that contains everything you need, it may look like the following for example:

class Player{   public:      Player();      string GetName();      int x, y; // coordinates      int level; // may want to use double for decimal's for the in between parts of a level      element Elements[numElements]; // array that holds elements called Elementsprivate:            string name;}Player::Player(string playerName, int x, int y, level = 0){      name = playerName;      this.x = x;            this.y = y;      this.level = level;}


Not a good example I know, but displays what you might want to add to it and how you might want to go about it. In terms of storing all the players in a single place that you can manage, either use a vector of Player objects (not sure if C# supports vectors), or a linked list. You can find more about linked lists here

http://cyberkruz.vox.com/library/post/c-tutorial-linked-list.html

Well, as Sean said the first step would be to create a class to store all this info in. Next you indeed get to the choice of the container in which you want to store the player class objects. In C# there luckily is no need to go an implement your own container, since you could simply use a List<Player> if you want to use the fancy generics or stick with a plain ArrayList if that works for you. You might want to check out some tutorials/resources about the characteristics (performance & mem use) of the available containers in C#, but please save yourself the trouble of implementing stuff like this yourself.

With that out of the way, there is little generic advice to be given without more info on your game. If the ArrayList or List<Player> works for you, then just stick with it until you need something different. If you want more information about that List<Player>, this is a so-called 'generic' list (see here) which can be constructed to hold only Player objects. There's a little more to it than that, but for starters it's enough to know that it's just a cleaner & safer alternative to using an ArrayList, which could have all kinds of objects in it.

Key point is that you shouln't have to go code these containers yourself; MS already did that for you (there's also a linked list flavor).
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
Another thing to keep in mind when choosing a container is how it will be populated and how it will be accessed. For instance, the requirement to find a player by name may ask for a different solution than a simple list (or perhaps you even need multiple types of storage for each specific scenario). Of course these are higher level decisions that can only be made with sufficient knowledge of the system at hand.
Thanks for the replies everyone, things are a bit clearer now :)

This topic is closed to new replies.

Advertisement