Chapter IX

Handling Server and Client Disconnections

 

  Now that we can connect and move our players around, I suppose we should figure out how to handle what to do when a player quits or the server quits or crashes.

  Let's handle the problem of what to do if a player quits or gets disconnected first. How we will do this, is to use the player's entity itself by using a useful 3DGS entity event called ENABLE_DISCONNECT. So let's add some code at the top of function move_player and then I will explain how it works.

 

function move_player

{

 

my.ENABLE_DISCONNECT = ON; // player can disconnect from session

my.EVENT = player_events; // player events function

 

  When our action is first called we enable the disconnection event. This event is triggered when the entity's client has disconnected and left the multiplayer game. This can happen by the player quitting or and unexpected disconnection. Now that we have the event set ON we need must specify the function that will be called when there is and event.Our event function will be function player_events(). So let's add that function now under the server_called() function.

 

endif;

}

}

 

//--------------------------------------------------------------------

// PLAYER EVENTS

//--------------------------------------------------------------------

function player_events()

{

// client disconnected

if (EVENT_TYPE == EVENT_DISCONNECT)

{

number_of_players -= 1; // decrement number of players

// Go through all entities, any player# that was above mine, decrement his player number

you = ent_next(NULL);

while(you != NULL)

{

if(you.player_number > my.player_number)

{

you.player_number -= 1;

send_skill(you.player_number, SEND_ALL); // send new player#

}

you =ent_next(you);

}

send_var(number_of_players); // let everyone know new number of players

ent_remove(me); // remove ent of player that quit

}

}

 

  This function will be called when ever a event happens for this entity. We first check to see if the event that got us here was a disconnection event.

if (EVENT_TYPE == EVENT_DISCONNECT)

  If the entity's client was disconnected then we conitnue with our code to remove him from play. First we decrement the number_of_players in the game since we just lost one.

number_of_players -= 1; // decrement number of players

  Then we use a ent_next() loop to go through all the entities in the level, checking for any entity that is a player who's player_number is greater that the player_number of the player that quit. If a player's player_number is greater than the player_number of the player who disconnected, when decrement his player_number by one, thus keeping all player's player_numbers in between 1 and the number_of_players in the game and then send his new player number to all the clients so they know. This way we don't end up with a player with a player_number = 20 when there are only 5 players currently in game. You might want to read this paragraph twice.

  Anyhow, here's how we did it.

// Go through all entities, any player# that was above mine, decrement his player number

you = ent_next(NULL);

while(you != NULL)

{

if(you.player_number > my.player_number)

{

you.player_number -= 1;

send_skill(you.player_number, SEND_ALL); // send new player#

}

you =ent_next(you);

}

  After that we send the new number_of_players to all of the clients and then remove the entity of the player we loast.

send_var(number_of_players); // let everyone know new number of players

ent_remove(me); // remove ent of player that quit

  Now that client disconnections are handled, let's add some code for if the server disconnects or crashes. I can't take credit for this ingenious bit of code. Thank SchokoKeks for this beauty. Add the following lines of code into the main() function.

 

move_camera(); // call simple 3rd Person chase camera

input_scan(); // get inputs

 

}

 

// main game loop

while(1)

{

// server disconnected so quit client too

if (dplay_latency == 0 && dplay_bps == 0 && connection == 2)

{

exit;

}

 

wait(1);

}

 

  What happens here is that if the clients latency , bps, and bpspeak are all equal to zero and he has a connection set, either someone has developed the perfect networking system or the server has disappeared.

  I love code like that! Simple yet effective.

  Now if you save and Publish or Resource and do a test, you should be able to join and quit as you like and the program will remove disconnect player's entities and keep player numbers and number of connections all in order.

  That all for this chapter folks. Get ready for the toughest chapter of all because it is next and I guarantee it will fry your brain.

 

Previous Page Contents Next Page