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?
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.
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
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
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:
function Start() {
// post a dummy name and score
postScore("scooter", 1000);
}
function postScore(name, score) {
// connect to server side php script that adds name and score to database
var highscore_url ="http://something.com/addScore.php?";
// supply player name and score
highscore_url += "name=" + name + "&score=" + score;
// post to the site and get the result
hs_post = WWW(highscore_url);
// wait until the download is done
yield hs_post;
// process any download errors
if (hs_post.error) {
print("There was an error posting the high score: " + hs_post.error);
}
}
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.
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.
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?
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.
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…
//OnMouseDown
function Update(){
if (Input.GetKey("p")){
var inputName = PlayerPrefs.GetString ("LastHighscoreName");
var inputScore= Persistent.m_Score;
PostScore( inputName, inputScore );
print(inputName +"," +inputScore);
}
}
//PostScore
function PostScore(name, score) {
//Connects to a server side php that adds the name score to a DB.
//Supplied a string representing the players name score.
var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(name) + "&score=" + score;
//Post the URL create a download object to get the result.
hs_post = new WWW(highscore_url);
// Wait until the upload is done
yield hs_post;
//Error Handling
if(hs_post.error) { print("Error posting score: " + hs_post.error); }
}
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.
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.
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.
All is well now
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.
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.
That feels much better
Thanks again for hand holding support
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.