Sign in to follow this  
starstriker1

Handling objects in a multiplayer game

Recommended Posts

I'm starting my first multiplayer game. I have a simple example game to look at for tips. In that game, a class called MsgDaemon assigns an ObjID to every object, and objects can send messages at will to any object simply by using the ID number. What ways are there to keep track of objects in a multiplayer game? In the past, I just used big arrays, but I'm not sure how well those would work in a client/server environment.

Share this post


Link to post
Share on other sites
what language are you using? if it's C++, i would use a std container. you should use a std container which fits the way you handle your objects.

personally, for storing players i use a std::map. a std::map is basically just a lookup table. i map an unsigned char ID to an instance of my Player class. then when i get an ID of a player (like how you are describing) i do a lookup to get the Player whos ID it is and perform whatever action with that player. when a player connects i add them to the table, and when they disconnect i remove them. the std::map takes care of doing all the hard work for me and it has O(log n) (IIRC) access time. you could use a hash map to get O(1) (IIRC again [smile]) if you want.

Share this post


Link to post
Share on other sites
well, IMO it is really the best way to do it. this is since you are always given an ID and must find who owns that ID. this is what std::map and lookup tables are made for. it is the fastest way to do it that i know of, and also uses the smallest amount of code. with a std::list or vector you would have to loop through possibly the entire container looking who owned the ID.

Share this post


Link to post
Share on other sites
Usually, a std::hash_map (which may be called stdext::hash_map) is faster and more efficient than a std::map, so if you don't need to iterate your objects in sorted order, try stdext::hash_map.

Also, you have to make sure that all object creation happens on the server -- else, hacked clients could cheat and create whatever objects they needed. There's also the issue of making sure there are no duplicate uses of an object ID. If all objects originate on the server, then only the server needs to enforce that, which makes the code simpler.

Share this post


Link to post
Share on other sites
Note: if using Visual C++ /.NET, see this page for details of getting a hash_map implementation:
http://forums.devshed.com/t55093/s.html

Share this post


Link to post
Share on other sites
Quote:
Original post by Aph3x
Note: if using Visual C++ /.NET, see this page for details of getting a hash_map implementation:
http://forums.devshed.com/t55093/s.html


.Net 2003 doesn't have the issue discussed in the linked article but you need to use stdext::hash_map instead of std::hash_map since it has changed namespace in this version (and thus the "std" version is flagged as deprecated).

Gizz

Share this post


Link to post
Share on other sites
A word of warning: I found out when I was looking into using std::hash_map that it is not yet part of the C++ standard. It's implemented in some STL implementations (Microsoft, SGI), but not others. So if you are planning on writing cross-platform code or using multiple compilers, this may not be the best choice.

I hope they get it into the next revision of the C++ standard, because a hash map would be very useful.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this