Highscores not loading properly

Hello!

We’ve been having some issues with our highscores. After the user ends his game, a scene with a text field and a submit button appears. Also, to the right of these, there is a list with all the Online highscores. This list remains “Loading”… Even after the user hits “submit”, the Loading doesn’t disappear, and highscores are not shown.

We are using the highscores script from the wiki.

private var secretKey="mypassword"; // Edit this value and make sure it's the same as the one stored on the server
var addScoreUrl="http://mysite.com/addscore.php?";
var highscoreUrl="http://mysite.com/display.php";    
var puntajes; 
var scrollViewVector : Vector2 = Vector2.zero;
var linecount = 3;
var CustomTextBox : GUIStyle;
var mySkin : GUISkin;



function Start() {
	getScores();
}

function OnGUI () {
	
	GUI.skin = mySkin;
	if (GUI.Button (Rect (Rect ((Screen.width/2)-400,  (Screen.height/2)-20, 100, 25)), "Submit")) {
		postScore(textField.nombre,LevelManager.score);
		Application.LoadLevel ("HighScore");
	}
	
	//GUI.Label (Rect ((Screen.width/2) - 110,(Screen.height/2)-140,400,40), "                  Top 100 Scores");
	scrollViewVector = GUI.BeginScrollView (Rect ((Screen.width/2) - 130, (Screen.height/2)-110, 250, 500), scrollViewVector, Rect (0, 0, 200, linecount*20));
	GUI.TextArea (Rect (0, 0, 250, linecount*20), puntajes, CustomTextBox);
	GUI.EndScrollView();
}





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.
    var hash=Md5.Md5Sum(name + score + secretKey); 
 
    var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash;
        
    // Post the URL to the site and create a download object to get the result.
    hs_post = WWW(highscore_url);
    yield hs_post; // Wait until the download is done
    if(hs_post.error) {
        print("There was an error posting the high score: " + hs_post.error);
    }
}
 
// Get the scores from the MySQL DB to display in a GUIText.
function getScores() {
     puntajes = "Loading Scores";
    hs_get = WWW(highscoreUrl);
    yield hs_get;
    
    if(hs_get.error) {
    	print("There was an error getting the high score: " + hs_get.error);
    } else {
   puntajes = hs_get.data; // this is a GUIText that will display the scores in game.
        var lines = puntajes.Split("\n"[0]);
		linecount = lines.length -1;

    }
}

Finally, we tried to separate the names and the scores… However, we only get one value from the server, which is hs_get.data. How do we separate those two, in order to put them in two separate labels?

THANK YOU! :smile:

Heh this is kind of a non-GUI question…

Basically this is “string parsing 101”.

Now, the big annoyance is that Unity’s version of JavaScript is not NEARLY as nice as “real” JavaScript for this kind of thing. What you need is to use the Mono libraries.

In JavaScript suppose your data looked something like this:

name,score
name,score
...
name,score

You could do something like:

list = hs_get.data.split("\n");
for(i=0; i<list.count; i++){
  parts = list.split(",");
  GUI.Label(... parts[0] ... ); // name
  GUI.Label(... parts[1] ... ); // score
}

if Unity JavaScript worked like … JavaScript. But, it doesn’t, so to get this behavior you need Mono Arrays and String utilities.

So up the top of the script you need:

import System; // imports a bunch of Mono crap

You’ll need to declare your variables thus:

var list : Array;
var parts : Array;

And use Mono’s weird-ass Split function:

list = hs_get.data.Split(","[0]); // Split takes a char parameter which you get by indexing off a string

Whats wrong with splitting by char? You don’t need more, at least not in this example.

Correct me if I’m wrong, but instead of “,”[0], couldn’t you write ‘,’?

Not in Javascript, no.

–Eric

In real JavaScript not only couldn’t you type “,”[0] … you wouldn’t need to.

BTW I spent quite a bit of time banging my head against that one…

The last language I coded in with this feature was the BASIC on the ZX-80 (which let you treat any string as an array).

Hello people!

Thank you for your replies. Seems we still have some tiny issues with this script. Two questions:

  1. Where exactly does this code go in my script? (we already imported Mono system…
list = hs_get.data.Split(","[0]);
  1. While doing one test, I got a error msg saying that I can’t convert Strings to Arrays. Is this what’s actually supposed to happen or did I missed something?

Thank you!

Use String[ ] instead of Array.

var list : String[];

Also you don’t need to do “import System;” when using Split. Not that it hurts anything.

–Eric

For some reason when I 'm trying to do this , Unity told me that Insert a semicolon at the end of:
GUI.Label(… parts[0] … ); // name

But if have one… what could be the problem??
Thanks…

You almost certainly have other errors. Fix those first.

–Eric

I was trying to solve the other problems that can cause the message of the semicolon, and now unity told me that

any ideas??

Needs to be ‘Split’ and not ‘split’. Also it looks like you’re trying to split an array of strings, which is what (String) means. You can only split single strings.

–Eric

This is an old topic. I can get the hs_get.text to send a copy of the php file to the guiText but I’m not sure that the script is connecting to the database. And I don’t know how to get the actual data. I’ve installed sql and have localhost working. Unity also attempts to check the database from the editor. But I still have no idea how to get that echo to work. If that’s what I need. Has anyone seen that before?

Where display.php is printed out when hs_get.data is called. ( text is the same thing according to the documentation.)