Lennaerts PHP mysql multiplayer tutorial

4

Index

Attached Files

The following files have been attached to this tutorial:

.capx

multiplayer.capx

Download now 501.64 KB

Stats

12,900 visits, 30,174 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 23 Nov, 2013. Last updated 19 Feb, 2019

Process relayed information

This is actually the hardest part to understand I guess and you will need a decent knowledge of how to loop through data with the expression tokenat().

Normally, I just have 1 string with the pipe sepperators, | but seeing as we have clusters of data, I made new clusters within the clusters.

data1|data2|clustereddata3-1-1[]clustereddata3-1-2[]clustereddata3-1-3[-]|data4|data5|clusterdata6-1[]clusterdata6-2[-]

As you can see there are two groups on position 3 and 6. (both only have one set, each set is terminated by a [-], so there can be as many sub sets of data in one of those clusters)

To get to these, you will need a loop in the main loop, or take the data out, and run a sepperate loop on them.

I do a bit of both, in the beginning of the main loop I grab the data on a certain position, for example, everything on position data3, and put that in a local variable(messages, gameshots, enemyunits).

Then during the loop, I can loop through that sepperate data without worrying what level sub event I am, seeing as some loops dont support more then 1 level sub events, but local variables do, as long as they are on top of the main loop.

This can be hard to understand for some, but I provided the files and the capx and a working example online together with the mysql file and php files.

There are lots of comments in thee capx and the php files.

In the PHP file are a couple more functions:

    
      
    //Catching the shot fired request
     if($_POST[request] == "shotfired"){    //Delete old shots
        mysql_query("delete from shotsfired where stamped < DATE_SUB( NOW(), INTERVAL 8 SECOND)");
         //Explode the details
      $locs = explode("-", $_POST[extra]);
     //Select all living players to update someone shot
       $query = mysql_query("select * from players where playercode != '$_POST[code]'");
        while($results = mysql_fetch_array($query)){
     //Add a shot entry for each player.
     mysql_query("insert into shotsfired (shooter,shootercode,playercodes,angle) values ('$locs[3]','$_POST[code]','$results[playercode]','$results[playerangle]')");
       }
    }
    
    //Catch the player hit request
    if($_POST[request] == "playerhit"){
        //Explode the details
        $locs = explode("-", $_POST[extra]);
        //Remove all older shots
        mysql_query("delete from shotsfired where `stamped` < (UNIX_TIMESTAMP() - 8)");
        //update kiled player killed amount and set dead to yes
        mysql_query("update players set state='yes', killed=killed+1 where id='$locs[2]'");
        //Update killer details
        mysql_query("update players set kills=kills+1 where id='$locs[3]'");
      //Loop through all living players to spread the word of the kill
       $query = mysql_query("select id from players where state='no'");
       while($results = mysql_fetch_array($query)){
    	 mysql_query("insert into messages (playerid,messages) values ('".$results[id]."', 'ID:".       $locs[3]." has killed ID:".$locs[2]."')");
         } 
        //Some redundant info
       $message = $locs[2]."|".$_POST[code];
        }
      
        //Player was killed and needs a restart 
      if($POST[request]=="playerrestart"){
          mysql_query("update players set state='no' playercode='$_POST[code]'"); 
       }
     
    

In the capx I added several events which should be obvious, such as player controls and rockets being shot/destroyed.

To understand how a player is killed is quite easy:

player makes a shot on his game and sends to database to inform all other living players that you shot.

when you actually hit a target, you sent the info, and the enemy is destroyed upon the next updatepositions run, this is to accomodate so when someone dies it is as close to simulataious on all playing fields as pobbile.

That should be it :) enjoy

I hope this all dont sound too confusing ... feel free to ask question :D

Good luck !!!

update: fixed the codes and correct some errrors, better page titles

.CAPX

multiplayer.capx

Download now 501.64 KB
  • 0 Comments

Want to leave a comment? Login or Register an account!