Post by mikehense on Feb 16, 2015 20:11:08 GMT
Playing against another person is always more exhilarating than playing against the computer... whether it be a First Person Shooter, or a Race Game... whether you are playing co-op or death match style, the element of having another human in the game adds that element of immersion and competitiveness that playing against the computer is hard to duplicate...
3DRAD is capable of making MultiPlayer games, and its a lot easier than most people think... within a half an hour you can have your own multiplayer game up and running... Today, in response to a question on Facebook about a multiplayer tank game, i'm going to show how easy it is to ccreate the basis for one... a simple multiplayer car game that can be extended to be a multiplayer tank shooter...
Before we start adding a single object, i think its best we go over a few 3DRAD Multiplayer Basics... some fundamental concepts that are necessary to understand in order to make this happen...
The 3DRAD Client/Server Model is the framework that all multiplayer games in RAD are based on... it consists of two programs running on different machines, that connect to form the running game... first you have the Server, the program where the most of the actual game takes place... and you have the Client, the program that connects to the server so that remote users can join and play the game...
All Physics and Game Logic happens on the Server program, and the Client send input to the Server and echo the position of the skinmesh objects to the Clients...
This is done in 3DRAD by the use of several Net Aware Objects which automatically synchronize across the game simulation... the most useful of these objects being the Skinmesh object and the Event On Input object... Synchronization is done automatically by assigning each of these net aware objects a Player Number and/or a unique Network ID... objects with the same Net ID are said to be synched across the simulation...
3DRAD is capable of making MultiPlayer games, and its a lot easier than most people think... within a half an hour you can have your own multiplayer game up and running... Today, in response to a question on Facebook about a multiplayer tank game, i'm going to show how easy it is to ccreate the basis for one... a simple multiplayer car game that can be extended to be a multiplayer tank shooter...
Before we start adding a single object, i think its best we go over a few 3DRAD Multiplayer Basics... some fundamental concepts that are necessary to understand in order to make this happen...
3D RAD Multiplayer BASICS
The 3DRAD Client/Server Model is the framework that all multiplayer games in RAD are based on... it consists of two programs running on different machines, that connect to form the running game... first you have the Server, the program where the most of the actual game takes place... and you have the Client, the program that connects to the server so that remote users can join and play the game...
All Physics and Game Logic happens on the Server program, and the Client send input to the Server and echo the position of the skinmesh objects to the Clients...
This is done in 3DRAD by the use of several Net Aware Objects which automatically synchronize across the game simulation... the most useful of these objects being the Skinmesh object and the Event On Input object... Synchronization is done automatically by assigning each of these net aware objects a Player Number and/or a unique Network ID... objects with the same Net ID are said to be synched across the simulation...
OK, Lets Get Started
We're going to start out just like we do if we were making a single player game... first we will make the Server... Add a Terrain, a Skybox, and a GForce object...
Next we add our Car object a lil above the Terrain, and a Skinmesh object right atop the Car... link the Skinmesh to the Car so that it'll follow it around...
To make it work we need to link the car to the Terrain and to the GForce objects... check the Car Properties to see if the default controls are what you want (UP, DOWN, LEFT, RIGHT), focus the view on the car and test run the game in the editor...
You should be able drive around the terrain and control the car... the first step is done, we've made a simple driving game... next, we're going to make it a Multiplayer Server...
Next we add our Car object a lil above the Terrain, and a Skinmesh object right atop the Car... link the Skinmesh to the Car so that it'll follow it around...
To make it work we need to link the car to the Terrain and to the GForce objects... check the Car Properties to see if the default controls are what you want (UP, DOWN, LEFT, RIGHT), focus the view on the car and test run the game in the editor...
You should be able drive around the terrain and control the car... the first step is done, we've made a simple driving game... next, we're going to make it a Multiplayer Server...
Making The Server
In order to make what we already have into a real Multiplayer Server we need to first add a Script object... in the Script object, copy and paste the following code...
You can name the Script Net Start... this will start up the 3DRAD networking system and set up this running program as a Multiplayer Server...
Next, lets go back to a few of our objects... first the Skinmesh... look at the properties of the Skinmesh and find the Network ID Property... set that to 6..
A lil note here about Network Ids and how i use em... each Network ID must be unique... this is what synchronizes the Skinmeshes across the simulation...
starting with 1, i use the first 5 for the controls of the vehicle and the 6th for the Skinmesh... the Server vehicle doesn't need to reserve these since it is being controlled from the Server program, but in order to keep things understandable, i maintain the convention... Player zero is always the player on the Server, and his Network IDs are 1 thru 10... the first Client, Player 1, uses Network IDs 11 thru 20... Player 2, the second client will use Network IDs 21 thru 30... this convention keeps things in order...
So, in my scheme, the 6th Network ID in a group will always be for the Skinmesh... 6 for Player 0 (on the Server), 16 for Player 1 (first Client), and so on... you'll see how this helps keep things organized in a few...
OK... save and test... you should see the network start after a short delay, and the program runs as before... not much difference yet... that's because even though we have a networked program running, the Server still is set up for only one player... we will take care of that next...
Next we're going to add a car for the first Client that will join the game... place it on the Terrain and like we did for the first car, add a SkinMesh (Buggy) over it and link em... Link the Car to the Terrain and GForce to enable the cars physics...
Next (important) we must set the new car up to be controlled by the client... in the Car properties, uncheck all the control keys... this is done so that the Car can be controlled by the Client via synched event objects we are about to add...
Now Go To The Skinmesh and set the Network ID to 16... When we make the Client, the Skinmesh For the Client will have the same Network ID, so it will automatically sync with the Car object here on the Server...
Next (another important step), we need to add 4 Event on Input Controls and set the control key properties for each one respectively to Up, Down, Left, Right (Fwd,Reverse,Left, Right Turn)... and set all of the Net Player properties to 1 (client 1, right) and the Network Ids 1 (Fwd), 2 (Reverse), 3 (Left), and 4 (Right) respectively... like the Skinmesh, when we make the Client program, there will be the client counterparts to these objects that will share the same Player Number and Network IDs...
Finally we need to link the 4 Event On Input objects and the second car to the Script object, and add a lil code so that when the Event On Input objects are triggered by the client, the script will trigger the Cars controls to make it move on the Server... and since the Skinmesh is synched for this car, the client will see it move on his machine as well... Neat eh
Add a lil Script to the Script Object so that it now looks like this...
(check your objects and IN OUT references as they may differ depending on the sequence which you added the objects)
Save and Run... You've just created the Multiplayer Car Game Server for two players...
Next, the Client... a lot less work
In order to make what we already have into a real Multiplayer Server we need to first add a Script object... in the Script object, copy and paste the following code...
//This script enables multiplayer mode for
//a project designed to run as the server.
bool NetworkActive = false;
void Main()
{
if (iInitializing())
{
//Activate the server network system.
if (iNetServerStart("NetworkBall Arena","Server",25857,987654321,100000))
{
NetworkActive = true;
}
}
if (iDeinitializing())
{
//De-activate the client network system.
if (NetworkActive)
{
iNetStop();
NetworkActive = false;
}
}
}
You can name the Script Net Start... this will start up the 3DRAD networking system and set up this running program as a Multiplayer Server...
Next, lets go back to a few of our objects... first the Skinmesh... look at the properties of the Skinmesh and find the Network ID Property... set that to 6..
A lil note here about Network Ids and how i use em... each Network ID must be unique... this is what synchronizes the Skinmeshes across the simulation...
starting with 1, i use the first 5 for the controls of the vehicle and the 6th for the Skinmesh... the Server vehicle doesn't need to reserve these since it is being controlled from the Server program, but in order to keep things understandable, i maintain the convention... Player zero is always the player on the Server, and his Network IDs are 1 thru 10... the first Client, Player 1, uses Network IDs 11 thru 20... Player 2, the second client will use Network IDs 21 thru 30... this convention keeps things in order...
So, in my scheme, the 6th Network ID in a group will always be for the Skinmesh... 6 for Player 0 (on the Server), 16 for Player 1 (first Client), and so on... you'll see how this helps keep things organized in a few...
OK... save and test... you should see the network start after a short delay, and the program runs as before... not much difference yet... that's because even though we have a networked program running, the Server still is set up for only one player... we will take care of that next...
Adding The Second Player's Car To The Server
Next we're going to add a car for the first Client that will join the game... place it on the Terrain and like we did for the first car, add a SkinMesh (Buggy) over it and link em... Link the Car to the Terrain and GForce to enable the cars physics...
Next (important) we must set the new car up to be controlled by the client... in the Car properties, uncheck all the control keys... this is done so that the Car can be controlled by the Client via synched event objects we are about to add...
Now Go To The Skinmesh and set the Network ID to 16... When we make the Client, the Skinmesh For the Client will have the same Network ID, so it will automatically sync with the Car object here on the Server...
Next (another important step), we need to add 4 Event on Input Controls and set the control key properties for each one respectively to Up, Down, Left, Right (Fwd,Reverse,Left, Right Turn)... and set all of the Net Player properties to 1 (client 1, right) and the Network Ids 1 (Fwd), 2 (Reverse), 3 (Left), and 4 (Right) respectively... like the Skinmesh, when we make the Client program, there will be the client counterparts to these objects that will share the same Player Number and Network IDs...
Finally we need to link the 4 Event On Input objects and the second car to the Script object, and add a lil code so that when the Event On Input objects are triggered by the client, the script will trigger the Cars controls to make it move on the Server... and since the Skinmesh is synched for this car, the client will see it move on his machine as well... Neat eh
Add a lil Script to the Script Object so that it now looks like this...
//This script enables multiplayer mode for
//a project designed to run as the server.
bool NetworkActive = false;
void Main()
{
if (iInitializing())
{
//Activate the server network system.
if (iNetServerStart("NetworkBall Arena","Server",25857,987654321,100000))
{
NetworkActive = true;
}
}
if (iDeinitializing())
{
//De-activate the client network system.
if (NetworkActive)
{
iNetStop();
NetworkActive = false;
}
}
// Check the Event On Input Objects To See If The Client Has Sent
// A Control Input, And If So Send to the Car
OUT_23=0;
OUT_22=0;
if(IN_0 !=0)OUT_23=1;
if(IN_66 !=0)OUT_23=-1;
if(IN_88 !=0)OUT_22=1;
if(IN_110 !=0)OUT_22=-1;
}
(check your objects and IN OUT references as they may differ depending on the sequence which you added the objects)
Save and Run... You've just created the Multiplayer Car Game Server for two players...
Next, the Client... a lot less work