[center]
How to Setup Basic Multiplayer[/center]
To avoid any "Are you a noob?" questions, I'd like to first say:
This is a small, simply how-to for those who haven't yet learned how to use DBP's multiplayer commands. I'm not trying to expain multiplayer, I'm trying to show the basic structure of a multiplayer game setup based on my knowledge aquired from toying around and observing, and I'm only trying to be useful here. If anything I'm saying here is wrong, please correct me - I appreciate it.That having been said, this tutorial is again, very basic. It will focus on a peer-to-peer connection using TCP/IP, and packet sending and receiving (strings only, since everything else works almost the same). It will not cover the client-server model, any other protocols, or DLLs like MultiSync or Tempest.
Finally, the tutorial itself:
-------------------------------------------------------------------
Part 1: Setting up a Connection-------------------------------------------------------------------
The first step in a multiplayer setup is the connection. Consider this the means with which your computer communicates with another. Here we'll be setting up a "TCP/IP" type connection, which is by far the most common type.
Dark Basic recognizes these different types of connections using numbers, so the first step is to find which number represents the TCP/IP type connection. To find it, you use the command
perform checklist for net connections, which fills the checklist with a list of available "methods of communication". You then search the checklist for an entry which starts with "Internet TCP/IP".
+ Code Snippet`Retrieve the TCP/IP Connection Number
tcpip_num AS INTEGER
perform checklist for net connections
for i = 1 to checklist quantity()
if left$(checklist string$(i),15) = "Internet TCP/IP"
tcpip_num = i
endif
next i
NOTE (added 6/10/07): This method of determining the connection type would only work on English language computers - computers using other languages would not have a string that says "Internet TCP/IP Connection For DirectPlay". If your target is an audience with non-English computers, you need to ask the user for the TCP/IP connection session number.
Now that you've got the number representing a TCP/IP connection, you need to decide if the user is a
host or a
client. For those of you not familiar with the two terms, a host is someone who sets up their own multiplayer game session, while a client is someone who joins a host's multiplayer session. Before you can set up our TCP/IP connection, you need to find out if your user wants to be a host, or a client.
If the user is a client, they'll also have to supply an IP address - the address of a host's computer, so that their computer can find the host.
This code snippet determines whether the user is a host or a client, and if it is a client, asks for an IP address. If the user
+ Code Snippet`Find out if our user is a host or a client
userType AS STRING
clientIP AS STRING
cls
print "Are you hosting a game, or are you a client? Press 'H' for host, and 'C' for client."
sync
repeat
if lower$(inkey$()) = "c"
userType = "client"
cls
print "What IP address are you connecting to?"
sync
input clientIP
cls
endif
if lower$(inkey$()) = "h"
userType = "host"
endif
until userType <> ""
Finally, you need to tell Dark Basic what type of connection we are using, and whether the user is a host or a client. The command used to do these things is
set net connection Connection ID Number,
Host/Client Identification StringThis command tells DBP to setup a connection, using the connection specified by the ID number. The "Host/Client Identification String" is a string DBP uses to determine if the user is a host or client. If the user is a host, the string is simply a string containing a single space: " ". If the user is a client, the string is the IP address that they wish to connect to. So, you set up the net connection for your user:
+ Code Snippet`Setup Connection
if userType = "host"
set net connection tcpip_num, " "
else
if userType = "client"
set net connection tcpip_num, clientIP
else
exit prompt "User type not specified","Multiplayer Error"
end
endif
endif
And finally, you've set up your connection.
-------------------------------------------------------------------
Part 2: Creating a Game ("Multiplayer Session")-------------------------------------------------------------------
Now, your game can host games, and connect users to other hosts using your game. There is another small step, and that is creating or joining the multiplayer session talked about earlier. There are only two commands involved in this process; one for hosts, and the other for clients.
If your user is a host, they'll need to create a new multiplayer session for clients to join. This is done with the command
create net game Name of Game,
Player Name, 1
The last attribute is a flag telling Dark Basic that you're using a peer-to-peer style connection. For the purposes of this tutorial (and most of your purposes as well), this will be 1.
This command initiates a new multiplayer session, or game, asking for a name of the game and a name for the player. You can prompt the user for his name:
+ Code Snippet`Prompt the user for a screenname
playerName AS STRING
cls
print "What would you like to call yourself?"
sync
repeat
input playerName
sync
until playerName <> ""
cls
And you can then use the new information to create a new multiplayer session:
+ Code Snippet`Host: Create new game
if userType = "host"
create net game "RegularNetGame", playerName, 1
endif
The first attribute, the one that says "RegularNetGame", can be basically whatever you want it to, provided you don't make it too long. It's there because it's possible for a host to set up multiple multiplayer sessions at a time (essentially, different games to join) by himself. Having one host create multiple multiplayer sessions at a time is not a common practice among DB users, and is not part of my tutorial here. If your user is a host, you have just set up a multiplayer session for him.
If you user is a client, you must use a different command. The command
join net game Session ID Number,
Player NameSession ID Number is the Number DB assigns to the multiplayer session created by the host. Using the
perform checklist for net sessions command, we can fill the checklist with games available at the address they're connected to. Since all of your sessions for your game are named "RegularNetGame", all we need to do is find the entry (and, since the host only created one sessions, there should be only one entry) that is named "RegularNetGame" and feed it into the
join net game command, along with the player's name. This can be done as so:
+ Code Snippet`Find our session
sessionID AS INTEGER
perform checklist for net sessions
for i = 1 to checklist quantity()
if checklist string$(i) = "RegularNetGame"
sessionID = i
endif
next i
`Join the net session
join net game sessionID, playerName
Now, your two computer are connected and have established (and joined) multiplayer sessions!
-------------------------------------------------------------------
Part 3: Figuring out who's joined your session-------------------------------------------------------------------
Dark Basic contains commands you can use to find information about the people who are present in your user's multiplayer session.
You can fill the checklist with people present in the multiplayer session using the
perform checklist for net players command. The checklist commands return the following information:
checklist value a( ) returns the a local (meaning different from computer to computer) ID number for the given user.
checklist value b( ) returns the global (meaning the same on all computers) ID number for the given user.
checklist value c( ) returns 1 if the given player is you, and returns a 0 if the given player is someone else.
checklist value d( ) returns 1 if the given player was the game's host, and returns a 0 if the given player was a client.
checklist string$( ) returns the given player's name.
checklist quantity() will equal the total number of players in the current multiplayer session.
-------------------------------------------------------------------
Part 4: Sending Information Between Computers-------------------------------------------------------------------
Once two or more computers have connected, you can send information from one to another. This is useful for things like sending player positions and text messages.
When you send information to another computer, you can send it in eight forms (I will only talk about 1 - all the others work the same way). The eight forms you can send information in are:
as a
Stringas a
Memblockas a
Imageas a
Meshas a
Soundas a
Bitmapas a
Integeras a
Float (or "Real" number)
Sending InformationFor the process of sending information, the following commands are used:
send net message string To Player,
String Valuesend net message memblock To Player,
Memblock Number,
Guarantee Delivery Flagsend net message image To Player,
Image Number,
Guarantee Delivery Flagsend net message mesh To Player,
Mesh Number,
Guarantee Delivery Flagsend net message sound To Player,
Sound Number,
Guarantee Delivery Flagsend net message bitmap To Player,
Bitmap Number,
Guarantee Delivery Flagsend net message integer To Player,
Valuesned net message float To Player,
Value
NOTE: The commands for sending of memblocks, bitmaps, images, sounds, and meshes come with the "Guarantee Delivery Flag". Since these commands send large amounts of data, you have the option of setting this flag to '0' in order to encourage Dark Basic to not send the information if it would cause a slowdown. Setting the flag to '1' guarantees that the information will reach it's destination, even if it causes a slowdown.
The attributes for these commands are very clear. The
To Player attribute is the ID number of the player to whom you want to send the information. If you set this attribute to '0', the information will be send to everyone (other than yourself).
Recieving InformationFor the process of recieving information, there are several commands used to determine the presence of, and type of, any information that has been sent to the player. These commands are:
net message exists() Returns a '1' if there are net messages that have been sent to the player which the player has not yet seen.
get net message Gets information about the first waiting net message.
net message type() returns an ID number, telling you what type of net message this is. The ID numbers it can return are:
1 - Integer
2 - Float
3 - String
4 - Memblock
5 - Bitmap
6 - Image
7 - Sound
8 - Mesh
Once you've grabbed a waiting net message and determined it's type, you can use these commands to get the information that was send to you.
net message string$() returns the string that was sent.
net message memblock Memblock Numbernet message image Image Numbernet message mesh Mesh Numbernet message sound Sound Numbernet message bitmap Bitmap Numbersnet message integer() returns the integer that was sent.
net message float() returns the real number that was sent.
As one final code snippet, I have a loop that checks for unseen net messages and deals with them.
+ Code Snippetget net message
while net message exists() = 1
if net message type() = 3 : `A string is recieved
print "A string was recieved: ";net message string$()
endif
if net message type() = 1 : `An integer is recieved
print "An integer was recieved: ";net message integer()
endif
get net message
endwhile
-------------------------------------------------------------------
That's it. I'll admit the the later parts don't have as many code snippets, but I think they're easily understandable.
Hope it was helpful.
Constructive criticism, correction, etc... any would be nice

-ThinkDigital