Handling objects in a multiplayer game

Started by
6 comments, last by MonkeyCookie 19 years, 5 months ago
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.
Advertisement
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.
FTA, my 2D futuristic action MMORPG
Yes, I'm using C++. And the method that was used was pretty much what you're describing... I think I remember a "std::map".

But are there other options?
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.
FTA, my 2D futuristic action MMORPG
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.
enum Bool { True, False, FileNotFound };
Note: if using Visual C++ /.NET, see this page for details of getting a hash_map implementation:
http://forums.devshed.com/t55093/s.html
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
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.

This topic is closed to new replies.

Advertisement