Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

High Scores in HTML

Discussion in 'Scripting' started by marty, Aug 14, 2007.

  1. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Could someone please show me how I can create a webpage that displays the high scores table created by the excellent Highscores template on the Unify Wiki?

    http://www.unifycommunity.com/wiki/index.php?title=Server_Side_Highscores

    The Wiki's template is designed to show the high scores inside a Unity app, but I want a webpage that will parse the MySQL database and print the scores.

    TIA!
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The display.php script would display the scores onscreen, albeit in a crude way, if you viewed it with a web browser rather than parsing its output from a Unity game. You can easily embellish this code with some HTML that would format the scores the way you want them. Basically, a PHP file can contain any other text outside the <?php ... ?> bits. You would just start with a contentless HTML page for your scoreboard and then paste in the entire code from display.php wherever you needed it. You can easily change the line that says
    Code (csharp):
    1. echo $row['name'] . "\t" . $row['score'] . "\n";
    ...to incorporate a bit of HTML that displays the scores in a table or whatever. You could put something like
    Code (csharp):
    1. echo "<tr><td>".$row['name'] . "</td><td>" . $row['score'] . "</td></tr>\n";
    ...to create a table row, for example.
     
  3. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Thanks, Andeeee!

    That's what I figured, but it isn't working for me. So, now I'm not sure what I'm doing wrong.

    When I call the display.php scripts, I don't get any errors, just a completely blank page.

    My guess is that I am not populating the database correctly maybe? I've tried to populate the database with the following script attached to an object in a simple Unity project:

    Code (csharp):
    1.  
    2. function Start() {
    3.    
    4.     //  post a dummy name and score
    5.     postScore("scooter", 1000);
    6.    
    7. }
    8.  
    9.  
    10. function postScore(name, score) {
    11.    
    12.     //  connect to server side php script that adds name and score to database
    13.     var highscore_url ="http://something.com/addScore.php?";
    14.    
    15.     //  supply player name and score
    16.     highscore_url += "name=" + name + "&score=" + score;
    17.        
    18.     //  post to the site and get the result
    19.     hs_post = WWW(highscore_url);
    20.    
    21.      //  wait until the download is done
    22.     yield hs_post;
    23.    
    24.     //  process any download errors
    25.     if (hs_post.error) {
    26.        
    27.         print("There was an error posting the high score: " + hs_post.error);
    28.        
    29.     }
    30. }
    31.  
    32.  
    Any suggestions?
     
  4. spadin

    spadin

    Joined:
    Aug 13, 2007
    Posts:
    26
    I suggest seperating the two functions. Rather than adding a highscore from Unity and then trying to retrieve the high score list back into Unity, I would break it down into as many pieces as possible.

    I'd start by populating the table by hand. Using PHPMyAdmin, go in and add some high scores. Then go with a web browser to display.php. If it works, then the problem is with how you are adding your scores. And so on.

    You could also post your PHP code here and I will can help you debug it.

    Sandro
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Also, you should get used to using the browser's View Page Source command liberally. One thing to check is that the web server is actually processing the PHP correctly. When you view the source in the browser, all the <?php ... ?> bits should have disappeared and been replaced by the output of the PHP script. If they haven't then PHP isn't active on the server or isn't working properly.

    Viewing the source can also help you spot unmatched quotes, etc. These are very easy to introduce with machine-generated HTML and can make big chunks of text vanish mysteriously.
     
  6. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    First off, thanks to Andeeee and Spadin for all the great suggestions and pointers.

    Second off, for anyone interested, everything here works just fine. The problem is that I am very, very dumb. Dumb enough to think that a PHP script named "addscore.php" is the same thing as one named "addScore.php". Can you image?
     
  7. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Okay, new but related question.

    Should I add some code to the addScore.php script to bubble sort the scores? Or is there a cool MySQL method to do that?

    OOPS! Nevermind. I just noticed that the querry automagically does that. Wow.
     
  8. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Trust me Marty, we've all been that "dumb"! :D
     
  9. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    I'm in good company then.

    ;-)
     
  10. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    Apparently our company edits the wiki. I just ran into the same issue and took note of your post. Sure enough I had the same error. I've updated the wiki to fix the typo.

    Also the SQL that generated the scores table caused an error (minor sql error). I've replaced that code as well.

    Unfortunately, I have yet to get my HighScore to work.

    Cheers,
     
  11. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    Ok my php works via the browser with a hardcoded url passing the values. However my testing code which uses the "p" key to call PostScore() does not add a score to the DB. It does print the correct info to be passed by PostScore(). Where's the pink elephant here? :? (is it multiple calls to PostScore???) I'll fix that and see. Until then...

    Code (csharp):
    1. //OnMouseDown
    2. function Update(){
    3.   if (Input.GetKey("p")){
    4.     var inputName = PlayerPrefs.GetString ("LastHighscoreName");
    5.     var inputScore= Persistent.m_Score;
    6.     PostScore( inputName, inputScore );
    7.         print(inputName +"," +inputScore);    
    8.   }
    9. }
    10.  
    11. //PostScore
    12. function PostScore(name, score) {
    13.     //Connects to a server side php that adds the name  score to a DB.
    14.     //Supplied a string representing the players name  score.
    15.       var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(name) + "&score=" + score;
    16.  
    17.     //Post the URL  create a download object to get the result.
    18.     hs_post = new WWW(highscore_url);
    19.  
    20.     // Wait until the upload is done
    21.     yield hs_post;
    22.  
    23.     //Error Handling
    24.     if(hs_post.error) { print("Error posting score: " + hs_post.error); }
    25. }
     
  12. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    Stopping multiple calls didn't fix it. Grr...
     
  13. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Firstly, have a look in the web server log to check that requests are being received from Unity. If they aren't then you might have the wrong URL for the PHP file. If you are testing with a Unity web player, then make sure that your PHP script is in the same folder as the UnityWeb file (it won't send requests to a URL that isn't in the same folder on the same server).

    If the requests are getting through, then try using the PHP error_log function in your script. Use it to write out the raw script parameters to a file so that you can check if they are getting through OK. If you are using the code from the wiki then you might be falling foul of a bug in the MD5 part - some code to fix this was posted in this thread.

    The main thing is that your PHP script works when called from the browser - it's basically OK.
     
  14. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    I'm testing via the editor on my MBPro accessing the .php at cuttlecandy.com
    The GetScores() works, it prints "testName 12345" but the PostScore() doesn't. The only error.log on the server in the directory where my .php lives is clean with no current errors.

    I'll have it print the url for me and I'll copy paste that into the browser to see if there is a typo introduced via the EscapeURL() function. I've never really worked with .php - my background is ColdFusion. It spells everything out for you. :)

    Thanks for your suggestion/help.

    Cheers,
     
  15. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    It's actually the access log you want, not the error log. Make a note of the time when you press the P key and then check the log to see if an access took place at that time. If not, then the web server never received any request, therefore something was wrong with the communication.

    I think my earlier post might have been a bit misleading, actually. The thing I was talking about was a function called "error_log". This is PHP's equivalent of Unity's Debug.Log. You can use it to send messages to a file or an e-mail when there is otherwise no output from the script (eg, when it is being requested by Unity rather than a browser). This might be handy for checking what the script is doing with the URL parameters after they have arrived at the server.
     
  16. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    All is well now :D
    I printed the generated url and sure enough the public var highscore was still set to addScore.php vs the coded value of addscore.php.

    <Rant level = 10>
    This is a Unity design feature of the public vars being set in the Inspector. I can't tell you how many hours I've lost and how many headaches this "feature" has cost me in the past and for some reason I continue to forget about it. Maybe because the mind tends to forget negative thoughts.

    If Unity had a #debug keyword that when used at head of the script that would reset the Inspector's public vars when the coded value were updated I think this "feature" would be less obtrusive. As it stands I'll go as far as say I _hate_ this feature.
    </Rant>

    That feels much better :D
    Thanks again for hand holding support ;)
     
  17. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I understand (as you know), but it sure beats having to recompile your scripts every time you want to change a variable. :) Especially as projects get larger and scripts get longer and more numerous.

    --Eric