Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How to Setup Basic Multiplayer (Tutorial)

Author
Message
ThinkDigital
17
Years of Service
User Offline
Joined: 18th Aug 2005
Location: A galaxy far, far away...
Posted: 10th Jun 2007 11:56 Edited at: 10th Jun 2007 21:48
How to Setup Basic Multiplayer


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".



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



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 String

This 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:



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:



And you can then use the new information to create a new multiplayer session:



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 Name

Session 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:



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 String
as a Memblock
as a Image
as a Mesh
as a Sound
as a Bitmap
as a Integer
as a Float (or "Real" number)

Sending Information

For the process of sending information, the following commands are used:

send net message string To Player, String Value
send net message memblock To Player, Memblock Number, Guarantee Delivery Flag
send net message image To Player, Image Number, Guarantee Delivery Flag
send net message mesh To Player, Mesh Number, Guarantee Delivery Flag
send net message sound To Player, Sound Number, Guarantee Delivery Flag
send net message bitmap To Player, Bitmap Number, Guarantee Delivery Flag
send net message integer To Player, Value
sned 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 Information

For 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 Number

net message image Image Number

net message mesh Mesh Number

net message sound Sound Number

net message bitmap Bitmap Numbers

net 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.



-------------------------------------------------------------------

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

"Variables won't, constants aren't."
Turoid
19
Years of Service
User Offline
Joined: 1st Nov 2003
Location: The Netherlands
Posted: 10th Jun 2007 13:44
Great post! I only got some trouble with this bit of code:



I get the error whichs says that the session id is illegal? I ran it over LAN and also tried loop back adapter.

I am awesome and always right.
GatorHex
17
Years of Service
User Offline
Joined: 5th Apr 2005
Location: Gunchester, UK
Posted: 10th Jun 2007 14:46
sessionID = x ???

looking at the code x is not set to anything

"RegularNetGame"

try printing out the string to make sure what is there to make sure this matches.

If all else fails dump direct play and use benjamins Tempest ot MultiSync instead.

Benjamin
20
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 10th Jun 2007 15:06
Quote: "You then search the checklist for an entry which starts with "Internet TCP/IP""

The entries are displayed according to the language of the operating system, so you must let the user choose the correct entry in the checklist, otherwise it'll only work on English computers.

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
ThinkDigital
17
Years of Service
User Offline
Joined: 18th Aug 2005
Location: A galaxy far, far away...
Posted: 10th Jun 2007 21:41
@turoid/gatorhex: Yes, it should be 'i' not 'x'. It's fixed in the post.

@benjamin: Yes, I'm assuming the user is English speaking. You're right though, I'll add that as a note..

-ThinkDigital

"Variables won't, constants aren't."

Login to post a reply

Server time is: 2022-12-01 09:49:37
Your offset time is: 2022-12-01 09:49:37