Search Unity

serverside - highscores ...

Discussion in 'Scripting' started by azabug, Dec 2, 2007.

  1. azabug

    azabug

    Joined:
    Aug 8, 2007
    Posts:
    111
    Hi guys

    Can anyone help me out ? I'm trying to implement the mySql/ php www highscores scripts ...
    I know this is dumb but I'm just not getting where I declare the "name " of the current player and the "score" .. or maybe I'm just not getting how the JSscript HSController actually works .. I think the url path is correct and the mySql data base table appear to be right ...

    I've attached the HSController script to a guiText gameObject and when I run it .. it displays the string "loading scores" ... and then does nothing .... I'm assuming that's because there's nothing to display yet ... so where and how do I declare what the players name and score is?

    Is it within
    function postScore(name, score) {
    //This connects to a server side php script that will add the name and score to a MySQL DB.
    // Supply it with a string representing the players name and the players score.
     
  2. azabug

    azabug

    Joined:
    Aug 8, 2007
    Posts:
    111
    Don't worry bout it ..... !!
     
  3. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    I'm doing this right now too.
    Let me know if I can help you.
    I've almost got it figured out.

    :)
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I don't think there is anything subtle about the postScore method - it's more likely that something else is wrong.

    Try accessing your URL from a web browser. If it works OK and the database gets updated, then some other aspect of the connection is at fault. If you are using a web player, then make sure the .unityweb file is in the same folder as the PHP page that activates the database (or maybe this restriction has changed in Unity 2.0?)

    If you can view the web server's access log, then you can check that the server has received the message from the Unity game. If it hasn't then the most likely problem is a dodgy URL - you may be able to retrieve the incorrect URL from the log and check it for capital letters, etc. Failing that, check that the score and name data are being sent correctly. Also, if you are using the MD5 hash thing, then check that it is being handled correctly (basically, just comment out the MD5 checks in the PHP script and see if it works without them).
     
  5. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    Any luck?
     
  6. azabug

    azabug

    Joined:
    Aug 8, 2007
    Posts:
    111
    Yeah ... I got it all going ... I neglected to include the Md5 script to handle the encryption .... it all seems to be working now ... I'm using it for a racing style game ..
    I need to rethink how I'm using though .... I want to generate a list of scores for the different games/tracks ... at the moment there's only two tracks, but there could be more tracks and also different game play modes ... you know like speed, pickups, stunts etc. ... so I'm thinking about how to implement a highscore system that can reflect the different score lists ... so I guess multiple mySql tables? or more columns within the tables with the added variables ... Track1, Track2, GameStyle1, GameStyle2 etc. and then call up the scores via a script that can read those values ...
    Code (csharp):
    1. if gameStyle1 == 1 {
    2. doSomethingLikeLoadThisScore;
    3. }
    4. else {
    5. dontLoadThatScore
    6. }
    or something to that effect, although as I'm not really familiar with the process ... I'm sure I'll have to do a bit more reading ...

    still ... it's essentially working and it's really just a matter of adapting it to suite the requirements of the game!

    How did you go with your attempts Hai_ok?

    Cheers!
     
  7. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    Yeah, got mine working.
    I was just trying to find out if I should try to write a tutorial, or if you got yours working.

    You should definitely use a separate table for each mode score list.

    You should try something like:

    Code (csharp):
    1.  
    2. var highscore_url="http://www.my-site.com/highscores.php";
    3.  
    4. function get_score(mode : String, name : String, score : String){
    5.     var form = new WWWForm();
    6.     form.AddField( "mode", mode );
    7.     form.AddField( "name", name );
    8.     form.AddField( "score", score );
    9.     var download = new WWW( highscore_url, form );
    10.     yield download;
    11.     if(download.error) {
    12.         print( "Error downloading: " + download.error );
    13.     }
    14.     else {
    15.         // show the highscores
    16.         print( download.data );
    17.         // you can send download.data to a string handler to display your high score list
    18.     }
    19. }
    So depending on which mode you send to your php, it will build the SQL query to read from that table in your database.

    Then in your PHP, use ORDER BY 'score' in your SQL query.
    Then it will either be in ascending or descending order. You can further use the 'name' value above as a way to change the color of the player's name when they look at the scores. That way it will be easier to see themselves wherever they appear in the score list.

    I hope this helps!

    Where to learn more (cause it's where I got my answers):
    SQL: http://www.w3schools.com/sql/sql_intro.asp
    PHP (Strings): http://www.tizag.com/phpT/strings.php
    Unity High Score (WWW) Page: http://unity3d.com/support/documentation/ScriptReference/WWW.html
     
  8. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    Also, you should be sending the current score (adding it to the database) and getting the high score list at the same time. You can easily do all this in a single php page, print it all to the screen and parse it with Unity.
     
  9. azabug

    azabug

    Joined:
    Aug 8, 2007
    Posts:
    111
    Thanks for that Hai_ok!

    Probably a more in depth tutorial wouldn't go astray ... I know it would have helped me out ...

    Cheers
     
  10. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If the information you store for each mode or track is very different then it might well be easiest to use a separate table for each. However, if the score data is essentially the same each time then you might consider just adding fields to your existing table.

    Say if you have several tracks in your game, you could add a field called "track" to the scores table. Then, in the SQL, you would just add an extra condition to the WHERE clause:-
    Code (csharp):
    1. SELECT * FROM scores_table WHERE (score > xxx) and (track = 2)...
    (Of course, the value of "track" would come from the web form in practice.)