Microsoft DirectX 8.1 (Visual Basic) |
In Microsoft® DirectPlay® , a message is essentially a block of game-related data that you send to one or more members of the session. DirectPlay does not specify the contents or format of the data block, it just provides a mechanism to transmit the data from one user to another. Once the game is underway, each session member will normally send a constant stream of messages to all other members of the session for the duration of the game. The primary purpose of these messages is to keep the game state synchronized, so that each user's application displays the same UI. However, messages can also be used for a variety of other game-specific purposes.
For many games, especially rapidly changing ones, you may have to manage your messaging carefully. DirectPlay throttles outgoing messages to a level that can be handled by the target. You will have be careful that you do not send messages too rapidly, and ensure that the most important messages get through. See Basic Networking for a discussion of how to effectively handle DirectPlay messaging.
To send a message to another session member, call DirectPlay8Peer.SendTo. DirectPlay will call that members DirectPlay8Event.Receive method with the data. To send a message to a specific player, set the idSend parameter to the player ID that you when your DirectPlay8Event.CreatePlayer method was called. You can also send a message to every player in the session by setting idSend to DPNID_ALL_PLAYERS_GROUP. You can also define groups of players, and use a single SendTo call to send a message to all members of a group.
Note You can also use the DirectPlay8Peer.SetPeerInfo method to send information to other users. DirectPlay will call their DirectPlay8Event.InfoNotify method with the information. However, this way of transmitting information is not very efficient, and should not be used for normal messaging.
Many games allow players to be organized into groups. For instance, strategy games typically allow individual players to be organized into groups that can then be directed as a single entity. Microsoft® DirectPlay® also allows the formation of groups of players. DirectPlay groups are essentially a way to simplify your messaging. Once you have defined a group, you can send a message to every group member with a single DirectPlay8Peer.SendTo. While DirectPlay groups normally correspond to the groups that are defined by the game, you are free to create a group for any reason.
To create a DirectPlay group, call DirectPlay8Peer.CreateGroup. DirectPlay will call all session members' DirectPlay8Event.CreateGroup method with the details. The method's lGroupID parameter will be set to the group ID that you can use to send messages to the group.
Once the group is created, you then add players by calling DirectPlay8Peer.AddPlayerToGroup. DirectPlay will then call all members' DirectPlay8Event.AddRemovePlayerGroup with the IDs of the group and the player that was just added.
Once the group is established, you can send data to the group by calling DirectPlay8Peer.SendTo, with the idSend parameter set to the group ID. DirectPlay will call all group members' DirectPlay8Event.Receive method with the data.
To remove a player from a group, call DirectPlay8Peer.RemovePlayerFromGroup. DirectPlay will call the session members' DirectPlay8Event.AddRemovePlayerGroup method with the player's ID.
Finally, when you no longer need the group, you can destroy it by calling DirectPlay8Peer.DestroyGroup. DirectPlay will call all session members' DirectPlay8Event.DestroyGroup method with the group ID.