DirectX:DirectPlay:Tutorials:VB:DX7:Starting and Joining a Game

From GPWiki
Jump to: navigation, search

Before you can join a game, someone must have started one, so we'll cover that first. To start (or "host") a game, you need to set up a DirectPlaySessionData object and fill it with data:

Dim objSessionData As DirectPlaySessionData
 
    Set objSessionData = dp.CreateSessionData
    objSessionData.SetMaxPlayers 4
    objSessionData.SetSessionName "Game"
    objSessionData.SetGuidApplication AppGuid
    objSessionData.SetFlags DPSESSION_DIRECTPLAYPROTOCOL Or DPSESSION_MIGRATEHOST
 
    Call dp.Open(objSessionData, DPOPEN_CREATE)

Ack! Much code!

Fear not, fair computer nerd, it is simple. First, we init our DirectPlaySessionData object using the DirectPlay4.CreateSessionData method. Next, we cram it full of info: The max number of players to allow, the session name (how it'll appear when enumerated by others seeking to join a game), and the GUID! Remember that GUID thing I told you about?

Set the flags however you desire, but I would suggest including the DPSESSION_DIRECTPLAYPROTOCOL flag at the very least. It enables DirectPlay to do the best it can to accomplish what you ask of it, and emulate any functionality needed that is not present. The DPSESSION_MIGRATEHOST flag is also useful in some cases; it instructs DirectPlay to assign a new session host should the initial host drop out of the game, allowing the game to continue. There are a number of other flags available, but you'll have to check the SDK for those!

Once our DirectPlaySessionData object is sufficiently stuffed, we can pass it to the DirectPlay4.Open method along with the DPOPEN_CREATE flag to start our session. Done! Our session (or "game") has been set up, all we have to do now is wait for someone to join!

But that doesn't happen all by itself, I'm afraid:

Dim objSessionData As DirectPlaySessionData
 
    Set objSessionData = dp.CreateSessionData    
    objSessionData.SetGuidApplication AppGuid
    objSessionData.SetSessionPassword ""

In order to join a game, we must first set up a DirectPlaySessionData object and fill it with the criteria upon which we would like to discriminate sessions. When we search for available sessions, we will use this object as a sort of template. Any session that we come across that fits the template, we'll be informed about. So, in this case, we'd like to find only those sessions which have the same GUID as our program. Also, we set the SetSessionPassword property at this point, allowing us to enumerate sessions that require passwords (if none is required, pass "").

Dim objEnumSessions As DirectPlayEnumSessions
Dim intSessionCount As Integer
Dim i As Integer
Dim strDetails As String
 
    Set objEnumSessions = dp.GetDPEnumSessions(objSessionData, 0, DPENUMSESSIONS_AVAILABLE)
 
    intSessionCount = objEnumSessions.GetCount
    For i = 1 To intSessionCount
        Set objSessionData = objEnumSessions.GetItem(i)
        strDetails = objSessionData.GetSessionName & " (" & objSessionData.GetCurrentPlayers _
		& "/" & objSessionData.GetMaxPlayers & ")"
        LstBox.AddItem strDetails
    Next

Using the DirectPlay4.GetDPEnumSessions method, we enumerate all sessions available that match our GUID. This info is used to initialize a DirectPlayEnumSessions object. Once initialized, we can extract the total number of sessions from the object, and then step through each session and display its description to the user. The code above assumes that LstBox is a valid listbox object. This listbox is filled with the session name, max number of players, and current number of players for each session that was successfully enumerated.

Now that we've enumerated the available sessions, the user can make a selection. Then we need to join the selected session:

    Set objSessionData = objEnumSessions.GetItem(Index + 1)    
    Call dp.Open(objSessionData, DPOPEN_JOIN)

Assume here that Index is the index value returned when the user clicked on the listbox containing the enumerated sessions data. We get the session data with a call to the DirectPlayEnumSessions object's GetItem method. Then, we use this in a call to the DirectPlay4.Open method, and pass the DPOPEN_JOIN flag as well. C'est fini! We are now connected to the session selected by the user!

It may be useful to note the exact points during execution where service provider dialogs may arise. When hosting (starting) a game, calling the DirectPlay4.Open method using the DPOPEN_CREATE flag may invoke a service provider dialog. When using the modem, for example, a dialog box will pop up allowing the user to instruct the modem to wait for and answer the incoming call.

When joining a game, the DirectPlay4.GetDPEnumSessions method will invoke the service provider dialogs. Modem users will be prompted for a number to dial, TCP/IP users will be prompted for the IP address to connect to, etc.

It's time to play god for a second. Learn how to Create a Player. (Click here to download this tutorial's source code.)