- Home /
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..)
This thread seems to address the same thing. I will try to implement the method found here: [https://forum.unity3d.com/threads/string-get-specific-username-from-string-array.361470/][1]
Getting close.. [1]: https://forum.unity3d.com/threads/string-get-specific-username-from-string-array.361470/
Answer by donimation · Jan 10, 2017 at 08:39 PM
Thanks again @TheyLeftMe4Dead (and btw welcome to the community!) and @GrayMatterTutorials for the feedback! The solution heavily involved this helpful UF thread.
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[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < entries.Length; i ++)
{ string[] entryInfo = entries[i].Split(new char[] {'|'});
string username = entryInfo[0];
if (username == playerName)
{ print("name already exists");
int score = int.Parse(entryInfo[1]);
PlayPref.nameTaken = true;
print(username + ": " + score);
stateSet.checkAvailability();
return;
}
else
{ print ("name not found");
PlayPref.nameTaken = false;
stateSet.checkAvailability();
}
}
}
The if else statements at the end basically trigger the .nameTaken variable. They also call the .checkAvailablility() method that uses said variable to enable/disable the appropriate UI.
I have a feeling that pipe-get --amongst many, many other things-- would have worked better, but I'm glad I got it to work at all!:D:
Anyway, I hope this helps others.
Cheers! :D
P.S. This answer also pertains to this related post.
Answer by GrayMatterTutorials · Jan 09, 2017 at 08:09 AM
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:
and for a single person:Get your data as pipe delimited: http://dreamlo.com/lb/5872fb68b6dd1500a4c95652/pipe
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!
Thanks for the Feedback!
I actually worked out a solution to this last week. I am about to post how I did it in a bit.
print() has been working for me in C# but, based on recent comments, I think I'll start using debug.log more.
After reading through this forum thread I ended up -- as you said -- pretty much recycling the FormatHighscores() and some other IEnumerator methods until I got a "match" so to speak.
Thanks for looking out!
Cheers :D
Answer by TheyLeftMe4Dead · Jan 08, 2017 at 06:51 PM
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.
Thank you for the response! I actually did do some research and worked this out last week. I will post my solution soon.
As I'm getting better at coding I'm realizing how useful debugging is!:]
I reeeally haven't found much difference between print(() and debug.log() on the C# side of things, at least, so far.
Your answer
Follow this Question
Related Questions
Find index number in an Array of a transform by it's name 1 Answer
Search an array? 2 Answers
Splitting textasset into an array and binary search 1 Answer
Add lap time in scoreboard 1 Answer