SOLVED - How can I search and retrieve a single element from a database array? [C#]

Hello everyone,

I am trying to search a database so that I may get the score for a single player. My goal is to be able to store said score inside a int variable so that I may be able to compare it with something else.

At the moment, I’m trying to figure out how IEnumerator and the WWW module work. Luckily, I have some working examples to go by. For instance, this is how a tutorial showed me how to get all the scores with dreamlo.

HiScores.cs

public void DownloadHighscores()
{	StartCoroutine("DownloadHighscoresFromDatabase");
}

IEnumerator DownloadHighscoresFromDatabase()
{	WWW www = new WWW(webURL + publicCode + "/pipe/");
	yield return www;

	if (string.IsNullOrEmpty(www.error))
	{	FormatHighscores(www.text);
		highscoresDisplay.OnHighscoresDownloaded(highscoreList);
	}
	else 
	{	print("Error Downloading: " + www.error);
	}
}

The tutorial however does not go into how to download a single score. Luckily, the dreamlo asset comes with a dreamloLeadboard.cs class. Inside I found the following method:

IEnumerator GetSingleScore(string playerName)
	{
		highScores = "";
		WWW www = new WWW(dreamloWebserviceURL +  publicCode  + "/pipe-get/" + WWW.EscapeURL(playerName));
		yield return www;
		highScores = www.text;
	}

I’ve incorporated (more like “Frankensteined”) this into my HiScores.cs class like this:

public void GetTheSingleScore()
{	// print("say something");
	StartCoroutine(GetSingleScore(nameFromInputFieldString));
}

IEnumerator GetSingleScore(string playerName)
{
	highScores = "";
	WWW www = new WWW(webURL +  publicCode  + "/pipe-get/" + WWW.EscapeURL(playerName));
	yield return www;
	highScores = www.text;
    // print("anything");
}

Good news is, I’m not getting any errors:| Howerver I can’t tell if this is even doing anything. When I call the GetTheSingleScore() method from a button I don’t even get the printout logs I’ve commented out above.

Am I on the right track or am I “missing the mark” completely? I have a feeling I’m making this more complicated than it needs to be…

If this is something you’ve come across recently, please let me know how you approached it. If you can think of Anything that might steer me in the right direction, this humble noob would be most grateful!
Any advice, comment or suggestion is welcome.

Best regards,

Don

(This is related to another Question I posted a few days ago. I’ve made a little bit of progress since; I’ve at least figured out how to delete a username entry! I will be posting the answer for that there soon. I think/hope I’m on the right track with this too, Cheers…)

I just watched the video tutorial. First off, I’m not sure how he is getting “print” to work, but you need to use Debug.Log(“Message”); That will display everything you want to display. So pass in the www.text like that. Next, I hope you kept the FormatHighscores() method. You will be able to pass in the results from the single search and the get high scores search through that method. Otherwise, you will need to copy that method for the use of a single Highscore. I’ll explain how that works:

First you make a request to dreamlo for the highscore of the specific person. Dreamlo has an example url stating:

Get your data as pipe delimited:
http://dreamlo.com/lb/5872fb68b6dd1500a4c95652/pipe
and for a single person:

To get the score for just 1 person, just add -get and the person’s name:
http://dreamlo.com/lb/5872fb68b6dd1500a4c95652/pipe-get/Carmine

This (the url) is the value you are creating and then passing into the new WWW. So this is fine, you actually get the person the right way. If you type Debug.Log(highscores); instead of print(“anything”); you will get the very thing you were looking for. However, I’m assuming it will come in an undesirable format. This was why the person who made the tutorial created the FormatHighscores() method. It takes the first part (username) and the second part (score) of the string you pass it. I recommend, for testing purposes, you call that method and pass the www.text to it instead of assigning your highscores variable. It will then overwrite the current array of high scores you have and have just that single high score stored in it.

I hope this helps! Good luck!

I don’t know anything about the WWW module or the IEnumerator (but I’d like to check them out, they seem exiting (: ) but I do have suggestion for debugging. Try to use Debug.Log(“”) in your IEnumerator method rather than print(). I script in Java so I don’t know if print() works or not. If that does nothing, then perhaps IEnumerator needs to be called from another method to run. Otherwise, I would research what exactly the WWW module and IEnumerator do.

Thanks again @TheyLeftMe4Dead (and btw welcome to the community!) and @GrayMatterTutorials for the feedback! The solution heavily involved this [helpful UF thread][1].

The following addition to my HiScores.cs class, along with some other messy code, did the trick for me. First I deleted my GetTheSingleScore() and IEnumerator GetSingleScore(string playerName) methods . I then I added a slightly different version of some existing methods like this:

	public void DownloadJustOneScore(){
		StartCoroutine("DownloadOneScoreFromDatabase");
	}

	IEnumerator DownloadOneScoreFromDatabase()
	{	WWW www = new WWW(webURL + publicCode + "/pipe/");
		yield return www;
		if (string.IsNullOrEmpty(www.error))
		{	nameFromInputFieldString = inputFieldnameGraber.text;
			// check for (and replase) spaces
			nameFromInputFieldString = nameFromInputFieldString.Replace (" ","");

			GetSinglePlayerScoreB(www.text , nameFromInputFieldString);
		}
	}


	void GetSinglePlayerScoreB(string textStream, string playerName)
	{	string[] entries = textStream.Split(new char[] {'

‘}, System.StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < entries.Length; i ++)
{ string entryInfo = entries*.Split(new char {’|'});*